franklinexpress
franklinexpress

Reputation: 1189

Java Multiple threads for just 2 computers, how to do it in main

I'm trying to get at least two computers to connect to my server, how would i start a second thread?

public static void main(String[] args) throws InterruptedException {
    // Create the server which waits for a client to request a connection.


while(true){
    FileSharedServer server = new FileSharedServer();
    Thread thread = new Thread(server);
    thread.start();
}
   }

this refuses my connection

Upvotes: 1

Views: 115

Answers (1)

Krzysztof Cichocki
Krzysztof Cichocki

Reputation: 6414

You need to wait on serverSocket.accept() method on incoming connections in your server, and after receiving one start a thread to serve it, but the server socket stay the same, you just do waiting for next connection in a loop.

while (true) {
    Socket connection = serverSocket.accept();
    new Therad() {
         public void run() {
              serveConnection(connection);
         }
    }.start();
}

Upvotes: 1

Related Questions