JAVA TCP/IP Socket Implementation (4)

Client Actions

Close the socket

  1. Instantiate a Socket object
  2. Communicate with server
    1. Send data/requests
    2. Receive data/replys
  3. Close the socket
  • Close the socket when finished communicating
  • Both client and server must close their sockets to completely tear down the connection
  • Server must also close down the ServerSocket when no more client connections are expected
try	{
	sock.close();
}
catch(IOException ioe) {
	System.out.println("Close error: " + ioe.getMessage());
}

// Again, close() needs to be called on both sides of the connection, and the server should 
// also be sure to close() the ServerSocket when it no longer wishes to accept client
// connections.