Network programming...how hard is it?

Status
Not open for further replies.

Nimdok

Ars Scholae Palatinae
1,214
If you have to ask, I'd say you're already in way over your head :)

Did you write the original game? Do you know C++? Have you ever done network programming? What language is the original game in? Will it have to be internet playable or just LAN? Is it realtime or turn-based?

I'd say if it's turn-based you still have a chance. That would be relatively simple. If you're unfamiliar with network programming, you might start out with something like the chat portion of your game first.
 

Screndib

Ars Tribunus Militum
2,038
The game hasn't been written yet, which I would think makes it easier. Yes I know C++. The game is real time and all I'd have to worry about is having it playable over LAN. I have no doubts about learning some sort of network programming, it's whether or not I can learn it in the span of a couple months on top of the other stuff I have to do. I know that I wouldn't attempt to learn network programming during the coding of the game, and I don't start on the game til the beginning of january. The reason why I ask now is that I'll have 2 months to put into learning how to use the networking before that and if that's not enough time then I'd rather spend the time looking into other stuff.

edit: fuck you english language.
 
If you already have a good grasp of TCP/IP operations, using the Winsock.ocx control won't be hard at all. The "hard" part is creating your own packets to control the actions/states of the game.

I do agree that the turn-based game is so much easier to learn from. I created a networked version of Battleship in about a day. State management was pretty complex and debugging the client/server was a true pain. A word to the wise, create some nice debug logs with your program.
 
Sure, not a problem. The code, however, won't help you because it is horribly designed and even I can't read it. :(


Here's the top-level view


[Program Starts]
User decides to be a Server or Connect to a server.
Server will bind to a predefined port and listen - client will connect to server (by name or IP) on predefined port.

[Boards Initialized]
Local and Enemy game boards are initialized on both client and server.
"Move Ships" state is enabled on both programs.

[I'm ready comunications]
Server waits for "My ships are in place" message from client.
Once server and client's ships are in place, server starts as the Offensive player
Game Start state is enabled

[Battle]
Offensive program sends a move in X,Y format
Deffensive program sends Hit or Miss message
Both programs draw results.
Offensive program and Deffensive program switch roles

Battle continues until the server sends the "Game Restart", program flow returns to the [Boards Initialized] section.


The packets I used where simple text. The first character specified the type of packet and the last character of the packet was a PIPE charater "|".
"0" = client ready (ships in place)
"3" = game start
"1" = offensive move
"2" = defensive respone

The offensive move is followed by the X,Y coordinates of the move.
The deffensive move is followed by the X,Y coordinates of the move and a 0 or 1 for miss or hit.

Example:

Client sends player ready: 0|
Server sends game start when all ships are in place: 3|
Server sends attack: 1,1,5|
Client sends response: 2,1,5,0|
Miss - Both boards are updated, roles reversed
Client sends attack: 1,8,5|
Server sends response: 2,8,5,1|
Hit - Both boards are updated, roles reversed


The "end of packet" character is needed to split up two packets that arrive at the same time (asynchronous communication). For example, "2,1,5,0|1,8,5|" can be the clients response to an attack as well as his next attack.

To parse a packet(s), tokenize it using the "|" character and then parse each individual packet. For the individual packets, chop of the first character and use a Case statement to handle to remaining section of the individual packet. In my example, PLAYER_MOVE and PLAYER_ATTACK_RESPONSE would further tokenize using the "," character.

Case PLAYER_READY:
Case GAME_START:
Case PLAYER_MOVE:
Case PLAYER_ATTACK_RESPONSE:
Case GAME_RESET:

The "battle" code is the same for both client and server. This was easier to program but would be lead to easy cheating in the real world.


I hope that's a good start. Let me know if I need to clarify further.
 
Status
Not open for further replies.