-1

I’m writing a multiplayer server in C using sockets and pthreads. The project is separated by responsibility:

-- server.c/.h → networking (socket, bind, listen, accept, thread creation)

-- player.c/.h → player data structure and functions

-- game.c/.h → game logic

-- config.h → constants

When a new client connects, I need to:

  1. Ask the client for a username

  2. Validate it (check uniqueness)

  3. Create a player object

  4. Add it to the global player list

  5. Then move the client into the lobby/menu loop

Right now this logic is inside a function like handle_client(...), which is called after accept().

Where should this function belong?

  1. Inside server.c, meaning the server handles both networking and player initialization?

  2. Inside player.c, even though that mixes networking I/O with data structure logic?

  3. Or should I create a new module, e.g., controller.c, where the server only accepts sockets and the controller handles client interaction flow?

I want to maintain clean separation of responsibilities. Which design makes more sense?

3
  • The cleanest way is to create a new module. But dear God, are you really going to write a game server in C? Do you like pain? Or is this some school project? Commented 10 hours ago
  • @freakish What is wrong with that? Writing game servers in C is my favorite. Commented 55 mins ago
  • @CPlus I didn't say it is wrong. Only that is painful. There are plenty modern languages that can do the job done, and are like 20x easier and faster to code. Especially, why would you want to deal with so low level details like sockets? And where's encryption? Authentication? Where's all of that? Without that this must be school project. With, it is just soooo much work. Commented 28 mins ago

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.