Write using Java SocketProgramUsually encounter several exceptions: bindexception, unknownhostexception, connectexception, sockettimeoutexception.
Bindexception
It occurs in the BIND () Operation of the socket or serversocket. If it cannot be bound to the IP address or port specified by the local machine, this exception is thrown.
There are two scenarios:
- The specified IP address is not on the host.
A host can contain N NICs and multiple IP addresses. When binding, you must specify the IP address to which the NIC is bound. If the specified IP address is not on the current host, an exception is thrown.
- The specified port is in use.
Unknownhostexception
If the specified host name or IP address cannot be identified during connect, this exception is thrown.
Connectexception
There are two scenarios in connect:
- The server does not listen to the specified port
- The backlog queue specified by the server socket is full.
The exception information is the same in both cases:
Java.net. connectexception: Connection refused: connectat java.net. plainsocketimpl. socketconnect (native method) at java.net. plainsocketimpl. doconnect (plainsocketimpl. java: 351) at java.net. plainsocketimpl. connecttoaddress (plainsocketimpl. java: 213) at java.net. plainsocketimpl. connect (plainsocketimpl. java: 200) at java.net. sockssocketimpl. connect (sockssocketimpl. java: 366) at java.net. socket. connect (socket. java: 529) at java.net. socket. connect (socket. java: 478)
SockettimeoutexceptionThe connect method can specify the timeout time. If no connection is successful between timeouts, this exception is thrown.