[Tutor] sockets, servers, clients, broadcasts...?

spir denis.spir at gmail.com
Fri Jun 4 10:42:54 CEST 2010


On Thu, 3 Jun 2010 18:03:34 -0400
Alex Hall <mehgcap at gmail.com> wrote:

> Hi all,
> I am a CS major, so I have had the required networking class. I get
> the principles of networking, sockets, and packets, but I have never
> had to actually implement any such principles in any program. Now I
> have this Battleship game (not a school assignment, just a summer
> project) that I am trying to make playable over the internet, since I
> have not written the AI and playing Battleship against oneself is
> rather boring.
> 
> Right now I am just trying to figure out how to implement something
> like the following:
> *you start the program and select "online game"
> *you select "server" or "client" (say you choose "server")
> *somehow, your instance of the program starts up a server that
> broadcasts something; your enemy has selected "client", and is now
> (SOMEHOW) listening for the signal your server is broadcasting
> *the signal is picked up, and, SOMEHOW, you and your opponent connect
> and can start sending and receiving data.
> 
> First, how does the client know where to look for the server? I am not
> above popping up the server's ip and making the client type it in, but
> a better solution would be great.
> How do I make it so that the client can find the server correctly? The
> above IP is one thing, but are ports important here? Not a big deal if
> they are, but I am not sure. Does someone have an example of this
> process?

I'm far to be a specialist in this field, so this is just reasoning by watching your requirements.

First, such a game seems to match peer-to-peer relation. This would be different if you implemented complicated game logic, like in the case of an AI playing.
For peers to connect, a general solution is them to register on a dedicated public interface (the same can be used to publish a server's IP, indeed). Then, your app first reads data there to know which (address,port) pair(s) are available.
But since you seem to be still exploring data exchange issues, you'd better concentrate on this basic mechanism first, choose and try one of numerous possible solutions, then only address higher-level problems. In the meanwhile, just put IPs and ports in a config file or even hardcode them as constants.

About client-server, as said above, this model does not really seem to match your use case, I guess. But this is still an interesting alternative. I would set my own post as server and players as clients. So, when playing myself, I would be a local client. This indeed allows two other guys playing using your post as server (and you maybe watching the game ;-).
But this does not make much sense if the server does not have any relevant info to deliver (eg playing hints).


Denis
________________________________

vit esse estrany ☣

spir.wikidot.com


More information about the Tutor mailing list