making a server handle multiple clients at once?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: making a server handle multiple clients at once?

  1. #1
    Join Date
    Nov 2006
    Posts
    13

    making a server handle multiple clients at once?

    Hello,Im making a chat program and so far ive programmed the client and the server.Unfortunately my server can only recive messages and send it too only 1 client at a time,so for example:
    1.Client1 connects to the server.
    2.Client1 types a message and clicks send
    3.The server recives the message and sends it back to the chat text area to Client1 with Client1's name.
    4.Client2 Connects types a message and clicks send.
    5.Client2 Never gets a reply from the server(even though it is connected to the server).
    6.When Client1 dissconectes from the server and client2 is the only one connected it gets a reply.

    Can someone solve this problem for me so everyone can talk to the server and the server outputs all the mesages to all the client at once.

    My current code for the server is:By the way run() is what handles all the messages at sends them.

    Code:
    import java.net.*;
    import java.io.*;
    import javax.swing.*;
    import java.lang.*;
    
    
    public class server implements Runnable {
    	ServerSocket server;
    	Socket income;
    		 DataInputStream input;
    		  PrintStream output;
    		  Thread t;	
    		  
    		 int numbers = 1; //number of clients connected
    		  
    	
    	
    	public void init()	
    	{	
    		t=new Thread(this);
    		t.start();
    	}
    
    	
    	
    	public server() {
    		
    		
    		try {
    		
    		server = new ServerSocket(4001);
    		
    	}catch(IOException ff){
    	}
    	try {
    		while(true){
    		
    		income = server.accept();
    		run();
    		numbers = 1;
    		
    		}
    	}catch(IOException ee){
    	}
    	
    
    	//Counter.start();
    	while(true) {
    	
    	}
    	}
    	
    	
    	
                
            public void run()                       
            {              
              try {//starts try 1
    
    	
    
    
    while(numbers > 0){
    
    	  	input = new DataInputStream(income.getInputStream());
       	 output = new PrintStream(income.getOutputStream());
       	 
       	 output.flush();
       	 String bye = "bye1";
       	 String read = input.readLine();
       	  if (read == null){
       	 	numbers--;
       	 	System.out.println("DISCONECTED");
       	 }
       	 System.out.println(read);
       	 output.println(read);
       	
       	 
       	 
       	 t.sleep(10);
    		
    	
    	//ends try
    	
    	}
    	}
    	catch(IOException e){
    		System.out.println(e);
    	}catch(InterruptedException gg){
    	}
    	}
            
    
    	
    	
    	
    	public static void main(String[] args){
    		server app = new server();
    	}
    }
    Thanks.So you don't get confused the thread t does not really do nothing.

  2. #2
    Join Date
    Dec 2006
    Location
    Kerala,India
    Posts
    51
    Your code calls run method when a Socket is recieved through the accept() method of ServerSocket. Since the run method is not executing as a different thread, ServerSocket will not accept any other client untill run method finish its execution.

    I'm posting a sample code which will make you to understand how to handle multiple client concurrently

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

    public interface SocketListener {
    public void socketRecieved(Socket socket);
    }

    public class Server implements Runnable{

    private int port = 0;
    private SocketListener socketListener = null;

    public Server(int port,SocketListener socketListener) {
    this.port = port;
    this.socketListener=socketListener;
    }
    public void run() {

    Socket socket = null;
    try
    {
    ServerSocket s = new ServerSocket(port);
    while(true) {
    socket = s.accept();
    new SocketHandlerThread(socket).start();
    }
    }catch(IOException ioexception){
    iOException.printStackTrace();
    }
    catch(Exception e){
    e.printStackTrace();
    }

    }
    private class SocketHandlerThread extends Thread{
    private Socket socket = null;
    public SocketHandlerThread(Socket socket){
    this.socket = socket;
    }
    public void run() {
    socketListener.socketRecieved(socket);
    }
    }
    }

    class SocketHandler implements SocketListener{
    public static void main(String args[]) {
    Runnable server = new server(2080,new SocketHandler());
    new Thread(server).start();
    }
    public void socketRecieved(Socket socket){
    //write to read the data from socket
    }
    }

Similar Threads

  1. find version & service packs
    By rperez in forum Database
    Replies: 5
    Last Post: 01-02-2009, 05:14 PM
  2. Replies: 0
    Last Post: 06-29-2001, 12:10 PM
  3. MAKING A PERSONAL EMAIL SERVER
    By sparkytheowl in forum Architecture and Design
    Replies: 0
    Last Post: 05-01-2001, 04:31 PM
  4. Winsock:1server Multiple Clients
    By FELIX in forum VB Classic
    Replies: 10
    Last Post: 04-24-2001, 09:03 AM
  5. Remote Clients can't access SQL Server
    By Jason Langston in forum Enterprise
    Replies: 1
    Last Post: 07-12-2000, 03:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center