One, Socket
Options for sockets
1) Tcp_nodelay: The default value is False. Represents the use of the Negale algorithm. When set to True, indicates that the socket buffer is closed and the data is sent immediately.
The socket sends the data by default using the Negale algorithm. The Negale algorithm means that the data sent by the sender is not emitted immediately, but is placed in the buffer, and the buffer is full and then emitted. After sending a batch of data, it waits for the other person to respond to this batch of data before sending the next batch of data. The purpose of this algorithm is to improve the efficiency of communication by reducing the number of transmissions.
2) SO_RESUSEADDR: The default value is False. When the socket connection is closed, the underlying port is not released immediately. To release immediately, set SO_RESUSEADDR to True
In socket communication, when the receiver calls the socket's close () method to close the socket, the underlying socket does not immediately release the local port if there is data sent to the socket on the network. Instead, it waits for a period of time to ensure that the data sent over the network is received before releasing the port. The socket receives these deferred data and does not do any processing.
The underlying socket continues to receive deferred data and does not immediately release the port because: ensure that the data is not received by other new processes that happen to be bound to the same port. When SO_RESUSEADDR is set to true, other processes on the same host can immediately reuse the port.
3) So_timeout:
When the data is read through the input stream of the socket, it waits if there is no data. The sample code for reading data from the input stream of the socket is as follows:
byte New byte [1024x768= socket.getinputstream (); In.read (buff);
If there is no data in the input stream, the In.read (buff) waits for the sender to send the data until the following conditions are met to end the wait.
A) The input stream has 1024 bytes of data, and the Read () method reads the bytes into the buff and returns the number of bytes read.
b) When approaching the end of the input stream, there is less than 1024 bytes of data at the end of the distance, and the Read method reads the data into the buff and returns the number of bytes read.
c) The end of the input stream has been read and returned-1
D) The connection has been disconnected, read will end waiting and throw IOException
f) If the wait time-out is set through the socket's Setsotimeout () method, the wait is exceeded and the sockettimeoutexception exception is thrown after the timeout period expires
The above five cases can be used to determine when the read-form is terminated, the code is as follows:
int len =-1; while (len = in.read (buff))! =-1) { //dosome process}
4) So_linger: This option is used to control the behavior of the socket when it is closed.
By default, when the close () method of the socket is executed, the method returns immediately, but the underlying socket does not actually close immediately, it delays for a period of time until all the remaining data is sent, and the socket is actually closed and disconnected.
If this option is set to True, the close () method that executes the socket returns immediately, and the underlying socket is immediately closed, and any remaining data that is not sent out is discarded.
Two, ServerSocket
ServerSocket has a constructor method: ServerSocket (int port, int backlog) throws IOException
The parameter backlog specifies the length of the client connection request queue. What is the customer connection request queue?
When ServerSocket is started (serversocket serversocket = new ServerSocket ("localhost", 8080), it will wait until the client connection arrives.
When the client creates a socket connection request through the socket socket = new socket ("localhost", 8080), if the ServerSocket connection request queue is not full, then the new socket ("localhost", 8080) immediately returns a socket object and places the request in the Client connection request queue. The task of managing client connection requests is done by the operating system, where the operating system places these connection requests in a queue.
only when ServerSocket calls the Accept () method does it mean that a socket connection is actually established. The server takes the connection request from the queue by ServerSocket's Accept method, establishes the connection, and also frees the queue for the queue to continue joining the new connection request. The socket constructor throws a Connectionexception exception if the connection request made by the client is rejected by the server.
ServerSocket options
So_timeout: Represents the time-out period waiting for a client connection. The default value is 0, which means that never times out.
When the server executes the Accept method of ServerSocket, if the connection request queue is empty, the server waits until the client connection is received before returning from the Accept method. If the timeout period is set, then the server waits longer than the timeout time, it will throw sockettimeoutexception
JAVA Socket Programming---sockets and serversocket