2

hi i m a beginner in java i have a doubt:

in the case for example i write a game with a server and 3 clients and since i dont need threads because clients follow a decided order and only one of them can play in its turn,

can i put the server in listening and execute some code like this :

 while((i++ < maxConnections) ){

      Socket connection  = listener.accept();
        // code that saves the  "connection"  to a player Object 
        //not closing the connection 

      }

tell me if i am wrong (interested in the part after the accept):

if i dont close the connection each time a create it for each client, can i write and read form the stream every time i need it ?? ( using the connection variable saved in each player object ) i ll close the connection only when the client has finished all the communication with the server

if i dont close it, the second client cannot connect? is it only possible with threads ?

sorry but i am really confused on netprogramming

2 Answers 2

2

The flaw in this method is that you have to receive that many listeners in order to continue on with the execution of the application. Also, you have to store them (which I can see you're doing with a Player object). What I'd do is, because the game is so small, pass the accepted socket endpoints off to new threads where they will be managed. The problem with this system is that you have no send/receive handling, so you need something to manage this without blocking up the rest of your server. You shouldn't do the sending and receiving from a socket in separate threads, manage the send and receive in the same thread, but be clever about it and don't block.

Also, I'd advise using Socket channels for the ease-of-programming this brings. A selector in this instance will tell you when a socket has data available for reading or room in the send buffer available for writing. You wait in a loop on the select() method of the selector after registering your channels with it. Apache MINA is easier as it handles the sockets and gives you asynchronous methods to work with, and it handles all this underlying stuff. You can choose to use these or wait for operations to complete, so it's quite flexible.

Sign up to request clarification or add additional context in comments.

2 Comments

yes exactly i dont want more than 3 players and not less. But i thought that when i need to send/receive objects, i could use the saved connection(in the Player object) to send/receive using the logic of the class Game(that resides on the server side). After the 3 connections are estabilished in the same thread i launch the Game class that could listen in turn on the specific socket of the player to send and receive with the client. It would be a simple turn based game. I have no idea how to pass communication beetwen server thread and the other threads. i ll try to use Socket channels
I'd use a Queue of some sort (ArrayDeque maybe, 1.6+) You can add your client commands to this queue and then consume them in the game thread via a standard single-threaded update loop. Synchronise on the queue object when you add and remove from the queue.
1

Use a thread for each connected client. Your game logic should be independent from the actual underlying technology to get data from the client to the server.

If you plan on getting serious with Game server development, RedDwarf has already everything in place for you.

2 Comments

uhm but what is unclear to me it is how thread can communcate data with other threads.
in the simplest case you give the thread a reference to an instance of the class that implements your game logic at creation time. Other possibilities is via static references.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.