[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 184
  • Last Modified:

Client/Server questions

I am making a multi-player card game.  I am going to have a server application.  The server is going to be able to handle multiple games as well.  I have a few questions.

I plan on making a GamePacket class with fields that will pertain to the game. This is the data that will be sent via ObjectOutputStream and recieved via the ObjectInputStream.  I am going to put a GameID field for the server to be able send the socket to the right Game thread.  Is this a good way to do this?  How is this usually done?

Should the client be constantly requesting updates for the game? Or should the game just send out game information?  I mean, once the intial connection is made I could  just get the IP address and have the client listening on a port.  But, I don't know if the clients firewall will block this.  How does this work?  

Overall, what is the best way to handle a multiplayer/multigame server application?
0
turtletimer
Asked:
turtletimer
  • 2
1 Solution
 
Mayank SAssociate Director - Product EngineeringCommented:
>> I plan on making a GamePacket class with fields that will pertain to the game. This is the data that will be sent via
>> ObjectOutputStream and recieved via the ObjectInputStream.

Sounds ok.

>> I am going to put a GameID field for the server to be able send the socket to the right Game thread.  Is this a good
>> way to do this?  How is this usually done?

There are many. This is one of them. Make sure the object is serializable. Some people often send data as XML strings, etc - that way, the client-side need not have the GamePacket class.

>> Should the client be constantly requesting updates for the game? Or should the game just send out game information?  

Depends upon what the game does. What kind of requests and updates are to be done? And what is the player at the client-side supposed to do in the game?

>> I mean, once the intial connection is made I could  just get the IP address and have the client listening on a port.

Don't make the client listen on a port. The server would already be listening to a port and the client would connect to that using the server's IP/ port. Then you should start off a new thread to service the client on that socket, and that socket should be used for communicating anything to the client using some predefined protocol.

>> But, I don't know if the clients firewall will block this.  

Read what I said above. Just make sure the server's fire-wall does not block it.
0
 
turtletimerAuthor Commented:
Okay, I was confused about that.  So, once a socket connection has been made, it stays made right?  I can keep calling the outputstream and inputstream of that socket?  And once the socket connection has been made the intial Server Application is going to ignore this incoming data?  This is where I get confused.  Let me explain my question better.  

1. A client requests to make a new game.  The server application answers this and ---
2. A new game is made on a seperate thread and the another thread within the game thread for the player i/o
3. Other players request to join this game and the server application routes them to the game thread where another thread is made for that player.
4. Now since there is a game thread and say 4 threads within in the game thread for each player, is there a continuous connection with each player(assuming the socket was passed to the thread)
5. Is the Server Application going to ignore all of this communication between the players and the game thread?

I just don't understand how the Server knows when to answer and when not too since all of this is going through the same port.  

Please explain well.  I've been searching around for days trying to understand this and it's just not clicking.  Links would be nice too. :)

P.S. This is a card game with four players.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> So, once a socket connection has been made, it stays made right?  

Yes.

>> I can keep calling the outputstream and inputstream of that socket?

Yes. Better to get them once using getInputStream () and getOutputStream () and maintain references to them.

>> And once the socket connection has been made the intial Server Application is going to ignore this incoming data?  

You mean the server-socket listening on that port for more client connections? Yes, that will not be affected by this.

As long as all threads have been created for the 4 different sockets, they will work fine. Try this as a sample code:

public class Server
{
  public static void main ( String[] args )
    throws Exception
  {
    ServerSocket ss = new ServerSocket ( 2725 ) ;
    while ( true )
    {
      new ServerThread ( ss.accept () ) ;
    }
  }
}

class ServerThread extends Thread
{
  private Socket s ;
  public ServerThread ( Socket s )
  {
    this.s = s ;
    start () ;
  }
  public void run ()
  {
    // obtain the input-stream and output-stream of the socket
    while ( true )
    {
      // keep talking to the client based on some protocol
    }
  }
}
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now