I am in a great problem. I am trying to implement a multithreaded server using java, first look at the code...
Expand|Select|Wrap|Line Numbers
- /*------- Main Server (server.java)--------------*/
- import java.io.* ;
- import java.net.* ;
- public class server
- {
- public static void main(String args[]) throws IOException
- {
- ServerSocket s = new ServerSocket(1234);
- while(true)
- {
- Socket s1 = s.accept();
- worker w = new worker(s1);
- w.run();
- }
- //s.close();
- }
- }
Expand|Select|Wrap|Line Numbers
- /*--------- worker (worker.java) -------------*/
- import java.io.* ;
- import java.net.* ;
- public class worker implements Runnable
- {
- private DataInputStream datain;
- public worker(Socket s) throws IOException
- {
- InputStream in = s.getInputStream();
- datain = new DataInputStream(in);
- }
- public void run()
- {
- try{
- while(true)
- {
- String st = new String(datain.readUTF());
- System.out.println(st);
- }
- }
- catch(IOException ioe)
- {
- System.out.println(ioe);
- }
- }
- }
they are...
Expand|Select|Wrap|Line Numbers
- /*---------- client1.java ----------------*/
- import java.io.* ;
- import java.net.* ;
- public class client1
- {
- public static void main(String args[]) throws IOException
- {
- Socket s = new Socket("localhost", 1234);
- OutputStream out = s.getOutputStream();
- DataOutputStream dataout = new DataOutputStream(out);
- try{
- while(true)
- {
- Thread.sleep(1000);
- dataout.writeUTF("client1");
- System.out.println("client1");
- }
- }
- catch(InterruptedException ie)
- {
- System.out.println(ie);
- }
- //dataout.close();
- //out.close();
- //s.close();
- }
- }
- /*----------- client2.java-------------- */
- import java.io.* ;
- import java.net.* ;
- public class client2
- {
- public static void main(String args[]) throws IOException
- {
- Socket s = new Socket("localhost", 1234);
- OutputStream out = s.getOutputStream();
- DataOutputStream dataout = new DataOutputStream(out);
- while(true)
- {
- dataout.writeUTF("client2");
- System.out.println("client2");
- }
- //dataout.close();
- //out.close();
- //s.close();
- }
- }
- /*--------------- client3.java ------------*/
- import java.io.* ;
- import java.net.* ;
- public class client3
- {
- public static void main(String args[]) throws IOException
- {
- Socket s = new Socket("localhost", 1234);
- OutputStream out = s.getOutputStream();
- DataOutputStream dataout = new DataOutputStream(out);
- try{
- while(true)
- {
- Thread.sleep(2000);
- dataout.writeUTF("client3");
- System.out.println("client3");
- }
- }
- catch(InterruptedException ie)
- {
- System.out.println(ie);
- }
- //dataout.close();
- //out.close();
- //s.close();
- }
- }
What will I do?........
I need concurrent workers!!!!