[Question] Question about "java.net. ServerSocket. accept ()"

Source: Internet
Author: User
Question:

Java.net. ServerSocket. accept () will listen to the specified port for some requests and create a socket.

But some question bewilder me.

There is a code:

ServerSocket serverSocket = new ServerSocket(listenPort);  // Create a server socket to listen to the sepcified port.      while(true) {  // Keep listening without break.        Socket incomingConnection = serverSocket.accept();        handleConnection(incomingConnection);  // Handle the request with multi thread.      }    } catch (BindException e) {        System.out.println("Unable to bind to port " + listenPort);    } catch (IOException e) {        System.out.println("Unable to instantiate a ServerSocket on port: " + listenPort);    }

The while {} block make the ServerSocket keep listening every time. If there is a request, the accept () method will create a socket and will not be block until a connection is made.

But, if there are more than one request at the same time? What will the accept () method process? Will it create sockets for each request at the same time?

Answer:> But, if there are more than one request at the same
> Time? What will the accept () method process?

Two network packets don't arrive at the same time on the network card, so there can't be exactly simultaneous connections. (Well, OK, unless you have two network cards and two CPUs ...)

If there are two requests very close to each other, accept () returns one, then the other on the next call.

Accept () will queue requests upto some limit-google for "TCP connection backlog ". there is also some backlog stuff in ServerSocket, look at the API. usually you don't need to worry about it, the default backlog is fine.

And, as PL says, immediately after accept () returns, fire off a new thread to handle the request. that way your accepting thread can get right back into calling accept (). then the backlog won't overflow-your CPU is much faster doing accept () than the network card is at creating ing new connections.

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.