Saturday, December 4, 2010

Approach to Multi Threaded Socket Communication

On this article i will introduce with socket connection on single server multiple clients,Single client multiple messages(transaction based continuous connection),Multiple client multiple transaction (per connection). I will proceed with two methods of threading (extend Thread and implement Runnable).
Also at the requirement, i'll explain the use of synchronized shared objects and serialized data objects.
This article is the extend of basic-socket-programming article on
single server multiple clients
This scenario is the most widely used socket communication in general.For example web servers.Many clients connects to the server requesting some data.The 3way TCP/IP handshake will initiate and identifies the two parties.Here, as regular, client who needs data wants to start the connection.Server or the data source must be up and running, waiting for a connection request form a client for a particular local port number(eg:80).Client will connect to the server,and the server will start a new thread in order to serve the clients request and transfer context to that thread.The master/main thread will resume and will stand by for another request from some other client(may be the same client).The created thread will process independently and serve data to the client.
As soon as the data is served, the connection will be closed.So, same client for some other data, wants to initiate another connection.This way the server is stateless of the clients, or all requests/connections are totally independent with others.
To serve independantly, the server must have a multi-thread application while client may very.

No comments:

Post a Comment