123 Eng

Engineering the engineers™


Latest Jobs   Forum Map

 


Home
Source Codes
Engineering Colleges

Training  Reports
Seminar Reports
Placement Papers

Forums

   Computer Science / IT
   Electronics
   Electrical
   Mechanical
   Chemical
   Civil

   CAT / MBA

   GMAT / Foreign MBA
Latest Jobs

Engineering Jobs / Technical Jobs
Management Jobs

Sitemap
Terms of use

Displaying  Source Code(s)  
 

 
--------------------------------------------------------------------------------

Program to develop a Multithreaded Server.

--------------------------------------------------------------------------------



import java.io.*;
import java.net.*;

public class MultiThreadedServer
{
public static void main(String [] args)
{
int i=0;
System.out.println("Server Started. Waiting for connections...");
try
{

ServerSocket s = new ServerSocket(8081);
for(;;)
{
Socket incoming = s.accept();
System.out.println("New Client Connected with id " + i );
Thread t = new ThreadedServer(incoming,i);
i++;
t.start();
}
}
catch(Exception e)
{
System.out.println("Error: " + e);
}
}
}

class ThreadedServer extends Thread
{
Socket incoming;
int counter;
public ThreadedServer(Socket i,int c)
{
incoming=i;
counter=c;
}

public void run()
{
try
{
BufferedReader in =new BufferedReader(new InputStreamReader(incoming.getInputStream()));
PrintWriter out = new PrintWriter(incoming.getOutputStream(), true);

String line;
boolean done=false;
while(!done)
{
line=in.readLine();
if(line.trim().equals("BYE") || line == null)
done = true;
else
{
out.println("Echo Server Responds: " + line);
System.out.println("Echo Server Responds: " + line);
}
}
incoming.close();
}
catch(Exception e)
{
System.out.println("Error: " + e);
}
}
}

 

Contribute content or training reports / feedback / Comments
job placement papers
All rights reserved © copyright 123ENG