Java Network Programming 4-serversocket

Source: Internet
Author: User

Port number is 0 system randomly assigned ports, connection queue system generally default 50, refers to more than the system is the largest system-based
//If the client's connection exceeds the connection queue, it will be rejected by the host
ServerSocket serversocket=new ServerSocket (3344,20);


//refers to which IP is listening on this machine
ServerSocket serversocket=new ServerSocket (3344, Inetaddress.getbyname ("localhost"));


ServerSocket serversocket=new ServerSocket ();
//ETC Client connection time-out, unit MS, default 0 does not time out
Serversocket.setsotimeout (1000);
//Bind a port, which is used primarily for non-parameter objects, set the ServerSocket property before the Bind method
Serversocket.bind (New Inetsocketaddress (3344));
//Blocking program, waiting for client connection, this socket close does not shut down ServerSocket
Socket socket= serversocket.accept ();
try {
if (serversocket!=null) {
//serversocket Close the associated socket after closing
Serversocket.close ();
}
} catch (Exception e) {
}


//Gets the bound IP, and if there are multiple NICs, randomly returns one of the
System.out.println (Serversocket.getinetaddress ());


//Determine if ServerSocket has bound a port
System.out.println (Serversocket.isbound ());
//Determine if the ServerSocket is closed and returns false if no ports are bound
System.out.println (serversocket.isclosed ());
//correctly determine if the ServerSocket is open
System.out.println (Serversocket.isbound () &&!serversocket.isclosed ());

Java Network Programming 4-serversocket

Related Article

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.