Java SocketChannel read在阻塞模式下返回0
我有简单的客户机和服务器,客户机基于NIO,而服务器是一个简单的老式程序 我正在使用客户端的默认模式,即阻塞。在我试图从客户端编写的程序中,服务器读取它。然后服务器回复,客户端读取 我能够在没有问题的情况下写入服务器,但事实证明,从客户端的服务器读取是有问题的。由于它处于阻塞模式,根据文档,我希望它永远不会返回0。但事实并非如此,我总是看到client_channel.read的返回值为0 *******************************服务器*******************************************Java SocketChannel read在阻塞模式下返回0,java,sockets,nio,Java,Sockets,Nio,我有简单的客户机和服务器,客户机基于NIO,而服务器是一个简单的老式程序 我正在使用客户端的默认模式,即阻塞。在我试图从客户端编写的程序中,服务器读取它。然后服务器回复,客户端读取 我能够在没有问题的情况下写入服务器,但事实证明,从客户端的服务器读取是有问题的。由于它处于阻塞模式,根据文档,我希望它永远不会返回0。但事实并非如此,我总是看到client_channel.read的返回值为0 *******************************服务器*******************
class MyBlockingServer extends Thread
{
private int M_PortNumber;
private ServerSocket M_ServerSocket;
MyBlockingServer(int PortNumber)
{
M_PortNumber = PortNumber;
try {
M_ServerSocket = new ServerSocket(M_PortNumber);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run()
{
int my_number = 0;
while(true)
{
try {
Socket ClientServerTuple = M_ServerSocket.accept();
//System.out.println("Server address is "+ ClientServerTuple.getLocalAddress() + "Server Port is " + ClientServerTuple.getLocalPort());
//System.out.println("Client address is " + ClientServerTuple.getRemoteSocketAddress() + "Client address is" + ClientServerTuple.getPort());
DataInputStream inputStream = new DataInputStream(ClientServerTuple.getInputStream());
byte b[] = new byte[48];
inputStream.read(b);
System.out.println("[SERVER]" + new String(b));
DataOutputStream outputStream = new DataOutputStream(ClientServerTuple.getOutputStream());
byte c[] = new byte[100];
String output= new String("Thanks for connection, you suck tata" + " "+ my_number);
c = output.getBytes();
outputStream.write(c);
my_number++;
System.out.println("write done");
ClientServerTuple.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
void socket_close()
{
try {
M_ServerSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class JavaBlocking
{
public static void main(String []args)
{
MyBlockingServer Server = new MyBlockingServer(8000);
try {
Server.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class JavaChannels
{
public static void main(String []args)
{
SocketChannel client_channel = null;
try {
client_channel = SocketChannel.open();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("[Async Client] Socket channel open");
try {
client_channel.connect(new InetSocketAddress("127.0.0.1",8000));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("[Async Client] Socket channel connected");
ByteBuffer my_buffer = ByteBuffer.allocate(248);
try {
my_buffer.put("seven77".getBytes("UTF-8"));
} catch (UnsupportedEncodingException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
my_buffer.flip();
try {
int bytes_written = client_channel.write(my_buffer);
while(my_buffer.hasRemaining())
{
bytes_written = client_channel.write(my_buffer);
}
System.out.println("[Async Client] Wrote "+ bytes_written +" bytes");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("[Async Client] Socket channel write finished");
my_buffer.clear();
my_buffer.flip();
try {
int read_length = client_channel.read(my_buffer);
System.out.println("Initial read is " + read_length + " bytes");
while(read_length !=-1)
{
read_length = client_channel.read(my_buffer);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Reading the buffer." +"Read "+read_length +"bytes");
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("[Async Client] server says" + new String(my_buffer.array()));
try {
client_channel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
*******************************客户*******************************************
class MyBlockingServer extends Thread
{
private int M_PortNumber;
private ServerSocket M_ServerSocket;
MyBlockingServer(int PortNumber)
{
M_PortNumber = PortNumber;
try {
M_ServerSocket = new ServerSocket(M_PortNumber);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run()
{
int my_number = 0;
while(true)
{
try {
Socket ClientServerTuple = M_ServerSocket.accept();
//System.out.println("Server address is "+ ClientServerTuple.getLocalAddress() + "Server Port is " + ClientServerTuple.getLocalPort());
//System.out.println("Client address is " + ClientServerTuple.getRemoteSocketAddress() + "Client address is" + ClientServerTuple.getPort());
DataInputStream inputStream = new DataInputStream(ClientServerTuple.getInputStream());
byte b[] = new byte[48];
inputStream.read(b);
System.out.println("[SERVER]" + new String(b));
DataOutputStream outputStream = new DataOutputStream(ClientServerTuple.getOutputStream());
byte c[] = new byte[100];
String output= new String("Thanks for connection, you suck tata" + " "+ my_number);
c = output.getBytes();
outputStream.write(c);
my_number++;
System.out.println("write done");
ClientServerTuple.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
void socket_close()
{
try {
M_ServerSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class JavaBlocking
{
public static void main(String []args)
{
MyBlockingServer Server = new MyBlockingServer(8000);
try {
Server.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class JavaChannels
{
public static void main(String []args)
{
SocketChannel client_channel = null;
try {
client_channel = SocketChannel.open();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("[Async Client] Socket channel open");
try {
client_channel.connect(new InetSocketAddress("127.0.0.1",8000));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("[Async Client] Socket channel connected");
ByteBuffer my_buffer = ByteBuffer.allocate(248);
try {
my_buffer.put("seven77".getBytes("UTF-8"));
} catch (UnsupportedEncodingException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
my_buffer.flip();
try {
int bytes_written = client_channel.write(my_buffer);
while(my_buffer.hasRemaining())
{
bytes_written = client_channel.write(my_buffer);
}
System.out.println("[Async Client] Wrote "+ bytes_written +" bytes");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("[Async Client] Socket channel write finished");
my_buffer.clear();
my_buffer.flip();
try {
int read_length = client_channel.read(my_buffer);
System.out.println("Initial read is " + read_length + " bytes");
while(read_length !=-1)
{
read_length = client_channel.read(my_buffer);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Reading the buffer." +"Read "+read_length +"bytes");
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("[Async Client] server says" + new String(my_buffer.array()));
try {
client_channel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我从客户端看到的输出如下
Initial read is 0 bytes
Reading the buffer.Read 0bytes
Reading the buffer.Read 0bytes
Reading the buffer.Read 0bytes
我认为这是错误的:
System.out.println("[Async Client] Socket channel write finished");
my_buffer.clear();
my_buffer.flip();
清除
通过将位置设置为零和容量限制来准备读取缓冲区
但是翻转
然后将限制设置到该位置;i、 e.零。这意味着,与尝试读入缓冲区时相比,存在可容纳零字节的空间
摆脱那个flip
呼叫
由于它处于阻塞模式,根据文档,我希望它永远不会返回0 哪些文件?用于
SocketChannel.read(ByteBuffer)
的代码说明:
“但是,可以保证,如果通道处于阻塞模式,并且缓冲区中至少剩余一个字节,则此方法将阻塞,直到至少读取一个字节。”
在本例中,突出显示的条件为false。就是这样,我不认为clear会将状态更改为read。尽管文档特别提到它在阻塞时永远不会返回0,但这仍然很奇怪call@DesertIce什么文件?Javadoc清楚地表明,如果缓冲区中没有剩余的空间,它就可以准确地做到这一点。