C : “same file descriptors of all client connections” (client server programming)

后端 未结 1 1999
慢半拍i
慢半拍i 2021-01-07 04:20

On every new client connection forked server process

different processes(other children of server i.e. exec) couldn\'t identify that client with same fd in used in

相关标签:
1条回答
  • 2021-01-07 04:57

    The file descriptor number is unique only within the process it exists in, and as soon as it's closed, it can be reused (for example the next time you call accept). It is not a good choice of "connection identifier".

    In your case where you have a new process for each connection, the most natural identifier for a connection would be the process id - but you've thrown it away by not saving the return value of fork. In the parent process, fork returns the pid of the child process it created. You want to save this and use it later. In particular you can use it to kill the child process or identify when the child process exits (wait-family functions will tell you which child exited).

    Of course if your whole model is to use a separate process for each connection, I'm not sure why you need to identify connections at all in the parent process. If each connection is not a completely independent process, you might do a lot better using threads instead of processes.

    0 讨论(0)
提交回复
热议问题