Advertisement

Basic network program structure

Started by October 28, 2004 07:54 AM
1 comment, last by hplus0603 20 years, 3 months ago
Hey, im writing a bit of a network program, it will send messages to the server, and also sometimes it will recieve messages. Im wondering what the correct structure is? At the moment everything is in the main function, and it basicaly steps through doing this. resolve ip, connect to ip and make socket, send the packet to join, close socket, exit. Of course this isnt the right way of doing it when it gets more complicated! Im not sure if i should open socket, send message, close socket each time or if i should open socket when program starts and then close it when it finishes? Should i have a loop when the program is waiting for somthing from the server? Its a bit confusing but i want to figure out what im doing before i start! thanks, kzar
Quote:
Original post by kzar
Should i have a loop when the program is waiting for somthing from the server?


Depends on the type of application you're developing. It's probably better to run a seperate thread in your client that waits for a signal, that way your program won't freeze up.


Advertisement
Yes, you typically create and open a socket on start-up, and then keep it open until you shut down. That way, packets that arrive while you're doing other things will sit in the socket's in-queue, waiting to be delivered when you ask for them.

The FAQ suggests against using threads for "waiting for data". Instead, you should poll or select() on your socket each time through your game loop.

enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement