Java Socket exception

Source: Internet
Author: User

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.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.