I know the basic ServerSocket, Server classes. Now I am trying to make a card game (which is turned based of course). One of the four players host the game, other 3 join as clients.
What I am having problem with is, a client needs to know the IP of the server to join right? So suppose I want to show the clients a list of available hosts and the players chooses from that list which game they want to join. How do I go about doing it? Do I need a dedicated server for this match making purpose? Isn’t there any other simpler way?
If you’re talking about on a LAN, then look into zero configuration (service discovery) and multicasting. Unfortunately I’m not that familiar with network discovery beyond the basic concepts so you’ll have to do some research.
If you’re talking about the Internet, then a centralized server that keeps track of user run servers is probably the best way to go since broadcast packets aren’t generally routed outside of the local network and service discovery was never meant to support a network on the scale of the Internet. You can keep it pretty simple and implement it using a database and your favorite web scripting language.
Generally a client hosted server would publish it’s availability to your central server and the central server would record the client hosted server’s address along with a “last contact time” entry in the database. The client run server would periodically “ping” the central server to let it know it was still available with the central server updating the “last contact time” field to the time stamp of the “ping”. When a game client connects to your central server, the central server sends back a list of client hosted servers that have “pinged” within a certain timeout period. Any that haven’t are simply ignored and not sent sent out by the central server. If your central server gets a lot of traffic, set up a Cron job to delete expired entries from the table at a set interval.
What Codehead said, but I’m not sure there is any need to store the info in a database - just keep the info in memory in application scope, it’s transient information anyway.
Also, you can do the whole thing (matchmaking) with generic HTTP requests - no need to get all low-level with sockets and stuff.
Keep in mind though that if you’re talking about doing it over the Internet you’ll run into problems with firewalls/routers if you want to connect directly to other people. In that case it’s a lot more user friendly to have a central server that routes the communication between the different players.
Mike