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,

CodePudding user response:

refer to the second floor foolish_smile 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 not a thread to deal with, so read and write operations in the socket, not because the time is too long and cause other socket request timeout, if a lot of socket connection, 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,


I watched the select and epoll, their server is only one port for receiving, I feel they are applicable to multiple socket different time random calls to order, but if more than one socket is sent at the same time? They can't send to the same port, feeling will go wrong

CodePudding user response:

Epoll under reference

CodePudding user response:

reference zone971016 reply: 3/f
Quote: refer to the second floor foolish_smile 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 not a thread to deal with, so read and write operations in the socket, not because the time is too long and cause other socket request timeout, if a lot of socket connection, 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, equivalent to 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,


I watched the select and epoll, their server is only one port for receiving, I feel they are applicable to multiple socket different time random calls to order, but if more than one socket is sent at the same time? They can't send to the same port, feel wrong

Select and epoll is used to handle multiple socket sent at the same time, although the service side is listening to a port, but through the accept returns a number of different fd and fd each corresponding to a client, so you want to add the fd to select or epoll, and then if the corresponding data from the client, the fd becomes readable
A fd corresponding to a link, not a port corresponding to a link

CodePudding user response:

引用 5 楼 flying_music 的回复:
Quote: 引用 3 楼 zone971016 的回复:
Quote: 引用 2 楼 foolish_smile 的回复:
select和epoll,实际上也都是返回连接请求的socket id,并且也是一个一个处理,如果请求比较耗资源的话,一般都是起线程去处理,这样在socket做读写操作时,不会因为时间过长而导致其他socket的请求超时,如果连接的socket很多,但是并不是每个socket每时每刻都有读写操作,可以使用线程池进行资源的合理调度,而select和epoll的区别在于,select是server端对socket进行轮询,相当于快递员十个房子一个一个去确认你要不要寄快递或者收快递,而epoll是事件驱动,相当于你寄快递或收快递时会打个电话,快递员直达,不需要一个一个去问,降低了server端的资源消耗,


I watched the select and epoll, their server is only one port for receiving, I feel they are applicable to multiple socket different time random calls to order, but if more than one socket is sent at the same time? They can't send to the same port, feel wrong

Select and epoll is used to handle multiple socket sent at the same time, although the service side is listening to a port, but through the accept returns a number of different fd and fd each corresponding to a client, so you want to add the fd to select or epoll, and then if the corresponding data from the client, the fd becomes readable
A fd corresponds to a link, it is not a port corresponding to a link


Thank you very much, I have now realized the two sockets at the same time to send, but the problem is read twice received string are buf, I how to write a different socket logic will send strings in different array to subsequent calls? Here is my receiving piece of code:
While (1)
{
Ssize_t count;
Char buf [2048].

The count=read (events [I] data. The fd, buf buf ).
printf("\n");
If (count==1)
{
/* If errno==EAGAIN, that means we have read all
The data. So go back to the main loop. */
If (errno!=EAGAIN)
{
Perror (" read ");
The done=1;
}
break;
}
Else if (count==0)
{
/* The End of The file. The remote has closed The
Connection. */
The done=1;
break;
}

/* for the buffer to standard output */
S=write (1, buf, count);
If (s==1)
{
Perror (" write ");
Abort ();
}
}

CodePudding user response:

Main purpose is to receive at the same time two string as an input parameter in a function
  • Related