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.
4. Invoke the start() method.
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.
Pretty informative!
ReplyDeleteInformative blog
ReplyDeleteThanks
DeleteIMFORMATIVE
ReplyDeleteVery helpful
ReplyDeleteThanks
DeleteNice post
ReplyDeleteThanks
DeleteThe blog is very useful
ReplyDeleteIt cleared all my doubts regarding lan examination
ReplyDeleteMeans alot
DeleteHelpfull amd more informative
ReplyDeleteThanks
DeleteVery informative. Will expect such nice blogs further.
ReplyDeleteSure
DeleteGreat blog man..
ReplyDeleteVery nicely explained
ReplyDeleteEach step for creating LAN Examination system is described
ReplyDeletevery well. It was worth a read. Great work by the team.
Helpful 💯💯
ReplyDeleteNice...
ReplyDeleteThanks
DeleteEkk number
ReplyDeleteThanks
DeleteThanks
DeleteInformative
ReplyDeleteInformative.
ReplyDeleteHelps me clear my doubts.
Thanks
DeleteHelpful
ReplyDeleteThanks
DeleteHelpful
ReplyDeleteThanks
DeleteHelpful!
ReplyDeleteGreat explanation
ReplyDeleteHelpful
ReplyDeleteThanks
DeleteGood work
ReplyDeleteThanks
DeleteNicely explained.
ReplyDeleteGood Content
ReplyDeleteInformative
ReplyDeleteUnique content
ReplyDeleteThank you
DeleteNice blog
ReplyDeleteThanks
ReplyDeleteThanks
ReplyDelete