Java.net.ServerSocket parsing

Source: Internet
Author: User

Note: This article is from: Pinterest: Jianshu

Jijs
Links: http://www.jianshu.com/p/7c0722a8b66f
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

Structure
    1. Public ServerSocket ()


      Constructs a serversocket, but does not bind any ports, and therefore cannot accept any request connections. You can use the Bing () method to bind later.

    2. public serversocket (int port)


      Constructs a ServerSocket object from a port.
      The default TCP queue size is 50.
      The default is to listen for all local IP addresses (if there are multiple network adapters).

  1 TCP is divided into fully connected and semi-connected queues. Here is the full-connection queue   2 semi-connection queue: The first handshake, when the server receives a request from the client, the request connection is placed in the semi-connected queue.   3 full connection queue: After three handshake, the current connection information is stored in the full connection queue. Connections in the full-connection queue wait for SERVERSOCKET.ACCPT () processing.
    1. public ServerSocket (int port, int backlog)


      Constructs a ServerSocket object through a port and TCP queue size.
      The default is to listen for all local IP addresses (if there are multiple network adapters).

    2. public ServerSocket (int port, int backlog, inetaddress bindaddr)


      Constructs a ServerSocket object through a port, a TCP queue size, and a inetaddress.

A server may have more than one network card, multiple IP addresses, passed through this construct into a inetaddress, can only listen to the request connection from this network card.
For example: A computer has two network cards, one is an external network address, and the other is the intranet address. For security This serversocket only allows the request to listen to the intranet IP address, not the external network request.

Bind method

With the ServerSocket object constructed without a parameter, the Bing method is required to bind the operation to process the client's request.

  1publicvoidthrows IOException {  2     bind (endpoint, +);   3 }  4  5publicvoidintthrows IOException {  6 ...   7 }
    1. Bound by socketaddress, the default TCP queue size is 50.
    2. Two parameters are bound by socketaddress and TCP request queue size.
Accept method
  1public Socket accept ()

This method obtains a client socket request from the full-connection queue. This method is a blocking method. If a connection is not currently being requested, it will block until a client connection request is reached.

Get information about ServerSocket
    1. Public inetaddress getinetaddress ()
      Gets the local address, or randomly returns one if there are multiple IPs locally.
    2. public int Getlocalport ()
      Gets the bound port.

If the default port in the construction ServerSocket is passed to a 0, then a port is randomly generated, and this method is needed to obtain the port information.

    1. Public socketaddress getlocalsocketaddress ()
      Returns the local address and port.

The method of obtaining the local address from the source is found by calling the Getinetaddress () method. The get port is obtained through the Getlocalport () method.

Use ServerSocket to determine which ports are already occupied by the current system
1  Public Static voidMain (string[] args)throwsException {2      for(intPort=1; port<65535; port++) {3         Try{4ServerSocket s =NewServerSocket (port);5}Catch(IOException e) {6System.out.println ("ports already in use in the current system:"+port);7}8}9}

The results of the operation are as follows:

Java.net.ServerSocket parsing

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.