Skip to main content
gamedev.net
🔒 Locked

server program flow, servers in general

Started by RolandofGilead Dec 26, 2004 at 12:45 PM 1 replies 400+ views
Original Post
RolandofGilead
RolandofGilead
I'm writing a console(no ui, no input other than the network) multi-client server in C using Winsock 2.0. It isn't a game server, it's used to process information sent to it. I'm quite new to programming and thinking in networking terms. I'd like a guide to server program flow. Similar to the way a game typically has a game loop, I'd like to know what paradigms are used for making servers in general. I'd also like to know how I'm supposed to let the server know it's supposed to terminate. Currently it either runs through its test and terminates or it runs until I kill the process in Task Manager. Its execution looks like this at the moment. Initialize some stuff. Printf some info. Process-while (any active client exists) {} Check incoming and outgoing buffers. Update active client list if incoming buffer has been filled at all. Send out anything in the output buffer. reorganize memory sleep so it doesn't take up 100% cpu time repeat, back to Process. For starters do I need to start a thread where the server listens for connections? The processing of each client is handled in one thread currently, what factors are there to determine if each client needs a separate thread? I can quite easily monitor how much processing has been done but I know thread switching is quite easy for modern computers but it is possilble for one client to change part of the data that all clients use.
hplus0603
hplus0603
No thread needed.

Typically, you'll have a separate program to control the server; it will connect to the server as a client, authenticate as an administrative client, and issue a "stop" command.

A typical server loop is very similar to a typical program message loop:

  create listening socket  put listening socket in fd set  while( not done ) {    select() on active sockets    handle all sockets that select() says had something  }


"handle" for the listening socket means accept()-ing a new connection and putting that socket in the set of active fds. For all other sockets, it means read all available data, then process any complete user requests, and enqueue output data.

Note that a user may connect to your server, and then disconnect, leaving you with 0 clients. It would be a bad idea to terminate at this point, because other users may want to connect later.
enum Bool { True, False, FileNotFound };
RolandofGilead
RolandofGilead
Quote:
Original post by hplus0603
Typically, you'll have a separate program to control the server; it will connect to the server as a client, authenticate as an administrative client, and issue a "stop" command.

That's what I figured.
Quote:
A typical server loop is very similar to a typical program message loop:
  create listening socket  put listening socket in fd set  while( not done ) {    select() on active sockets    handle all sockets that select() says had something  }

"handle" for the listening socket means accept()-ing a new connection and putting that socket in the set of active fds. For all other sockets, it means read all available data, then process any complete user requests, and enqueue output data.

Excellent. Thanks.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.