Socket accepting only one client
Domanda
I have created a server in java which accepts client connections. But I am able to only connect one client
class Server extends Thread{
private void startServer() {
try{
ss=new ServerSocket(3000);
s=ss.accept();
DataRead d1=new DataRead();
d1.t.start();
}catch(Exception er){
er.printStackTrace();
}
}
}
Soluzione
You only ever accept one socket. In your jButton1ActionPerformed
you have
s=ss.accept();
But that is only invoked once, when you click the jButton1 button. You need to keep calling accept()
to if you want to have multiple clients able to connect.
Also, keep in mind that each call to accept()
will block until a client connects and then return a new socket, representing that connection. So if you want to support multiple client, you shouldn't have your Socket
as a global variable, it should instead be included in the constructor of your DataRead
class, so each reader operates on a unique socket/connection/client.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow