且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java TCP 在客户端和客户端之间发送和接收多条消息服务器端在同一个套接字上

更新时间:2022-04-25 22:18:11

在服务器上,您将 PrintWriter 设置为 autoFlush(第二个参数)

On server you have PrintWriter set to autoFlush (second param)

outToClient = new PrintWriter(socket.getOutputStream(),true);

并在写入后调用flush().

and also calling flush() after writing.

outToClient.print(strresponse); //autoFlush here
outToClient.flush();

然后当客户端正在阅读

while((i = in.read()) > 0)

它可能正在读取第二次刷新.在这种情况下,它没有什么可读取的,退出循环并打印先前的响应.打印后尝试清除客户端上的响应并检查是否是此问题.

it is probably reading second flush. In that case, it has nothing to read, exits loop and prints previous response. Try to clear response on client after printing it and check if this was the problem.