无法从通过SocketChannel接收的消息中提取正确的信息



我正在发送一个使用DataOutput流转换为字节的字符串

// creates a client socket which connects to the first successor's server.
Socket clientSocket = new Socket(host, succ1_port);
// send the output_string to the first successor.
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(output_string);

然后通过SocketChannel接收它们:

// accept connection
SocketChannel connectionSocket = tcpserver.accept(); // tcpserver is a ServerSocketChannel
ByteBuffer buf = ByteBuffer.allocate(48);
// store and print no. of bytes read
int bytes_read = connectionSocket.read(buf);
System.out.println("bytes read = " +bytes_read);
String from_client_string = new String(buf.array(), Charset.forName("UTF-8"));

接收到的消息格式为"XXXX XXX",其中X是0-9中的任意数字。

然后我试着把这些消息分成两部分:

Pattern p = Pattern.compile("([0-9]{4}) ([0-9]{3})"); 
Matcher m = p.matcher(from_client_string);
if (m.matches()){   
  int filename = Integer.parseInt(m.group(1));
  int hash = Integer.parseInt(m.group(2));
  System.out.println("filename = " +filename);
  System.out.println("hash = " +hash);
else
  System.out.println("no match");   

问题是,有时当我打印出从字节缓冲区转换的字符串时,它的值改变了…通常情况下,它是正确的,如"1234 210",但在其他情况下,它可能会删除一个数字,显示"1234 21"。然而,即使它是正确的,我没有得到匹配?我还发现读取的字节数总是变化的…

有谁知道这里的问题是什么吗?

谢谢你的帮助。

你应该在一个循环中继续读取套接字,直到它被关闭。如果没有可用的字节,读取将阻塞,因此请确保您正在关闭另一端的连接。

客户端

// creates a client socket which connects to the first successor's server.
Socket clientSocket = new Socket(host, succ1_port);
// send the output_string to the first successor.
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(output_string);
outToServer.close();
clientSocket.close();
服务器端

:

// accept connection
SocketChannel connectionSocket = tcpserver.accept(); // tcpserver is a ServerSocketChannel
ByteBuffer buf = ByteBuffer.allocate(48);
// store and print no. of bytes read
String result="";
while(connectionSocket.isOpen()){
  int bytes_read = connectionSocket.read(buf);
  System.out.println("bytes read = " +bytes_read);
  String from_client_string = new String(buf.array(),0,bytes_read Charset.forName("UTF-8"));
  reulst+= from_client_string;

}}

当你用ByteBuffer读取时,你会得到可用的字节数,这可能不是你写的所有字节。DataInputStream.readUTF()所做的是等待解码长度(16位无符号值)所需的字节数。

我建议您使用DataOutputStream.writeUTF(String)String DataInputStream.readUTF()。我建议你也考虑使用缓冲。

谢谢Peter!你的建议似乎起作用了:)我会投票给你,但我没有足够的声誉。

发送数据:

// creates a client socket which connects to the first successor's server.
Socket clientSocket = new Socket(host, succ1_port);
// send the output_string to the first successor.
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeUTF(output_string);
outToServer.close();    
clientSocket.close();

服务器端(接收数据)

// accept connection
SocketChannel connectionSocket = tcpserver.accept(); // tcpserver is a ServerSocketChannel
DataInputStream inToServer = new DataInputStream(connectionSocket.socket().getInputStream());
String from_client_string = inToServer.readUTF(); 

模式匹配的东西是一样的,在OP…除了现在它实际上似乎工作:)

最新更新