Packet structure: "%d %d %d %s" So when receiving packets, we get: res=recv(yoursocket, buf, 200, flags); then do a sscanf() on buf: sscanf("%d %d %d %20s", pkttype, index, val, strval); switch(pkttype) { case PLAYER: player=val; break; case BOARD : Board[index]=val; break; etc etc etc } When sending a packet, we do: sprintf(buf, "%d %d %d %20s", BOARD, 3, 2, "unused"); res=send(mysocket, buf, strlen(buf), flags);Thoughts having done the start-game network code:
What we could do, is make a note of our outgoing socket number when we first send our name to the server, then always make a point of sending from that port number. But we can't guarantee that the port will always be available, of course, so that won't work!
So what I suppose we should do, is when we send our "join" packet, not close the connection, meaning that we are now hogging the connection until we've finished talking to the server. Everybody else can just queue until we're free. That deals with the starting situation. But we'd still have the same problem during the game - so every player must identify themselves by sending a ("%d", MyPlayerNum) at the start of every packet. Of course, you don't know your playernum until you've negotiated the start!
Looking again, we've still no way of forcing one client to get the packet and not another. We are going to have to use different ports for each user. The master must be on a well-known port, so that the others can establish the initial connection (I suppose we could work like telnet and accept a port number on the command line...). The users send in their desired port number along with their name... These are all then broadcast before the start of the game.
This is now working. Changed the syntax: Master starts as "./monopoly Myname", the rest start as "./monopoly Myname Myport"
------------------------- src/monopoly/c2 tree: connection-oriented All connect to "master" on port 6010 in order of joining (as per now) They disconnect and receive the update listening on their own port (as selected by them). Nobody closes this port. Then: Master plays first (s/he is Player 1), and sends out updates to all players as vars change (eg, Location[1]). Then Master sends PLAYER 0 2 to all players. Master now listens to player 2. Player 2 moves, sends updates to master. Master passes this on to all others. When P2 sends PLAYER 0 3, Master sends this then starts listening to P3. etc. etc. When P7 sends PLAYER 0 1, Master sends this out then plays. ---------------------------------------------------------- This networking stuff seems to be okay now (though not totally sure!) - moved from src/monopoly/c2/ to src/monopoly/ - original to old/