Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

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.

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.
[2750 byte] By [dojo] at [2007-11-11 9:57:37]
# 1 Re: making a server handle multiple clients at once?
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
}
}
sudheerprem at 2007-11-11 22:32:09 >