Create a serversocket object

Source: Internet
Author: User
Tags connection reset

The serversocket class constructor has four reloads. Their definitions are as follows:

public ServerSocket() throws IOExceptionpublic ServerSocket(int port) throws IOExceptionpublic ServerSocket(int port, int backlog) throws IOExceptionpublic ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException

The preceding constructor involves three parameters: Port, backlog, and bindaddr. Specifically, port is the port to be bound to the serversocket object, backlog is the length of the Request queue, and bindaddr is the IP address to be bound to the serversocket object.

1. Bind a port through the constructor

Binding a port through constructor is the most common method for creating a serversocket object. You can bind a port by using the following constructor:

public ServerSocket(int port) throws IOException

If the port specified by the port parameter has been bound, the constructor throws an ioexception. But the exception actually thrown is bindexception. From the inheritance relationship diagram of exception classes in Figure 4.2, we can see that all network-related exceptions are subclasses of the ioexception class. Therefore, ioexception is used to throw other exceptions for the serversocket constructor.

If the port value is 0, the system selects a random port number. However, the randomly selected port is of little significance, because the client needs to clearly know the port number of the server program when connecting to the server. You can use the tostring method of serversocket to output information related to the serversocket object. The following code inputs information related to the serversocket object.


In the above output result, ADDR is the IP address bound to the server. If no IP address is bound, this value is In this case, the serversocket object listens to all IP addresses of all network interfaces on the server. Port is always 0. Localport is the port bound to serversocket. If the port value is 0 (not the output port, it is the parameter port of the serversocket constructor), localport is a randomly selected port number.

1 ~ in the operating system ~ 1023 indicates the port number used by the system. The minimum value of the port number is 1 and the maximum value is 65535. In Windows, you can bind a program with a port number less than 1024, but in Linux/Unix, you must use root login to bind a port less than 1024. In the previous article, we used the socket class to determine which ports are opened on the local machine. In fact, the serversocket class can achieve the same purpose. The basic principle is to use serversocket to bind the local port. If a bindexception exception is thrown when a port is bound, it indicates that the port has been opened. Otherwise, the port has not been opened.

