Java Network Programming Learning Note (ii): Socket details

Source: Internet
Author: User

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

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.