1.Socket has multiple construction methods, and most construction methods specify the host and port number of the connection at the time of construction. When the client's construction method is connected to the server, it may take a while for the connection to be established. By default, the socket construction method waits until the connection succeeds or an exception occurs. When the socket is constructed, it is affected by the transfer speed of the underlying network and may be in a waiting state for a long time. If you want to limit the wait time, you need an argument-free construction method, which can be done as follows:
Socket socket=new socket ();
Socketadress remoteaddr=new inetsocketaddress ("localhost", 800);
Socket.connect (remoteaddr,60000);//The timeout to wait for the connection to be established is 1 minutes, in milliseconds.
The above code represents the time that the socket waits for a connection of 1 minutes, and if it exceeds 1 minutes, it throws sockettimeoutexception.
Java Network Programming Learning Note (ii): Socket details