我有一个关于java SocketChannel的问题.
假设我在阻塞模式下打开了套接字通道; 在调用write(ByteBuffer)方法之后,我得到一个描述写入了多少字节的整数.javadoc说: "返回:写入的字节数,可能为零"
但究竟是什么意思呢?这是否意味着真正传递给客户端的字节数(以便发送方收到tcp ack表明服务器已接收到多少字节),或者这是否意味着已将字节数写入tcp堆栈?(这样一些字节仍然可能正在等待,例如在网卡缓冲区中).
我正在学习pubnub并且我阅读了他们的文档,但我找不到如何管理多房间聊天框.
默认情况下,任何人都可以收听频道.订阅它并在其上发布很容易.
我想要的是拥有一个主要的公共房间(到目前为止一直很好),但任何人都应该能够私下与其他人交谈而不会被其他用户阅读.
这些动态房间将被标记,用户应该能够从一个房间到另一个房间.
另一个要求是私下与某人交谈不会让你离开你订阅的其他房间(你仍然可以通知你在聊天时在另一个房间发布了一条新消息)
实现这一目标的最佳做法是什么?
我需要知道实现这一目标的最佳方法,因为文档仅描述了单个房间的基本情景,而且在互联网上没有任何相关内容.
谢谢.
PS:我知道PubNub一次不建议超过2个频道(即使我很难找到解释).
PPS:我正在使用punbub和socket.io
我试着了解java NIO是如何工作的.特别是SocketChannel如何工作.
我写下面的代码:
import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
public class Test {
public static void main(String[] args) throws IOException {
SocketChannel socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
socketChannel.connect(new InetSocketAddress("google.com", 80));
while (!socketChannel.finishConnect()) {
// wait, or do something else...
}
String newData = "Some String...";
ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();
buf.put(newData.getBytes());
buf.flip();
while (buf.hasRemaining()) {
System.out.println(socketChannel.write(buf));
}
buf.clear().flip();
int bytesRead;
while ((bytesRead = socketChannel.read(buf)) != -1) {
System.out.println(bytesRead);
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,方法socketChannel.read(buf)始终返回0并无限执行.
哪里弄错了?
假设我们有一个 JavaSocketChannel连接到一个正在等待传入数据的服务器:
SocketChannel server = SocketChannel.open();
server.connect(new InetSocketAddress(ip, port));
Run Code Online (Sandbox Code Playgroud)
我们发送我们的请求如下:
byte[] request = "This is a request for server!".getBytes();
ByteBuffer buffer = ByteBuffer.wrap(request);
buffer.flip();
int write = 0;
while (buffer.hasRemaining())
write += server.write(buffer);
System.out.println(write);
Run Code Online (Sandbox Code Playgroud)
上面的代码返回0,这意味着它不会向通道写入任何字节!
但是,如果我删除该buffer.flip()行,它将正常工作并发送数据:
byte[] request = "This is a request for server!".getBytes();
ByteBuffer buffer = ByteBuffer.wrap(request);
int write = 0;
while (buffer.hasRemaining())
write += server.write(buffer);
System.out.println(write);
Run Code Online (Sandbox Code Playgroud)
为什么是这样 ?!
阅读本教程后:http : //rox-xmlrpc.sourceforge.net/niotut/(这是关于编写非阻塞服务器和客户端的信息,我阅读了NIO部分,跳过了SSL部分),现在我正尝试重写自己的内容客户端,但是在尝试编辑客户端代码时遇到了问题。
首先,我想让您看到本教程的客户端代码,它包含2个文件:
RspHandler.java: http://rox-xmlrpc.sourceforge.net/niotut/src/RspHandler.java
NIOClient.java: http://rox-xmlrpc.sourceforge.net/niotut/src/NioClient.java
但是我在main函数中编辑了NIOClient.java 来解释我的问题,如下所示:
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.channels.spi.SelectorProvider;
import java.util.*;
public class NIOClient implements Runnable {
// The host:port combination to connect to
private InetAddress hostAddress;
private int port;
// The selector we'll be monitoring
private Selector selector;
// The buffer into which we'll read data when it's available
private ByteBuffer readBuffer = ByteBuffer.allocate(8192);
// A list …Run Code Online (Sandbox Code Playgroud) 非常新的NIO我正在建立一个聊天应用程序我在所有客户端都有连接,但在从客户端读取内容时我得到了java.nio.channels.IllegalBlockingModeException.请帮我这里是发生异常的代码.同时达到while (rbc.read(b) != -1)的PrintRequest class异常occures
public class PrintRequest extends Thread
{
public PrintRequest(SocketChannel sc,int i)throws Exception
{
System.out.println("going to enter the try block of PrintRequest");
try
{
System.out.println("Am in the try block of PrintRequest");
ReadableByteChannel rbc = Channels.newChannel(sc.socket().getInputStream());
System.out.println("checking in PrintRequest 0001");
WritableByteChannel wbc = Channels.newChannel(System.out);
System.out.println("checking in PrintRequest 0010");
ByteBuffer b = ByteBuffer.allocateDirect(1024); // read 1024 bytes
// int numBytesRead = sc.read(b);
System.out.println("checking in PrintRequest 0011");
while (rbc.read(b) != -1)
{
System.out.println("Am in …Run Code Online (Sandbox Code Playgroud) 我正在使用SocketChannel与远程服务器通信.我使用socketChannel.write()发送数据,没有错误和异常,但是,服务器日志表明没有收到数据; client tcp traffic monitor还显示ByteBuffer中的字符串消息未发送.
任何人都可以给我一个提示,为什么会这样?谢谢!
public class Client implements Runnable {
SocketChannel socketChannel;
Selector selector;
SelectionKey key;
ByteBuffer inbuf, outbuf;
int id;
@Override
public void run() {
try {
// prepare inbuf and outbuf
inbuf = ByteBuffer.allocate(10000);
outbuf = ByteBuffer.allocate(10000);
// prepare a socket channel for communication
socketChannel = SocketChannel.open();
socketChannel.connect(new InetSocketAddress("<remote server ip>", ));
selector = Selector.open();
socketChannel.configureBlocking(false);
key = socketChannel.register(selector, SelectionKey.OP_READ
| SelectionKey.OP_WRITE);
while (selector.select() > 0) {
if (key.isReadable()) {
// read from channel when server …Run Code Online (Sandbox Code Playgroud) 下面的Java代码:
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
public class Test {
public static void main(String args[]) throws IOException {
SocketChannel c = SocketChannel.open();
c.connect(new InetSocketAddress("google.com", 80));
ByteBuffer b = ByteBuffer.allocate(1024);
b.put("Request".getBytes());
System.out.println("Write: " + c.write(b));
int i;
while ((i = c.read(b)) != -1) {
System.out.println("Read: " + i);
b.clear();
}
}
}
Run Code Online (Sandbox Code Playgroud)
实际结果:
写:1017阅读:0阅读:1024阅读:44
第一次,方法read()读取0个字节.这不酷.
我修改了我的代码:
b.put("Request".getBytes());
System.out.println("Write: " + c.write(b));
b.flip(); //I added this line
int i;
while ((i = c.read(b)) != -1) {
System.out.println("Read: …Run Code Online (Sandbox Code Playgroud) 下面的Java代码:
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
public class Test {
public static void main(String[] args) throws IOException {
SocketChannel channel = SocketChannel.open(new InetSocketAddress(
"google.com", 80));
ByteBuffer buffer = ByteBuffer.allocate(1024);
while ((channel.read(buffer)) != -1) {
buffer.clear();
}
channel.close();
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码很简单.
但我没有向Channel写入任何数据,因此,它不包含任何要读取的数据.
在这种情况下,方法channel.read()执行时间过长,不返回任何数据.
我该如何处理这种情况?
谢谢.