TCP concurrent server

Homework:
1. Can newfd in multi-threading be modified to be global? If not, why?
2. Can the newfd of the branch thread in multi-threading be accessed indirectly by using a pointer to the newfd of the main thread without saving it separately? No, why?

Answer: Because multiple threads under the same process share all the resources of its affiliated processes, when the file descriptor newfd is set as a global variable, a new newfd will be generated with the accpet function to update and overwrite the original newfd. When the client is disconnected, When opening a connection, the newly generated newfd is closed, resulting in other file descriptors not being closed. And because of the update coverage of newfd, communication disorder occurs, and other clients except the newly connected client cannot communicate with the server.

Guess you like

Origin blog.csdn.net/qq_46766479/article/details/132288091