Java socket technology is divided into server-side socket:serversocket and client Socket:socket
ServerSocket and Socket Communication mode:
Service side:
1, ServerSocket listening port, call accept () to create a socket, and wait for the client's connection
2. Get the socket InputStream and read the data sent from the client.
Client:
1. Create a socket for the specified IP, port, and get the OutputStream output stream of the socket
2. Write data to the output stream, then flush () to send the data
Because the socket is two-way communication, all ServerSocket can read the client's data, can also get outputstream write data to the client, the client can also get InputStream read the server sent over the data.
Note: 1, accept () is blocked, InputStream is blocked. 2, write data after the need to flush ();
Now introduce a more practical question: How does the server and the client handle the data that is received?
The idea is: the server, the client after receiving the data to judge, for the specific character is to call the appropriate method, so that can handle a lot of logical business.
Also end the socket by passing data to the server or client-specific characters to tell the wait to end reading the data.
There is also a practical problem, how to handle the relationship between multiple clients and a server?
Here a server is referred to as an IP and port, in order to handle the traffic in the server to deal with multiple clients can actually create multiple sockets to correspond processing.
In order to be able to process the client's request asynchronously, after the server has created the socket into the thread to run, so that the implementation of asynchronous processing, processing method in the thread of the Run method is called.
The socket setting timed out and timed out to interrupt read data blocking.
Client.setsotimeout (10*1000);
Socket encoding Problem:
The input stream encoding of the client is consistent with the output stream encoding of the service side
The output stream encoding of the client is consistent with the input stream encoding of the service side
Attached to a specific URL: http://haohaoxuexi.iteye.com/blog/1979837#comments
JAVA SOCKET Technology