简体   繁体   English

非阻塞的系统/套接字并发?

[英]Sys/socket concurrency for non-blocking?

I have a simple socket server set up using sys/socket and OpenSSL.我有一个使用 sys/socket 和 OpenSSL 设置的简单套接字服务器。 For each connection, the client is required to send a message to the server, receive a response and then reply to that response.对于每个连接,客户端都需要向服务器发送消息,接收响应,然后回复该响应。

I can't find any clear mechanism for making these sockets non-blocking?我找不到任何使这些套接字非阻塞的明确机制? The system has to be able to handle multiple sockets concurrently...系统必须能够同时处理多个套接字......

My server code for listening for connections:我用于侦听连接的服务器代码:

while(1)
{
   struct sockaddr_in addr;
   uint len = sizeof(addr);
   SSL *ssl;            
   int client = accept(sock, (struct sockaddr*)&addr, &len);
   if (client > 0) 
   {
      std::cout<<"Client accepted..."<<std::endl;
   }
   else
   {
      perror("Unable to accept");
      exit(EXIT_FAILURE);
   }

   ssl = SSL_new(ctx); 
   SSL_set_fd(ssl, client);

   if (SSL_accept(ssl) <= 0)
   {
      std::cout<<"ERROR"<<std::endl;
   }
   else
   {
      char buff[1024];
      SSL_read(ssl, buff, 1024);
      std::cout<<buff<<std::endl;

      std::string reply="Thanks from the server";
      char buff_response[1024];
      reply.copy(buff_response, 1024);
      const void *buf=&buff_response;
      SSL_write(ssl, buf, 1024);

      char another_buff[1024];
      SSL_read(ssl,another_buff,1024);
      std::cout<<another_buff<<std::endl;
   }
}

I've looked into 'select()', however this doesn't seem to allow concurrency as such, but allows the system to know when a socket is freed?我已经研究过“select()”,但是这似乎不允许并发,但允许系统知道套接字何时被释放?

Does anyone have any experience in solving this basic problem?有没有人有解决这个基本问题的经验?

First, with server code, it's important to differentiate between concurrency and parallelism .首先,对于服务器代码, 区分并发性和并行性很重要。 A reasonable server will typically handle many more connections concurrently than its number of cores .一个合理的服务器通常会同时处理比其内核数多得多的连接 Consequently, it's important to make the code concurrent in the sense that it can (efficiently) handle many concurrent connections, in a way that does not rely on parallelism (in the sense of having each connection handled by a thread).因此,重要的是使代码并发,因为它可以(有效地)处理许多并发连接,而​​不依赖于并行性(在每个连接都由一个线程处理的意义上)。

In this sense, select is actually a reasonable choice for concurrency, and gives you the effect of being non-blocking .从这个意义上说, select实际上是并发的合理选择,并且给了你非阻塞的效果

When your system handles multiple sockets concurrently, select indicates on which socket(s) you can perform operations such as send and recv without their blocking when you do so.当您的系统同时处理多个套接字时, select指示您可以在哪些套接字上执行诸如sendrecv而不会阻塞。 If you use select well you won't have cases where your thread is idling, waiting indefinitely for some operation to proceed, while other sockets are ready.如果您很好地使用select您将不会遇到线程空闲、无限期等待某些操作继续进行而其他套接字已准备就绪的情况。

The minimal example from gnu.org shows a reasonably efficient server which it seems you can adapt to your needs. 来自 gnu.org最小示例显示了一个相当高效的服务器,它似乎可以满足您的需求。

fd_set active_fd_set, read_fd_set;
FD_ZERO (&active_fd_set);
FD_ZERO (&read_fd_set);

// Use FD_SET to add sockets according to what you want to do with them

/* This call (checking to see who can be read) is the 
* only thing that blocks. But if it does, no socket is ready for reading. */
if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0) {
    // Handle error;

for (i = 0; i < FD_SETSIZE; ++i)
    if (FD_ISSET (i, &read_fd_set))
        // Here you can read without its blocking.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM