简体   繁体   English

在从服务器接收数据时,在关闭套接字后显示/接收数据。 服务器发送后如何立即接收。

[英]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.

相关问题 通过套接字发送字符串。 服务器未收到 - Sending a String through socket. Server not receiving it TCP连接 - 服务器仅在关闭套接字后发送消息 - TCP connection - server only sends message after closing socket Java(客户端)和 C#(服务器)TCP 套接字。 和服务器从客户端读取无限的最后数据 - Java(client) and C#(Server) TCP Socket. and Server read infinite last data from client 如果使用 Java 接收数据,如何制作一个从服务器接收数据并将数据发送到服务器的程序 - How to make a program that receives data from the server and sends data to the server if data is received with Java 如何从单个套接字连续发送和接收数据报包。 - How to send and receive datagram packets continuously from single socket. Java套接字。 服务器如何知道客户端关闭流 - java socket. How to server know client close stream 客户端套接字将不会从服务器接收数据 - Client socket will not receive data from server 组播服务器接收发送的相同数据 - Multicast server receive the same data it sends 服务器刷新数据之前,套接字如何从服务器接收数据? - How can data be received by a socket from a server before server flushes the data? 无法从Java套接字服务器接收数据到C套接字客户端 - Trouble receiving data to C socket client from Java socket server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM