• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

multiple clients

 
conor murphy
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

i have a client and a server program and i am trying to make multiple clients connect to one server. i create a new server thread each time a client connects. when i run the program, i can open up a single client and send a message to the server. but when i open up a new client and connect, the new client can send a message to the server but the first client doesnt send it anymore. could someone please tell me what im doing wrong.

thanks

ps. i took most of the code out to make it smaller. i think the problem is somewhere in this code but i cant find it.


class Client {


public static Socket sock;

public static BufferedReader in;
public static PrintWriter out;


public Client() {

}


public static Socket connect() {


try {

Socket skt = new Socket("localhost", 12345);

return skt;
}
catch(Exception e) {
System.out.print("Connection Failed!\n");
return null;
}

}

public static void sendMessage() {

out.println(msgBox.getText())//this is a textbox that i have;
out.flush();



}


/** main class that initiate and starts the application */
public static void main(String[] args){

String s;

while(true)
{


try{

sock = connect();

}

catch (IOException e) {
System.out.println("Connection Failed");
}




}


}//main
}


class Server {


public static boolean listening = true;

public static Socket sock;

public static BufferedReader in;
public static PrintWriter out;



public Server() {
//initiate the inteface


}

public static void main(String[] args) throws IOException{

ServerSocket server = null;
String s;

try {

server = new ServerSocket(12345);
}

catch (IOException e) {
System.out.println("Connection Failed");
}

while(listening)
{

new ServerThread(server.accept()).start();

}

server.close();
}//main
}


public class ServerThread extends Thread {

private static Socket sock = null;

public ServerThread(Socket socket) {

this.sock = socket;


}

public void run() {

String s;

while(true)
{
try{

BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));

PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sock.getOutputStream())), true);

s="";

if (in.ready())
{

s = in.readLine();

if ((s != null) && (s.length() != 0))
{
System.out.println("Recieved from: "+ s+"\n");

}
}

}

catch (IOException e) {
System.out.println("Connection Failed");
}

}


}
}
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This looks to be largely on the right track. One thing to try is avoid ready(). This only tells you whether there are bytes ready to read right now. It doesn't block and wait for some that might come later. Loop until the line you read is null.

Here's some very similar code that uses a thread pool instead of making new ones all the time:

[ May 21, 2006: Message edited by: Stan James ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic