Home > Back-end >  Java network programming the client startup has been stuck in reading the data returned by the serve
Java network programming the client startup has been stuck in reading the data returned by the serve

Time:11-25

Start the server, the client starts the server console print "hello server"
The client has been stuck in is. Read (bytes) method as a result, the client can't print in the console output "received access", without any error, the client and the server service has been unable to stop,
The client
 import Java. IO. IOException; 
Import the Java. IO. InputStream;
Import the Java. IO. OutputStream;
The import java.net.Socket;

Public class TCPClient {
Public static void main (String [] args) throws IOException {
The Socket Socket=new Socket (" 127.0.0.1 ", 9999);
OutputStream OS=socket. GetOutputStream ();
OS. Write (" hello server ". GetBytes ());

InputStream is=socket. GetInputStream ();
Byte [] bytes=new byte [1024].
Int len=0;
While ((len=is. Read (bytes))!=1) {
System. The out. Println (new String (bytes, 0, len));
}
Socket. The close ();
}
}

The server side
 import Java. IO. IOException; 
Import the Java. IO. InputStream;
Import the Java. IO. OutputStream;
The import java.net.ServerSocket;
The import java.net.Socket;

Public class TCPServer {
Public static void main (String [] args) throws IOException {
ServerSocket ServerSocket=new ServerSocket (9999);
The Socket Socket=serverSocket. The accept ();

InputStream is=socket. GetInputStream ();
Byte [] bytes=new byte [1024].
Int len=0;
While ((len=is. Read (bytes))!=1) {
System. The out. Println (new String (bytes, 0, len));
}

OutputStream OS=socket. GetOutputStream ();
OS. Write (" receive access ". GetBytes ());
Socket. The close ();
ServerSocket. Close ();
}
}

CodePudding user response:

The server into an infinite loop, waiting for the client to send data

SocketInputStream. The read method does not return 1, it is not like a file stream, file is stored locally, it is very easy to know whether the file has already read the last byte, for the flow of the socket, it is not impossible to know whether the data has been finished, because this is decided by the TCP/IP protocol,

So this kind of communication based on TCP/IP, tend to have a mechanism that is generally define a data in baotou, from the sender told the receiver, I'm going to send how many bytes of data, and then send the data, the receiver prior to read and parse the data in baotou, then knew what needs to receive the data, then receive data, and to count the number of bytes of data received, the end will continue to the back of the logic, this mechanism is called communication protocols,

CodePudding user response:

reference 1/f, little gray Wolf replies:
server into an infinite loop, waiting for the client to send data

SocketInputStream. The read method does not return 1, it is not like a file stream, file is stored locally, it is very easy to know whether the file has already read the last byte, for the flow of the socket, it is not impossible to know whether the data has been finished, because this is decided by the TCP/IP protocol,

So this kind of communication based on TCP/IP, tend to have a mechanism that is generally define a data in baotou, from the sender told the receiver, I'm going to send how many bytes of data, and then send the data, the receiver prior to read and parse the data in baotou, then knew what needs to receive the data, then receive data, and to count the number of bytes of data received, the end will continue to the back of the logic, this mechanism is called communication protocols,

Gratitude gratitude

CodePudding user response:

The process of network communication, TCP connection, the operating system will be set up to send and receive data at the bottom of the buffer,
After the client to send data, to refresh buffer, like flush the toilet, brush data out,
Please call flush function,
Similarly, if the server to the client to send data, also must carry on the flush operation,
  • Related