Java.net.ServerSocket parsing

Source: Internet
Author: User

Note: This article is from: Pinterest: Jianshu

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

Socket construction
    1. Public Socket ()


      Constructs a socket, because no target host and port are specified, so connections are not made over the network.

    2. Public Socket (proxy proxy)


      Build an disconnected socket from an agent.

    3. Public Socket (String host, int port)


      Build a socket from a host and port. The target host is connected when the socket is constructed, and a ioexception or Unknownhostexception exception is thrown if the target host is not connected.

    4. Public Socket (inetaddress address, int port)


      A socket is built via a inetaddress and port, and the target host is connected when the configuration is made, with 3.

    5. Public Socket (String host, int port, inetaddress localaddr, int localport)


      Through a remote host and port to connect to, and specify which IP and port to connect from locally.

    6. Public Socket (inetaddress address, int port, inetaddress localaddr, int localport)


      Through a remote host and port to connect to, and specify which IP and port to connect from locally. The same 5.

Get socket-related information get remote host related information
  1public socketaddress getremotesocketaddress ()  2public inetaddress  Getinetaddress ()  3publicint getport ()
Get local related information
  1public socketaddress getlocalsocketaddress ()  2public  InetAddress getlocaladdress ()  3publicint getlocalport ()

The above 6 methods get information about local and remote host ports. Returns NULL if the socket is not connected and returns information on the local and remote host ports that have been connected if the socket is closed.

Connect method
  1publicvoid Connect (socketaddress endpoint)  2public  voidint timeout)

Connet provides two methods, one is to pass in a socketaddress to connect the destination address
The other is to connect to the destination address through a socketaddress and timeout wait time.

code example:

  1new Socket ();   2new inetsocketaddress ("www.baidu.com", 80);   3 socket.connect (SOCKETADDR);   4//socket.connect (socketaddr,);   5

The code uses the empty constructor of the socket to construct the socket object. An empty construct does not connect to the target host (because the destination address is not set), the target host needs to be connected using the Connet method.

Proxy Server

1 //construct proxy server address2SocketAddress SA =NewInetsocketaddress ("192.168.10.130", 808);3 //Construct socket proxy4Proxy proxy =NewProxy (Proxy.Type.SOCKS, SA);5 //Create socket with proxy6Socket socket =NewSocket (proxy);7 //Construct Destination address8SocketAddress socketaddr =NewInetsocketaddress ("www.baidu.com", 80);9 //socket using a proxy to connect to the destination addressTenSocket.connect (SOCKETADDR); One 

Use the public socket construction method to construct a socket using proxy.
Subsequent network operations that use the socket will be connected through a proxy server.

Reading, writing data

Use the following two places to interact with the remote host to read data and write data.

  1public InputStream getinputstream ()  2public outputstream  Getoutputstream ()

Through these two methods we can find that the use of the socket to exchange operations, in fact, is the operation of convection.

The following is an example of swapping remote hosts using the getInputStream and Getoutputstream methods.

  1new Socket ("127.0.0.1", 9000);   2 outputstream os = Socket.getoutputstream ();   3//via outputstream to remote server write as Data   4 ...   5 InputStream is = Socket.getinputstream ();   6//Read the response data through the InputStream stream.    7 ...   8  9
Half-closed connection

If you want to close the input or output of a socket, you can use two methods.

  1publicvoid shutdowninput ()  2publicvoid Shutdownoutput ()

When you call Shutdowninput (), you are not allowed to read data from the socket again.
When the Shutdownoutput () method is called, it tells the stream that it has entered completed and does not allow the input again. When the other side reads the stream, it receives a stream end flag (1 is returned).

However, closing the input or output will not close the socket because they will not release the local port, and you will need to call the close () method of the socket to close the socket.

The following two methods determine whether the socket input or output stream is closed.

  1publicboolean isinputshutdown ()  2public  Boolean Isoutputshutdown ()

If the socket is not connected (a socket constructed through an empty construction method), it also returns False (not closed).

Determine if the socket is closed
  1//whether to connect to destination address   2publicboolean isconnected ()  3 //Whether the socket has been closed   4publicboolean isClosed ()

The IsConnected method does not return true for the connection state, but returns true if the destination address is connected, even if the socket that is already closed returns true.
The IsClosed method is to determine if the socket has closed the socket by calling the close () method.
If the socket is built using an empty construction method without connecting to the target host, the Close method is not called, and the method returns True.

So it is necessary to determine whether the socket is connected to the destination address.

  1//open connection, but not yet closed connection   2if(socket.isconnected () &&! Socket.isclosed ()) {  3      System.out.println (" Connection Status ");   4 }else{  5      System.out.println ("The connection is not open or the connection has been closed ");   6 }  7  8

Java.net.ServerSocket parsing

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.