All About Sockets
All About Sockets
All About Sockets
What Is a Socket?
This page contains a small example that illustrates how a client program can read from
and write to a socket.
The previous page showed an example of how to write a client program that interacts
with an existing server via a Socket object. This page shows you how to write a program
that implements the other side of the connection--a server program.
What Is a Socket?
Normally, a server runs on a specific computer and has a socket that is bound to a
specific port number. The server just waits, listening to the socket for a client to make a
connection request.
On the client-side: The client knows the hostname of the machine on which the server is
running and the port number on which the server is listening. To make a connection
request, the client tries to rendezvous with the server on the server's machine and port.
The client also needs to identify itself to the server so it binds to a local port number that
it will use during this connection. This is usually assigned by the system.
If everything goes well, the server accepts the connection. Upon acceptance, the server
gets a new socket bound to the same local port and also has its remote endpoint set to the
address and port of the client. It needs a new socket so that it can continue to listen to the
original socket for connection requests while tending to the needs of the connected client.
On the client side, if the connection is accepted, a socket is successfully created and the
client can use the socket to communicate with the server.
The client and server can now communicate by writing to or reading from their sockets.
The java.net package in the Java platform provides a class, Socket, that implements
one side of a two-way connection between your Java program and another program on
the network. The Socket class sits on top of a platform-dependent implementation,
hiding the details of any particular system from your Java program. By using the
java.net.Socket class instead of relying on native code, your Java programs can
communicate over the network in a platform-independent fashion.
If you are trying to connect to the Web, the URL class and related classes
(URLConnection, URLEncoder) are probably more appropriate than the socket classes. In
fact, URLs are a relatively high-level connection to the Web and use sockets as part of
the underlying implementation.
The example program implements a client, EchoClient, that connects to the Echo server.
The Echo server simply receives data from its client and echoes it back. The Echo server
is a well-known service that clients can rendezvous with on port 7.
EchoClient creates a socket thereby getting a connection to the Echo server. It reads
input from the user on the standard input stream, and then forwards that text to the Echo
server by writing the text to the socket. The server echoes the input back through the
socket to the client. The client program reads and displays the data passed back to it from
the server:
import java.io.*;
import java.net.*;
try {
echoSocket = new Socket("taranis", 7);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: taranis.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to: taranis.");
System.exit(1);
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}
Note that EchoClient both writes to and reads from its socket, thereby sending data to
and receiving data from the Echo server.
Let's walk through the program and investigate the interesting parts. The three statements
in the try block of the main method are critical. These lines establish the socket
connection between the client and the server and open a PrintWriter and a
BufferedReader on the socket:
The second statement gets the socket's output stream and opens a PrintWriter on it.
Similarly, the third statement gets the socket's input stream and opens a BufferedReader
on it. The example uses readers and writers so that it can write Unicode characters over
the socket.
To send data through the socket to the server, EchoClient simply needs to write to the
PrintWriter. To get the server's response, EchoClient reads from the
BufferedReader. The rest of the program achieves this. If you are not yet familiar with
the Java platform's I/O classes, you may wish to read Basic I/O.
The next interesting part of the program is the while loop. The loop reads a line at a time
from the standard input stream and immediately sends it to the server by writing it to the
PrintWriter connected to the socket:
String userInput;
out.close();
in.close();
stdIn.close();
echoSocket.close();
These lines of code fall into the category of housekeeping. A well-behaved program
always cleans up after itself, and this program is well-behaved. These statements close
the readers and writers connected to the socket and to the standard input stream, and close
the socket connection to the server. The order here is important. You should close any
streams connected to a socket before you close the socket itself.
This client program is straightforward and simple because the Echo server implements a
simple protocol. The client sends text to the server, and the server echoes it back. When
your client programs are talking to a more complicated server such as an HTTP server,
your client program will also be more complicated. However, the basics are much the
same as they are in this program:
1. Open a socket.
2. Open an input stream and output stream to the socket.
3. Read from and write to the stream according to the server's protocol.
4. Close the streams.
5. Close the socket.
Only step 3 differs from client to client, depending on the server. The other steps remain
largely the same.
Writing the Server Side of a Socket
This section shows you how to write a server and the client that goes with it. The server
in the client/server pair serves up Knock Knock jokes. Knock Knock jokes are favored by
children and are usually vehicles for bad puns. They go like this:
The example consists of two independently running Java programs: the client program
and the server program. The client program is implemented by a single class,
KnockKnockClient, and is very similar to the EchoClient example from the previous
section. The server program is implemented by two classes: KnockKnockServer and
KnockKnockProtocol, KnockKnockServer contains the main method for the server
program and performs the work of listening to the port, establishing connections, and
reading from and writing to the socket. KnockKnockProtocol serves up the jokes. It
keeps track of the current joke, the current state (sent knock knock, sent clue, and so on),
and returns the various text pieces of the joke depending on the current state. This object
implements the protocol-the language that the client and server have agreed to use to
communicate.
The following section looks in detail at each class in both the client and the server and
then shows you how to run them.
This section walks through the code that implements the Knock Knock server program.
Here is the complete source for the KnockKnockServer class.
The server program begins by creating a new ServerSocket object to listen on a specific
port (see the statement in bold in the following code segment). When writing a server,
choose a port that is not already dedicated to some other service. KnockKnockServer
listens on port 4444 because 4 happens to be my favorite number and port 4444 is not
being used for anything else in my environment:
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.out.println("Could not listen on port: 4444");
System.exit(-1);
}
ServerSocket is a java.net class that provides a system-independent implementation
of the server side of a client/server socket connection. The constructor for ServerSocket
throws an exception if it can't listen on the specified port (for example, the port is already
being used). In this case, the KnockKnockServer has no choice but to exit.
If the server successfully binds to its port, then the ServerSocket object is successfully
created and the server continues to the next step--accepting a connection from a client
(shown in bold):
After the server successfully establishes a connection with a client, it communicates with
the client using this code:
Step 1 is already familiar. Step 2 is shown in bold and is worth a few comments. The bold
statements in the code segment above initiate the conversation with the client. The code
creates a KnockKnockProtocol object-the object that keeps track of the current joke, the
current state within the joke, and so on.
The server initiated the conversation with a "Knock! Knock!" so afterwards the server
must wait for the client to say "Who's there?" As a result, the while loop iterates on a read
from the input stream. The readLine method waits until the client responds by writing
something to its output stream (the server's input stream). When the client responds, the
server passes the client's response to the KnockKnockProtocol object and asks the
KnockKnockProtocol object for a suitable reply. The server immediately sends the reply
to the client via the output stream connected to the socket, using a call to println. If the
server's response generated from the KnockKnockServer object is "Bye." this indicates
that the client doesn't want any more jokes and the loop quits.
The KnockKnockServer class is a well-behaved server, so the last several lines of this
section of KnockKnockServer clean up by closing all of the input and output streams, the
client socket, and the server socket:
out.close();
in.close();
clientSocket.close();
serverSocket.close();
The KnockKnockProtocol class implements the protocol that the client and server use to
communicate. This class keeps track of where the client and the server are in their
conversation and serves up the server's response to the client's statements. The
KnockKnockServer object contains the text of all the jokes and makes sure that the client
gives the proper response to the server's statements. It wouldn't do to have the client say
"Dexter who?" when the server says "Knock! Knock!"
All client/server pairs must have some protocol by which they speak to each other;
otherwise, the data that passes back and forth would be meaningless. The protocol that
your own clients and servers use depends entirely on the communication required by
them to accomplish the task.
The KnockKnockClient class implements the client program that speaks to the
KnockKnockServer. KnockKnockClient is based on the EchoClient program in the
previous section, Reading from and Writing to a Socket and should be somewhat familiar
to you. But we'll go over the program anyway and look at what's happening in the client
in the context of what's going on in the server.
When you start the client program, the server should already be running and listening to
the port, waiting for a client to request a connection. So, the first thing the client program
does is to open a socket that is connected to the server running on the hostname and port
specified:
The KnockKnockClient program also specifies the port number 4444 when creating its
socket. This is a remote port number--the number of a port on the server machine--and is
the port to which KnockKnockServer is listening. The client's socket is bound to any
available local port--a port on the client machine. Remember that the server gets a new
socket as well. That socket is bound to local port number 4444 on its machine. The
server's socket and the client's socket are connected.
Next comes the while loop that implements the communication between the client and
the server. The server speaks first, so the client must listen first. The client does this by
reading from the input stream attached to the socket. If the server does speak, it says
"Bye." and the client exits the loop. Otherwise, the client displays the text to the standard
output and then reads the response from the user, who types into the standard input. After
the user types a carriage return, the client sends the text to the server through the output
stream attached to the socket.
In the interest of good housekeeping, the client closes its input and output streams and the
socket:
out.close();
in.close();
stdIn.close();
kkSocket.close();
Running the Programs
You must start the server program first. To do this, run the server program using the Java
interpreter, just as you would any other Java application. Remember to run the server on
the machine that the client program specifies when it creates the socket.
Next, run the client program. Note that you can run the client on any machine on your
network; it does not have to run on the same machine as the server.
If you are too quick, you might start the client before the server has a chance to initialize
itself and begin listening on the port. If this happens, you will see a stack trace from the
client. If this happens, just restart the client.
If you forget to change the host name in the source code for the KnockKnockClient
program, you will see the following error message:
If you try to start a second client while the first client is connected to the server, the
second client just hangs. The next section, Supporting Multiple Clients, talks about
supporting multiple clients.
When you successfully get a connection between the client and server, you will see the
following text displayed on your screen:
If at any point you make a typing mistake, the KnockKnockServer object catches it and
the server responds with a message similar to this:
To keep the KnockKnockServer example simple, we designed it to listen for and handle a
single connection request. However, multiple client requests can come into the same port
and, consequently, into the same ServerSocket. Client connection requests are queued at
the port, so the server must accept the connections sequentially. However, the server can
service them simultaneously through the use of threads - one thread per each client
connection.
while (true) {
accept a connection ;
create a thread to deal with the client ;
end while
The thread reads from and writes to the client connection as necessary.
Try This: Modify the KnockKnockServer so that it can service multiple clients at the
same time. Two classes compose our solution: KKMultiServer and
KKMultiServerThread. KKMultiServer loops forever, listening for client connection
requests on a ServerSocket. When a request comes in, KKMultiServer accepts the
connection, creates a new KKMultiServerThread object to process it, hands it the socket
returned from accept, and starts the thread. Then the server goes back to listening for
connection requests. The KKMultiServerThread object communicates to the client by
reading from and writing to the socket. Run the new Knock Knock server and then run
several clients in succession.