Академический Документы
Профессиональный Документы
Культура Документы
client-side and the code that executes at server-side. So using the functionality
of sockets can be partitioned into two major steps:
1. The server or the server-side code
2. The client or the client-side code
The multi-threaded nature of former can always be guaranteed whereas the later may
or may not be multi-threaded.
The Server
The server's main function is to wait for incoming requests, and to service them
when they come in. So the code to implement the server can be further broken down
to the following steps:
So to create a socket bound to port number 8888 with a backlog queue of size 5 and
bound with address of local host the statement would be:
ServerSocket server = new ServerSocket(8888, 5, InetAddress.getLocalHost() );
One point to keep in mind is that the above mentioned constructors return TCP
sockets and not UDP sockets.
2. The next step is to tell the newly created server socket to listen
indefinitely and accept incoming requests. This is done by using the accept()
method of ServerSocket class. When a request comes, accept() returns a Socket
object representing the connection. In code it would be:
3. Communicating with the socket, which means reading from and writing to the
Socket object. To communicate with a Socket object, two tasks have to be
performed. First the Input and Output stream corresponding to the Socket object
has to be obtained. That can be done by using the getInputStream() and
getOutputStream() methods of Socket class. In code it would be:
The second task is to read from and write to the Socket object. Since the
communication has to continue until the client breaks the connection, the reading
from and writing to is done within a loop, like this:
boolean done = false;
while (!done)
{
String line = in.readLine();
if (line == null) done = true;
else
{
out.println("Echo: " + line);
if (line.trim().equals("BYE"))
done = true;
}
}
The actual syntax for reading and writing is not different from the I/O done for
simple files.
4. Once the client breaks the connection or stops sending the request, the Socket
object representing the client has to be closed. This can be done by calling
close() method on the Socket object. The statement would be:
incoming.close();
That's how a server is coded. The next section deals with creating a client.
The Client
The main purpose of the client is to connect to the server and communicate with it
using the connection. So coding a client requires the following steps:
a. Creating a Socket object. The socket at client side just needs to know the
host name (the name of the machine where server is running) and the port where the
server is listening. To create a Socket object, there are seven constructors
provided by the Socket class, of which the most commonly used are:
Then you read and write using the corresponding streams. For example, if the
client just waits for the data sent by the server, the code would be
That covers all the steps involved in creating a network enabled application. In
the next section I will be developing a file server application with multi-
threading to handle multiple clients.