Will I need a separate socket and thread for every player that joins? [JAVA]
Asked Answered
W

2

5

I have been learning about sockets for sometime now (I'm quite young) and I think I have a good grip on java sockets. I have decided to create a simple multiplayer Java 2D social game. My goal is to have the server output players' X,Y coordinates and chat every 10 milliseconds. From what I have read, my very average logic tells me that only one user at a time can connect to a socket. So therefore I will need a separate thread and socket for each player that connects.

Is it necessary to have one ServerSocket and thread per player?

Walden answered 20/6, 2013 at 6:21 Comment(3)
Yes it is. One ServerSocket, but multiple Sockets and Threads to handle the clients.Admix
Beware of unbounded thread creation! You had better limit the total number of threads. Your best bet is to use an ExecutorService here.Protectorate
Should I just create a socket that handles connections and then directs the user to a unique socket?Walden
W
10

You should have just one ServerSocket listening on a port that is known to the client. When a client connects to the server, a new Socket object is created and the original ServerSocket goes back to listening again. You should then spin off a new Thread or hand over to an Executor the actual work of talking to the client, otherwise your server will stop listening for client connections.

Here is a very basic sketch of the code you will need.

import java.net.*;
import java.util.concurrent.*;

public class CoordinateServer {
  public static void main(String... argv) throws Exception {
    // 'port' is known to the server and the client
    int port = Integer.valueOf(argv[0]);
    ServerSocket ss = new ServerSocket(port);

    // You should decide what the best type of service is here
    ExecutorService es = Executors.newCachedThreadPool ();

    // How will you decide to shut the server down?
    while (true) {
      // Blocks until a client connects, returns the new socket 
      // to use to talk to the client
      Socket s = ss.accept ();

      // CoordinateOutputter is a class that implements Runnable 
      // and sends co-ordinates to a given socket; it's also
      // responsible for cleaning up the socket and any other
      // resources when the client leaves
      es.submit(new CoordinateOutputter(s));
    }
  }
}

I have put sockets here since they are easier to get started with, but once you have this working well and want to boost your performance you will probably want to investigate the java.nio.channels package. There's a good tutorial over at IBM.

Watercourse answered 20/6, 2013 at 8:31 Comment(0)
R
6

Yes.

A Socket is connection between two points (the client and the sever). This means that each player would require their own socket connection on the server end.

If you want your application to be responsive in any meaningful manner, then each incoming connection on the server should be processed within their own thread.

This allows clients that might have a slow connection not to become a bottle neck for others. It also means that if a client connection is lost, you don't burden any updates waiting for time-outs.

Ruthi answered 20/6, 2013 at 6:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.