MULTI THREADING IN DATAGRAM (UDP) - Code

Latest

programs

Sunday, October 30, 2011

MULTI THREADING IN DATAGRAM (UDP)


MULTI THREADING IN DATAGRAM (UDP)

THEORY:

The ability of executing simultaneously several programs is known as multitasking. In system terminology, it is called multithreading.
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 Echo Server (UDP)
(Client Algorithm)


1. Create a DatagramSocket.
2. Create a DatagramPacket
3. Send data to Server using datagramsocket.send
4. Receive the data using datagramsocket.receive     //Echo back
5. if socket not equal to null then
            goto 2
    else
            goto 3
6. End

Multi Threaded Echo Server (UDP)
(Server Algorithm)


1. Create a Thread Class
2. Declare the DatagramSocket with the port number
3. When server received many connection from many client 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
AIM: EVEN ODD QUERY FROM SERVER

THEORY:
          The server does is wait for a connection, then uses the Socket produced by that connection to create an InputStream and OutputStream. After that, everything it reads from the InputStream it echoes to the OutputStream until it receives the line END, at which time it closes the connection.
            The client makes the connection to the server, then creates an OutputStream. Lines of text are sent through the OutputStream. The client also creates an InputStream to hear what the server is saying (which, in this case, is just the words echoed back). Both the server and client use the same port number and the client uses the local loopback address to connect to the server on the same machine so you don’t have to test it over a network.

             You can see that the ServerSocket just needs a port number, not an IP address (since it’s running on this machine!). When you call accept( ), the method blocks until some client tries to connect to it. That is, it’s there waiting for a connection but other processes can run . When a connection is made, accept( ) returns with a Socket object representing that connection. 
     
              If the ServerSocket constructor fails, the program just quits (notice we must assume that the constructor for ServerSocket doesn’t leave any open network sockets lying around if it fails). For this case, main( ) throws IOException so a try block is not necessary.
      
             If accept( ) fails, then we must assume that the Socket doesn’t exist or hold any resources, so it doesn’t need to be cleaned up. If it’s successful, however, the following statements must be in a try-finally block so that if they fail the Socket will still be cleaned up.
 

ALGO:
          SERVER SIDE
  1. Create the server socket and begin listening.
  2. Call the accept() method to get new connections.
  3. Create input and output streams for the returned socket.
  4. Conduct the conversation based on the agreed protocol.
  5. Close the client streams and socket.
  6. Go back to step 2 or continue to step 7.
  7. Close the server socket.
CLIENT  SIDE
  1. reate the client socket connection.
  2. Acquire read and write streams to the socket.
  3. Use the streams according to the server's protocol.
  4. Close the streams.
  5. Close the socket. 

No comments:

Post a Comment