简体   繁体   English

ServerSocket可以在从客户端读取之前写入客户端吗?

[英]Can ServerSocket write to client before reading from client?

After a ServerSocket accepts a connection to a client, can the ServerSocket start writing data to the client before the client sends a message? ServerSocket接受到客户端的连接后,ServerSocket可以在客户端发送消息之前开始向客户端写入数据吗? When I try to write to the client from the ServerSocket first, the client is blocking forever tyring to read it. 当我尝试首先从ServerSocket向客户端写入数据时,客户端将永远阻止读取它的操作。 However, if have the client send a message to the server first, the I/O streams between them work OK. 但是,如果让客户端首先向服务器发送消息,则它们之间的I / O流可以正常工作。

The ServerSocket can neither read nor write, but the accepted Socket can do both, in either order, or simultaneously. ServerSocket既不能读取也不能写入,但是被接受的Socket可以按任何顺序或同时进行。

Probably you are reading lines but not sending lines, or else not flushing a buffered output stream or writer. 可能是您正在读取行而不是发送行,或者不是在刷新缓冲的输出流或写入器。

Yes, you are correct that I meant getting the Socket from the accept() method. 是的,您是正确的,我的意思是从accept()方法获取套接字。

My problem was very simple. 我的问题很简单。 The client was doing a readLine() method call but the server was not sending the "\\n" character. 客户端正在执行readLine()方法调用,但服务器未发送“ \\ n”字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM