MULTITHREADING IN TCP - Code

Latest

programs

Sunday, October 30, 2011

MULTITHREADING IN TCP


AIM:  MULTITHREADING IN TCP

THEORY:

In a typical server, you’ll want to be able to deal with many clients at once. The answer is multithreading, and in languages that don’t directly support multithreading this means all sorts of complications.
Because threading in Java is reasonably straightforward, making a server that handles multiple clients is relatively easy.
The basic scheme is to make a single ServerSocket in the server and call
 accept( ) to wait for a new connection. When accept( ) returns, you take the resulting Socket and use it to create a new thread whose job is to serve that particular client. Then you call accept( ) again to wait for a new client.
 
 
all of the operations to serve a particular client have been moved inside a separate thread class: 
The ServeOneJabber thread takes the Socket object that’s produced by accept( )
 in main( ) every time a new client makes a connection. Then, as before, it creates a BufferedReader and auto-flushed PrintWriter object using the Socket. Finally, it calls the special Thread method start( ), which performs thread initialization and then calls run( ). This performs the same kind of action as in the previous example: reading something from the socket and then echoing it back until it reads the special “END” signal. The responsibility for cleaning up the socket must again be carefully designed. In this case, the socket is created outside of the ServeOneJabber so the responsibility can be shared. If 
the ServeOneJabber constructor fails, it will just throw the exception to the caller, who will then clean up the thread. But if the constructor succeeds, then the ServeOneJabber object takes over responsibility for cleaning up the thread, in its run( ). Notice the simplicity of the MultiJabberServer. As before, a ServerSocket is created and accept( ) is called to allow a new connection. But this time, the return value of accept( ) (a Socket) is passed to the constructor for ServeOneJabber, which creates a new thread to handle that connection. When the connection is terminated, the thread simply goes away. 
 
      If the creation of the ServerSocket fails, the exception is again thrown through main( ). But if it succeeds, the outer try-finally guarantees its cleanup. The inner try-catch guards only against the failure of the ServeOneJabber constructor; if the constructor succeeds, then the 
ServeOneJabber thread will close the associated socket. To test that the server really does handle multiple clients, the following program creates many clients (using threads) that connect to the same server. Each thread has a limited 
lifetime, and when it goes away, that leaves space for the creation of a new thread. The maximum number of threads allowed is determined by the final int maxthreads. You’ll notice that this value is rather critical, since if you make it too high the threads seem to run out of resources and the program mysteriously fails. 
 
Multithreading is a conceptual programming paradigm where a program is divided into two or more subprograms (process), which can be implemented at the same time in parallel. A thread is similar to a program that has a single flow of control. It has a beginning, a body, and an end, and executes commands sequentially.
A program that contains multiple flows of control is known as multithread program.



CREATING THREAD:
Threads are implemented in the form of objects that contain a method called run(). A typical run() would appear as follows:
public void run()
{
            …………………………………………
            …………………………………………
            (Statements of implementing thread)
}
The run() method should be invoked by an object of the concerned thread.
A new thread can be created in two ways.
1. By creating a thread class.
2. By converting a class to a thread.

EXTENDING THE THREAD CLASS:
We can make our class runnable as a thread by extending the class java.lang.Thread. It includes the following steps:
  1. Declare the class as extending the Thread class.
  2. Implement the run() method that is responsible for executing the sequence of code that will execute.
  3. Create a thread object and call the start() method to initiate the the thread execution.

Declaring the class

Class MYThread extends Thread
{
      ………………………………………………
      ………………………………………………
      ………………………………………………
}



ALGO:
Multi Threaded Server
(Client Algorithm)

1. Create a Socket with local machine address and port.
2. If Server running then
            Send data to Socket Output Stream
            Wait for Echo back
    Else
            Throws Exception
            Close the Socket
3. End


Multi Threaded Server
(Server Algorithm)


1. Create a Thread Class
2. Declare the ServerSocket and Socket
3. When server received many connections from many clients it will create a Thread for each of the client
4. Accept each client's Connection
5. Echo back the data received from the client(s)
6. If any client(s) close the connection then
            Close the socket
7. End

OUT PUT:

SERVER:
D:\J2SDK\BIN>javac serverthread.java
D:\J2SDK\BIN>java serverthread
SERVER WATING FOR CONNECTION:
SERVER RECEIVE MESSAGE: smitonial
SERVER SEND MESSAGE: smitonial

CLIENT:
D:\J2SDK\BIN>javac clientthread.java
D:\J2SDK\BIN>java clientthread
smitonial
CLIENT SEND MESSAGE:
CLIENT RECEIVE MESSAGE MESSAGE: smitonial
D:\J2SDK\BIN>

No comments:

Post a Comment