After the data interaction between the client and the server is complete, you usually need to disable the network connection. For the server, you need to disable socket and serversocket.
After the socket is closed, the client does not immediately notice that its socket has been closed. That is to say, after the socket on the server is closed, the isclosed and isconnected methods of the client socket still obtain false and true respectively. However, a socketexception is thrown when you operate the input/output stream of a closed socket.
After the serversocket of the server is closed, the port bound to the serversocket object is released. The client cannot connect to the server.Program. The followingCodeDemonstrate how the client responds after the socket is closed on the server.
Package server;
Import java.net .*;
Class Client
{
Public static void main (string [] ARGs) throws exception
{
Socket socket = new socket ("127.0.0.1", 1234 );
Thread. Sleep (1000 );
// Socket. getoutputstream (). Write (1 );
System. Out. println ("Read () =" + socket. getinputstream (). Read ());
System. Out. println ("isconnected () =" + socket. isconnected ());
System. Out. println ("isclosed () =" + socket. isclosed ());
}
}
Public class closesocket
{
Public static void main (string [] ARGs) throws exception
{
Serversocket = new serversocket (1234 );
While (true)
{
Socket socket = serversocket. Accept ();
Socket. Close ();
}
}
}
Test
Run the following command:
Java Server. closesocket
Java Server. Client running result
Read () =-1
Isconnected () = true
Isclosed () = false
The preceding running result shows that the client of the routine does not throw a socketexception. The read method in row 012 returns-1. If you remove socket. Close, the client's read method will be blocked. This is because after Java finds that data cannot be obtained from the socket on the server end, it returns-1 through the read method. If you remove the comments from the 011 lines, the client will throw a socketexception. You can try it and change the socket. Close line to serversocket. Close, and the client will throw a connection exception:
Exception in thread "Main" java.net. socketexception: Connection Reset
At java.net. socketinputstream. Read (fig. Java: 168)
At java.net. socketinputstream. Read (fig. Java: 182)
At chapter5.client. Main (closesocket. Java: 12)
It is not necessary to explicitly call the close method to close serversocket. When the program exits, it will automatically close serversocket. However, close serversocket can bind other serversocket objects to this port. You can use the isclosed and isbound methods of the serversocket class to determine whether the serversocket is active, as shown in the following code:
Serversocket = new serversocket (1234 );
If (serversocket. isbound () = true & serversocket. isclosed () = false)
System. Out. println ("serversocket is active! ");
Else
System. Out. println ("serversocket is inactive! ");
The "inactive" State shown in the code above may be that the serversocket object has been closed, or that the serversocket object was created using the default constructor of the serversocket class, And the bind method is not called to bind the port. It is important to note that the isbound method returns true, which does not mean that the serversocket object is active. Calling the close method does not set the binding status to false. This is similar to the isconnected method of the socket class.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/nokiaguy/archive/2009/08/04/4682938.aspx