("Connecting to server on port " + serverPort) InetAddress host = InetAddress.getByName("localhost") Here, we'll use a very simple client and server example to show the use of TCP socket in Java. The class library provides classes Socket and ServerSocket for message passing for TCP/IP. Once the server accepts the connection request, messages can be passed in both directions across the channel. The client issues a request to the server to make a connection between the two sockets.Messages can be received from any client, as far as the client machine knows the server's address and the port number which are bound to the server's socket. The server's socket also should specify a local I/O port to receive messages.The client's socket should specify the address of the destination machine as well as the port number that is expected to be bound to the server thread's socket. The client thread's socket specifies a local I/O port for sending messages.What do we call those endpoint objects? They are sockets.
The OS manages the hardware and software that is used to transport messages across the channel (between the endpoints). When two threads want to exchange message over the channel, each thread create an endpoint object that represents its end of the network channel. Because a program on one machine cannot directly reference object in a program running on other machines, we need to from a communication network with help from the OS. In this chapter, we are working with programs which run on different machines.