Discussion on the determination of TCP connection state

Source: Internet
Author: User

Although both TCP and UDP use the same network layer (IP), TCP provides a completely different service to the application layer than UDP.
TCP provides a reliable, connection-oriented, byte-throttling service. Introduction to TCP Protocols

Connection-oriented means that two applications that use TCP (typically a client and a server) must establish a TCP connection before exchanging data with each other. This process is similar to the telephone, the first dial ringing, waiting for each other to pick up the machine said "Hello", and then explain who.

TCP provides a reliable connection, and TCP realizes reliable transmission based on the IP protocol of the unreliable network layer by means of timeout, acknowledgement, inspection and duplication. long connection of TCP

In the TCP protocol inside the description, once the connection is established, if not actively send the fin signal, the channel will remain. And, it takes four more handshakes to completely dismantle the current TCP channel.

Everything above is built in the ideal case of TCP protocol. The actual situation is often very complex, such as the abrupt interruption of the network, equipment suddenly power off, and so on, these will lead to the abnormal TCP interrupt, so in the actual application, it is important to determine the current TCP connection status. Determining the connection state through the socket API

In the actual turn, the implementation of TCP connection is generally implemented by the way of socket. I don't make too many introductions about this knowledge. We can refer to the relevant information. A random search is a lot of information.

The Java socket class does not provide a direct interface for determining the current connection state.
1, isconected (), mainly used to determine whether you have successfully connected, if you have recently successfully connected, then return True. The current connection may have been interrupted for some reason.
2, isclosed (), this interface can only determine whether you closed the socket, as long as you do not close the socket, will always return false.

Neither of these methods can determine the status of the current TCP connection.

So how do we determine the current channel state? In fact, there is a simple way to send some data, if you can send success will indicate that the current connection is not a problem. This is the most direct, but also one of the most common methods we use.
Regardless of the reason, the current channel interrupt, the socket write operation will throw a ioexeption, the use of read the same way to achieve the same effect.

This allows the TCP channel to be rebuilt within the caught exception.
The act of not giving code is bullying. The client-side code is given below.

/** * Created by kiplening 3/9/2016.
        * * Public class Client {public static void main (string[] args) {socket socket = NULL;
        try {socket = new socket ("192.168.1.129", 8000);
        catch (IOException e) {e.printstacktrace ();
        } outputstream OS = null;
        try {os = Socket.getoutputstream ();
        catch (IOException e) {e.printstacktrace (); } while (true) {BufferedReader reader = new BufferedReader (New InputStreamReader (

            system.in));
            String line = null;
            try {line = Reader.readline ();
            catch (IOException e) {e.printstacktrace ();
            try {os.write (line.getbytes ()); The catch (IOException e) {System.out.println ("Connection interrupted.") Try to connect ...
                ");

          TODO: Re-connect Operation//e.printstacktrace ();  }
        }   
    }
} 
Code Test Results

When the two ends are working correctly, the input content can be sent on the client. Once the server breaks the network, or shuts down the service program. At this point, as long as not to send content, the entire program does not have any exception, once sent content, it will throw ioexcption. Prompts you to re-establish the connection.

As for the test of the isconnected () and IsClosed () method mentioned above, I will not do it, we can verify it by ourselves.

StackOverflow

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.