1

I'm learning about TCP/IP and am trying to use it to execute different commands on my server.

I thought i'd start small and build up. I've got a current example running which has a server and client connect, and then the server sends the current time to the client.

Now i want to make it such that the server can handle multiple clients.

How can I do this? I think i could use fork, but is there a way to do it without having multiple processes to worry about?

Are there any good primers on this sort of thing, or could you provide some instructions on how to modify my existing code?

Thanks,

Blackbinary
  • 3,936
  • 18
  • 49
  • 62
  • Check out [Beej's Guide to Network Programming](http://beej.us/guide/bgnet/). – s4y Mar 31 '11 at 17:48
  • See also http://stackoverflow.com/questions/3981566/what-is-event-driven-web-server/3982440#3982440 for some alternatives of how to do this. – ninjalj Mar 31 '11 at 19:56

3 Answers3

3

glibc Manual has a nice example. The missing code bits can be found earlier in the chapter. The nice thing about the example is that you do not need multiple threads

doron
  • 27,972
  • 12
  • 65
  • 103
1

I would recommend the use of threads:

  • One server thread has the sole purpose of listening at the server socket for incoming connections. Once a connection is received, it is passed off to a worker thread, while the server keeps listening.
  • One or more worker threads. These threads will do the majority of the work. You can choose to use one thread per socket, or you can use the select function to allow one thread to handle multiple sockets.

I don't know any primers off the top of my head, sorry.

Kevin Lacquement
  • 5,057
  • 3
  • 25
  • 30
0

Take a look at Erik's answer on this other question. You don't really need to do multithreading.

Community
  • 1
  • 1
Jeff
  • 1,807
  • 1
  • 15
  • 17