Why to use threads in network programming?
The reason is simple, we don’t want only a single client to connect to server at a particular time but many clients simultaneously. We want our architecture to support multiple clients at the same time. For this reason, we must use threads on server side so that whenever a client request comes, a separate thread can be assigned for handling each request. With our basic server-client program, the request which comes even a nano-second first would be able to connect to the server and the other request would be rejected as no mechanism is provided for handling multiple requests simultaneously. To overcome this problem, we use threading in network programming.
Socket Programming provides one to one as well as one to many client server communication. In various application we need to connect multiple client with server. For example, In LAN examination system that we are going to discuss in, we need to connect multiple students with server.
One of the way to handle multiple clients is via multithreading . As most of the platform provides one to many model in which one kernal level thread is created and many threads depending on requirement can be created by that kernal level thread.
 |
Fig. Implementation of multiple clients. |
Server class : The main server implementation is easy and similar to the previous article. The following points will help understand Server implementation :
1. The server runs an infinite loop to keep accepting incoming requests.
2. When a request comes, it assigns a new thread to handle the communication part.
3. The sever also stores the client name into a vector, to keep a track of connected devices. The vector stores the thread object corresponding to the current request. The helper class uses this vector to find the name of recipient to which message is to be delivered. As this vector holds all the streams, handler class can use it to successfully deliver messages to specific clients.
try
{
s = ss.accept();
System.out.println(
"A new client is connected : "
+ s);
DataInputStream dis =
new
DataInputStream(s.getInputStream());
DataOutputStream dos =
new
DataOutputStream(s.getOutputStream());
System.out.println(
"Assigning new thread for this client"
);
Thread t =
new
ClientHandler(s, dis, dos);
t.start();
}
catch
(Exception e){
s.close();
e.printStackTrace();
}
ClientHandler class : Similar to previous article, we create a helper class for handling various requests. This time, along with the socket and streams, we introduce a name variable. This will hold the name of the client that is connected to the server. The following points will help understand ClientHandler implementation :
1. Whenever the handler receives any string, it breaks it into the message and recipient part. It uses Stringtokenizer for this purpose with ‘#’ as the delimiter. Here it is assumed that the string is always of the format:
2. Message #recipent.
3. It then searches for the name of recipient in the connected clients list, stored as a vector in the server. If it finds the recipients name in the clients list, it forwards the message on its output stream with the name of the sender prefixed to the message.
try
{
received = dis.readUTF();
if
(received.equals(
"Exit"
))
{
System.out.println(
"Client "
+
this
.s +
" sends exit..."
);
System.out.println(
"Closing this connection."
);
this
.s.close();
System.out.println(
"Connection closed"
);
break
;
}
Date date =
new
Date();
switch
(received) {
case
"ans1"
:
toreturn = fordate.format(date);
dos.writeUTF(toreturn);
break
;
case
"ans2"
:
toreturn = fortime.format(date);
dos.writeUTF(toreturn);
break
;
default
:
dos.writeUTF(
"Invalid input"
);
break
;
}
}
catch
(IOException e) {
e.printStackTrace();
}
}