Reputation: 251
I have a server class which connects a client on a specific server socket port and starts off a thread with a service class. Specifically, I have 3 service classes so I would like to have 3 different ports. This however, is not working as I had expected it to. This is my code for the server:
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
public class WebsiteServer {
public static void main(String args[]) throws IOException {
ServerSocket serversocket = new ServerSocket(22401);
ServerSocket serversocket2 = new ServerSocket(22402);
Thread thread;
Thread thread2;
Socket socket;
Socket socket2;
NewUserService newuserservice;
ExistingUserService existinguserservice;
System.out.println("Waiting for clients to connect.");
while (true) {
socket = serversocket.accept();
socket2 = serversocket2.accept();
if(socket.isConnected()) {
System.out.println("NewUserClient has connected.");
newuserservice = new NewUserService(socket);
thread = new Thread(newuserservice);
thread.start();
}
if(socket2.isConnected()) {
System.out.println("ExistingUserClient has connected.");
existinguserservice = new ExistingUserService(socket2);
thread2 = new Thread(existinguserservice);
thread2.start();
}
}
}
}
It works fine if I only use one port for example:
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
public class WebsiteServer {
public static void main(String args[]) throws IOException {
ServerSocket serversocket = new ServerSocket(22401);
ServerSocket serversocket2 = new ServerSocket(22402);
Thread thread;
Thread thread2;
Socket socket;
Socket socket2;
NewUserService newuserservice;
ExistingUserService existinguserservice;
System.out.println("Waiting for clients to connect.");
while (true) {
socket = serversocket.accept();
//socket2 = serversocket2.accept();
if(socket.isConnected()) {
System.out.println("NewUserClient has connected.");
newuserservice = new NewUserService(socket);
thread = new Thread(newuserservice);
thread.start();
}
// if(socket2.isConnected()) {
//
// System.out.println("ExistingUserClient has connected.");
// existinguserservice = new ExistingUserService(socket2);
// thread2 = new Thread(existinguserservice);
// thread2.start();
// }
}
}
}
Any help would be appreciated.
Upvotes: 0
Views: 1540
Reputation: 5452
You need to call the accept()
method in a separate thread. accept()
blocks the thread until someone joins the server. This could be 1 ms or 1 year.
I suggest you create an AcceptListener to be called when someone joins
public interface AcceptListener{
public void socketAccepted(Socket s);
}
And a handler class
public class MyServerSocket extends Thread{
private AcceptListener l;
private boolean run = true;
private ServerSocket s;
public MyServerSocket(AcceptListener l, int port){
this.l = l;
this.s = new ServerSocket(port);
this.start();
}
public void run(){
while(run){
l.socketAccepted(s.accept());
}
}
}
You'll have to handle errors and whatnot and make the overrides in you subclass(es) of AcceptListener.
Upvotes: 0
Reputation: 5873
accept()
is a blocking method. In other words, the second accept()
is waiting for another connection. Untik a second connection is accepted, your code will be blocked
Upvotes: 1