Java.net.Socket Usage Explanation

Source: Internet
Author: User

First, socket construction method

Socket ()
Socket (inetaddress address, int port) throws Unknowhostexception, IOException
Socket (inetaddress address, int port, inetaddress localaddr, int localport) throws IOException
Socket (String host, int port) throws Unknowhostexception, IOException
Socket (String host, int port, inetaddress localaddr, int localport) throws IOException

In addition to the first constructor without parameters, other construction methods attempt to establish a connection to the server, and if the connection succeeds, the socket object is returned; If the connection fails for some reason, IOException is thrown.

Second, socket common method getinetaddress ();          The IP address of the remote server Getport (); Remote server Port getlocaladdress () local client IP address getlocalport () port of the local client getInputStream(); Gets the input stream and returns an instance of the InputStream object. Getoutstream(); Gets the output stream and returns an instance of the OutputStream object. It is worth noting that in these methods, the most important is getinputstream () and Getoutputstream (). Note: Where both the getInputStream and Getoutputstream methods produce a ioexception, it must be captured because the stream object they return is usually used by another stream object. Third, socket status isclosed ();      If the connection is closed, returns true if closed, otherwise returns Falseisconnect ();            Returns True if ever connected, otherwise returns Falseisbound (); Returns true if the socket is already bound to a local port, otherwise false if you want to confirm that the state of the socket is in a connection, the following statement is a good way to judge.
boolean isconnection=socket.isconnected () &&!socket.isclosed ();   // Determine whether the connection is currently in
half-closed socketMany times, we don't know how long it will be to end up in the input stream we get. Here are some of the more common approaches:
    • Custom identifiers (such as the following example, when a "bye" string is received, the socket is closed)
    • Tells the read length (some custom protocol, fixed the first few bytes to indicate the length of the Read)
    • Finish reading all the data
    • Closes the input and output stream when the socket closes when it calls close
Iv. Socket parameter Options
    • Tcp_nodelay: means sending data immediately

    • SO_RESUSEADDR: Indicates whether the local address that the socket is bound to is allowed to be reused

    • So_timeout: Indicates the wait time-out period when data is received. The unit is in milliseconds, and the default value is 0, which means waiting forever.

    • So_linger: Indicates whether to close the underlying socket immediately when the Close method of the socket is executed. This unit is seconds.

    • SO_SNFBUF: Buffer size for sending data

    • SO_RCVBUF: Buffer size for receiving data

    • So_keeplive: Indicates whether to automatically close a socket that is idle for a long time

    • Oobinline: Indicates whether to support sending a byte of TCP emergency data

    • Backlog: The maximum queue length for the input connection indication (Request for connection) is set to the backlog parameter. If the queue receives a connection indication when it is full, the connection is rejected.

Remarks: Explanation of the specific parameter options next analysis

Reference Source:

http://my.oschina.net/u/725800/blog/303634

Http://www.cnblogs.com/rond/p/3565113.html

  

Java.net.Socket Usage Explanation

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.