gpt4 book ai didi

java - 阻塞模式下SocketChannel读取返回0

转载 作者:行者123 更新时间:2023-11-30 03:35:18 25 4
gpt4 key购买 nike

我有简单的客户端和服务器,客户端基于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();
}
}


}

我从客户端看到的输出如下

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();

clear 通过将位置设置为零和容量限制来准备缓冲区以供读取。

但是flip然后设置了该位置的限制;即零。这意味着当您尝试读入缓冲区时,有零字节的空间。

摆脱那个flip调用。

<小时/>

As it is in blocking mode, i expect that it never returns 0 according to the documentation.

哪个文档? javadocs对于SocketChannel.read(ByteBuffer)说:

"It is guaranteed, however, that if a channel is in blocking mode and there is at least one byte remaining in the buffer then this method will block until at least one byte is read. "

在这种情况下,突出显示的条件为 false。

关于java - 阻塞模式下SocketChannel读取返回0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28109442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com