TCP/IP Client Sockets

TCP/IP sockets are used to implement reliable, bidirectional, persistent, point-to-point, stream-based connections between hosts on the Internet. A socket can be used to connect Java’s I/O system to other programs that may reside either on the local machine or on any other machine on the Internet.

There are two kinds of TCP sockets in Java. One is for servers, and the other is for clients. The ServerSocket class is designed to be a “listener,” which waits for clients to connect before doing anything. Thus, ServerSocket is for servers. The Socket class is for clients. It is designed to connect to server sockets and initiate protocol exchanges. Because client sockets are the most commonly used by Java applications, they are examined here.

The creation of a Socket object implicitly establishes a connection between the client and server. There are no methods or constructors that explicitly expose the details of establishing that connection. Here are two constructors used to create client sockets:

Socket(String hostName, int port)
throws UnknownHostException,
Creates a socket connected to the named host
and port.
Socket(InetAddress ipAddress, int port)
throws IOException
Creates a socket using a preexisting
InetAddress object and a port
TCP/IP Client Sockets

Socket defines several instance methods. For example, a Socket can be examined at any time for the address and port information associated with it, by use of the following methods:

InetAddress getInetAddress()Returns the InetAddress associated with the Socket
object. It returns null if the socket is not connected.
int getPort( )Returns the remote port to which the invoking Socket
object is connected. It returns 0 if the socket is not
int getLocalPort( )Returns the local port to which the invoking Socket
object is bound. It returns –1 if the socket is not bound.
TCP/IP Client Sockets

You can gain access to the input and output streams associated with a Socket by use of the getInputStream( ) and getOuptutStream( ) methods, as shown here. Each can throw an IOException if the socket has been invalidated by a loss of connection.

InputStream getInputStream( )
throws IOException
Returns the InputStream associated with the
invoking socket.
OutputStream getOutputStream( )
throws IOException
Returns the OutputStream associated with the
invoking socket
TCP/IP Client Sockets

Several other methods are available, including connect( ), which allows you to specify a new connection; isConnected( ), which returns true if the socket is connected to a server; isBound( ), which returns true if the socket is bound to an address; and isClosed( ), which returns true if the socket is closed

The following program provides a simple Socket example. It opens a connection to a “whois” port (port 43) on the InterNIC server, sends the command-line argument down the socket, and then prints the data that is returned. InterNIC will try to look up the argument as a registered Internet domain name, and then send back the IP address and contact information for that site

Demonstrate Sockets

class Whois {
public static void main(String args[]) throws Exception {
int c;
// Create a socket connected to, port 43.
Socket s = new Socket("", 43);
// Obtain input and output streams.
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
// Construct a request string
String str = (args.length == 0 ? "" : args[0]) + "\n";
// Convert to bytes.
byte buf[] = str.getBytes();
// Send request.
// Read and display response.
while ((c = != -1) {
System.out.print((char) c);

Leave a Comment