aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes multiple clients Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "multiple clients" Watch "multiple clients" New topic
Author

multiple clients

conor murphy
Greenhorn

Joined: May 06, 2006
Posts: 15
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

Joined: Jan 29, 2003
Posts: 8791
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 ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
wood burning stoves
 
subject: multiple clients