[英]On receiving a data from the server it is displayed/received after closing the socket. How to receive it immediately once the server sends.?
I am sending a data from the server to client, but the client receives it only at the time of disconnect. 我正在从服务器向客户端发送数据,但客户端仅在断开连接时才收到数据。 But i need it as soon as the server sends.
但是一旦服务器发送我就需要它。 I dont know why it is sending the data late after the socket connection closes.
我不知道为什么它会在套接字连接关闭后发送数据。
public void run() {
try {
socket = new Socket(eHostIp.getText().toString(), Integer.parseInt( eHostPort.getText().toString() ) );
BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream() ), 1024 );
eReceiveData.setText( "Server Connected... XXXXXXX" );
String line;
while ((line = in.readLine()) != null) {
Log.d("read line",line);
eReceiveData.setText( line );
socket.setSoTimeout( 1000 );
//socket.close();
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} );
thread.start();
}
Check that your server sends an EOL character at the end of each message such that your BufferedReader can use the readline() method properly. 检查服务器是否在每条消息的末尾发送EOL字符,以便BufferedReader可以正确使用readline()方法。
Alternatively you can try to force flush() after each message as well. 或者,您也可以尝试在每条消息之后强制执行flush()。
This saved my day...REading the line as a character (line += (char)in.read()) 这节省了我的一天...将行重命名为一个字符(行+ =(char)in.read())
String line = " ";
while ((line += (char)in.read()) != null) {
Log.i( "read line", line );
eReceiveData.setText( line );
}
Thanks. 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.