Package server; import *; public class scanport {public static void main (string [] ARGs) {If (ARGs. length = 0) return; int minport = 0, maxport = 0; string ports [] = ARGs [0]. split ("[-]"); minport = integer. parseint (ports [0]); maxport = (ports. length> 1 )? Integer. parseint (ports [1]): minport; For (INT Port = minport; port <= maxport; port ++) Try {serversocket = new serversocket (port); serversocket. close ();} catch (exception e) {system. err. println (E. getclass (); system. err. println ("Port" + port + "enabled! ");}}}

In the above Code, the information about the exception class thrown when the serversocket object is created is output. Scanport uses the command line parameter to pass the range of port numbers to be scanned into the program. The parameter format is minport-maxport. If you only enter one port number, the scanport program only scans this port number.


java server.ScanPort 1-1023

Running result:

Class bindexception port 80 is enabled! Class bindexception port 135 is enabled!

Ii. Set the length of the Request queue

When writing a server program, it generally processes multiple client requests simultaneously through multiple threads. That is to say, a thread is used to receive client requests. When a request is received (a socket object is obtained), a new thread is created and the client request is handed over to the new thread for processing. The thread that receives client requests continues to receive client requests. The implementation code of this process is as follows:

Serversocket = new serversocket (1234); // bind the port // code for processing other tasks while (true) {Socket socket = serversocket. accept (); // code waiting to receive client requests // code for processing other tasks new threadclass (socket ). start (); // create and run the thread that processes client requests}

The threadclass class in the code above is a subclass of the thread class. The constructor of this class has a socket-type parameter. You can pass the socket object into the threadclass object through the constructor, and process client requests in the run method of the threadclass object. This code looks like seamless on the surface. No matter how many client requests there are, as long as the server configuration is high enough, it can be processed. However, if you think carefully about the above Code, we may find some problems. If there is enough complex code in lines 2nd and 6th, the execution time is long, which means that the server program cannot respond to the client request in time.

Assume that the code for lines 2nd and 6th is thread. Sleep (3000), which will delay the program for 3 seconds. In these three seconds, the program will not execute the accept method. Therefore, this program only binds the port to 1234 and does not start to receive client requests. If a client sends a request to port 1234 at this time, in theory, the client should have a connection rejection error, but the client shows that the connection is successful. The reason is that the request queue to be discussed in this section is at work.

After a server socket object is bound to a port, the operating system will allocate a first-in-first-out queue for the port (the default queue length is generally 50 ), this queue is used to save unprocessed client requests, so it is called a request queue. The serversocket accept method reads unprocessed client requests from this queue. If the request queue is empty, accept is blocked. Each time a client sends a request to the server, the server first saves the client request in the Request queue, and then reads the accept from the request queue. This can also explain why the above Code can still receive a certain number of client requests without executing the accept method. If the number of client requests in the Request queue reaches the maximum capacity of the Request queue, the server will no longer be able to receive client requests. If the client sends another request to the server, the client will throw a socketexception.

The serversocket class has two constructor Methods: You can use the backlog parameter to reset the length of the Request queue. In the following situations, the maximum length of the Request queue is still limited by the operating system:

  • The backlog value is less than or equal to 0.
  • The value of backlog is greater than the maximum length of the Request queue defined by the operating system.
  • The backlog parameter is not set in the serversocket constructor.

The following code demonstrates some features of the Request queue. The length of the Request queue is passed in to setrequestqueue through the command line parameter.

Package server; import *; Class testrequestqueue {public static void main (string [] ARGs) throws exception {for (INT I = 0; I <10; I ++) {Socket socket = new socket ("localhost", 1234); socket. getoutputstream (). write (1); system. out. println ("created successfully" + String. valueof (I + 1) + "client connection! ") ;}} Public class setrequestqueue {public static void main (string [] ARGs) throws exception {If (ARGs. length = 0) return; int queuelength = integer. parseint (ARGs [0]); serversocket = new serversocket (1234, queuelength); system. out. println ("port (1234) has been bound. Press enter to start processing client requests! "); System. in. read (); int n = 0; while (true) {system. out. println ("<prepare to receive" + (++ n) + "client requests! "); Socket socket = serversocket. accept (); system. out. println ("processing" + N + "client requests"); thread. sleep (1, 3000); system. out. println (the "+ N +" client request has been processed!> ");}}}

Test (follow these steps ):

1. Execute the following command (do not press the Enter key after executing this command ):

java server.SetRequestQueue 2

Running result:

Port (1234) has been bound. Please press enter to start processing client requests!

2. Execute the following command:

java server.TestRequestQueue

Running result:

1st client connections have been created successfully! 2nd client connections have been created successfully! Exception in thread "Main" socketexception: Connection reset by peer: Socket write error at socketoutputstream. socketwrite0 (native method) at socketoutputstream. socketwrite (socketoutputstream. java: 92) at socketoutputstream. write (socketoutputstream. java: 115) at server. testrequestqueue. main (setrequestqueue. java: 12)

3. Press the Enter key to continue executing setrequestqueue. The running result is as follows:

Port (1234) has been bound. Please press enter to start processing client requests! <Prepare to receive 1st client requests! Processing of 1st client requests 1st client requests have been processed!> <Prepare to receive 2nd client requests! Processing of 2nd client requests 2nd client requests have been processed!> <Prepare to receive 3rd client requests!

The running result of step 2 shows that when testrequestqueue creates two socket connections, the request queue of the server is full, and the server cannot continue execution for the time being (because of system. in. the execution of the program is suspended for the reason of read (), waiting for user input ). Therefore, the server can no longer receive client requests. At this time, testrequestqueue throws a socketexception. The two socket connections that have been successfully created in testrequestqueue have been saved in the Request queue of the server. Press any key to continue executing setrequestqueue. The accept method reads the two client request queues from the request queue in sequence. The running result of step 3 shows that after the server processes the two requests (one <…> Contains a processing process), the request queue is empty, then the accept processing blocking status, waiting to receive the third client request. If you run testrequestqueue again, how many client requests will the server receive? If we set the length of the Request queue to a value greater than 10, what is the running result of testrequestqueue? You can perform these experiments on your own to see if they are consistent with what you think.

3. bind an IP address

On a computer with multiple network interfaces or IP addresses, you can use the following constructor to bind the server to a specific IP Address:

public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException

The bindaddr parameter is the IP address to bind. If you bind the server to an IP address, only clients that can access this IP address can connect to the server. If a machine has two NICs, one is connected to the Intranet and the other is connected to the Internet. If you use Java to implement an email server and only want Intranet users to use it. You can use this constructor to bind the serversocket object to the IP address connected to the Intranet. In this way, the Internet cannot access the email server. You can use the following code to bind
Fixed IP Address:

ServerSocket serverSocket = newServerSocket(1234, 0, InetAddress.getByName(""));

The above code binds the IP address to Therefore, the server program can only use the network interface bound to this IP address for communication.

Iv. Use of default constructor

In addition to using the serversocket constructor to bind a port, you can also use the bind method of serversocket to complete the work of the constructor. To use the bind method, you must use the default Construction Method of the serversocket class (the construction method without parameters) to create the serversocket object. The bind method has two overload forms. Their definitions are as follows:

public void bind(SocketAddress endpoint) throws IOExceptionpublic void bind(SocketAddress endpoint, int backlog) throws IOException 

The bind method can not only bind a port, but also set the length of the Request queue and the bound IP address. The bind method is used to set some options of the serversocket class after the serversocket object is created. These options must be set before the port is bound. Once the port is bound, setting these options will no longer work. The following code demonstrates the use of the BIND method and how to set the serversocket class options.

ServerSocket serverSocket1 = new ServerSocket();serverSocket1.setReuseAddress(true);serverSocket1.bind(new InetSocketAddress(1234));ServerSocket serverSocket2 = new ServerSocket();serverSocket2.setReuseAddress(true);serverSocket2.bind(new InetSocketAddress("", 1234));ServerSocket serverSocket3 = new ServerSocket();serverSocket3.setReuseAddress(true);serverSocket3.bind(new InetSocketAddress("", 1234), 30);    

The so_reuseaddr option is set in the above Code (this option will be discussed in detail in later articles ). If the following code is used, this option does not work.

ServerSocket serverSocket3 = new ServerSocket(1234);serverSocket3.setReuseAddress(true);

The IP address and port are bound to row 6th. The combination cannot be obtained by using the constructor (backlog parameters must be set to bind IP addresses). Therefore, the bind method is more flexible than the constructor.

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: 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.