Logan Serman
Logan Serman

Reputation: 29880

JAVA - Difficulties with a simple server seeing the clients

I am trying to make a simple game that goes across a TCP network.

The server does something like this to check for connections, in Server.java:

try
{
    server = new ServerSocket(port);
    System.out.println("Server started on port " + port);

    while (true)
    {
        socket = server.accept();
        System.out.println("A new player has joined the server.");
        new Server(socket).start();
    }
}

And to make a new client, in Player.java:

socket = new Socket(hostname, port);

Now, this works fine and all, but I need the server to add the instance of the Player to an array list and be able to send all of them certain data. They both have main methods, so one computer can just run the server and have 4 other computers connect to it by running Player.java. If all Player does is create a new socket, how is the Server supposed to interact with each Player?

Upvotes: 1

Views: 453

Answers (4)

Manoj
Manoj

Reputation: 374

Let me do some name refactorings based on my understanding of his question.

Rename existing Server.java to Player.java

Player player = new Player(socket);
players.add(player); // server can use this collection for group notification
.....


class Player implements Runnable {

   Player(Socket socket) {
        this.socket = socket;
        new Thread(this).start(); // later you may avoid it by using a threadpool executor. 
   }


   public void run() {
        while(running) {
            // read and write data from client machines.
            // use wait() appropriately
        }
   }

}

If server wants to notify all players, it can call a method on the player object which further does required work to send data to the client.

Finally rename existing Player.java to GameLauncher.java.

You might get more grip after doing this simple 'rename' refactorings.

Upvotes: 0

Bhushan Bhangale
Bhushan Bhangale

Reputation: 10987

server.accept() method returns you an object of Socket. Socket class have a method getOutputStream(), you can use this stream to send a message.

Upvotes: 1

C. K. Young
C. K. Young

Reputation: 223153

Try this:

Server client = new Server(socket);
clients.add(client);
client.start();

where clients has type List<Server>. Then, add a sendMessage method (or whatever) to your Server class. Then:

for (Server client : clients) {
    client.sendMessage("...");
}

Hope this helps! (BTW there is a distinct reason I call the variables client and clients. They're really abstractions for incoming clients, not servers as such, even though the code inside Server is really a server for interacting with said clients.)

Upvotes: 0

Uri
Uri

Reputation: 89819

I'm not sure I understand the question.

The call to accept will produce a different return value for each connecting client (typically representing the client address and port), so you need to store each of them. You can then interact via the sockets, Sun has a pretty good example in its tutorial:

That being said, save yourself a lot of headache and think over the performance you expect from the game. You can save yourself a lot of trouble and easily add reliability and scalability if you use JMS and ActiveMQ for your communications rather than mess around with sockets yourself.

Spend your time writing the game, not doing low-level socket programming, unless you're trying to learn.

Upvotes: 1

Related Questions