Home > Back-end > C Linux socket, single server client
C Linux socket, single server client
Time:04-17
Two client such as a server, logic is server running all the time, only receive any client data processing after an end to the cycle and not wait for another client, if received two client data is processed at the same time end of cycle,
I want to a server code prototype:
Bind (sock1) Listen (sock1)
Bind (sock2) Listen (sock2)
While (1) {
Int clnt_sock1=accept (serv_sock1); Int clnt_sock2=accept (serv_sock2);
The recv (clnt_sock1); The recv (clnt_sock2);
the process data;
Close (clnt_sock1); Close (clnt_sock2); }
But this kind of treatment would be if only sock2 bind successful send sock1 accept blocked off, so write only two sock received to succeed at the same time,
I checked before multithreaded, such as asynchronous I/O, but the feeling is not suitable for the problem with this logic, Internet chatrooms, seems a little similar, but was a little problem,
CodePudding user response:
Baidu once multiplexing, select, or epoll, is designed to solve you this problem
CodePudding user response:
Select and epoll, actually also is returned to the socket connection request id, and is also a a processing, if the request is the consumption of resources, are generally a thread to handle, so read and write operations in the socket, not because the time is too long and cause other socket request timeout, if the connection socket is very much, but not every socket every moment has read and write operations, can use the thread pool with reasonable scheduling of the resources, and the difference between the select and epoll, select socket for polling is the server end, is equivalent to ten house Courier one by one to confirm do you want to send it special delivery or express, and epoll is event driven, the equivalent of a call when you send it special delivery or express, express direct, don't need to ask one by one, reduce the server side resource consumption,