简体   繁体   English

C ++多线程TCP服务器问题

[英]C++ Multithreaded TCP Server Issue

I'm writing a simple TCP server. 我正在写一个简单的TCP服务器。 The model that I though of is the server accepting client connections in the main thread and handing them over to another thread so that server can listen for connections again. 我所使用的模型是服务器接受主线程中的客户端连接并将它们交给另一个线程,以便服务器可以再次侦听连接。 The relevant parts of the code that I used are posted below: 我使用的代码的相关部分发布如下:

Accepting connections: 接受连接:

void startServer () {

    int serverSideSocket = 0;
    int clientSideSocket = 0;

    serverSideSocket = socket(AF_INET, SOCK_STREAM, 0);
    if (serverSideSocket < 0) {
        error("ERROR opening socket");
        exit(1);
    }

    clientAddressLength = sizeof(clientAddress);
    memset((char *) &serverAddress, 0, sizeof(serverAddress));
    memset((char *) &clientAddress, 0, clientAddressLength);

    serverAddress.sin_family = AF_INET;
    serverAddress.sin_addr.s_addr = INADDR_ANY;
    serverAddress.sin_port = htons(32000);

    if (bind(serverSideSocket, (struct sockaddr *) &serverAddress, sizeof(serverAddress)) < 0) {
        error("ERROR on binding");
        exit(1);
    }      

    listen(serverSideSocket, SOMAXCONN);

    while(true) {
        clientSideSocket = accept(serverSideSocket, (struct sockaddr *) &clientAddress, &clientAddressLength);

    if (clientSideSocket < 0)
            error("ERROR on accept");

        processingThreadGroup->create_thread(boost::bind(process, clientSideSocket, this));
    }

}

Here, the processingThreadGroup is a boost::thread_group instance. 这里,processingThreadGroup是一个boost :: thread_group实例。 In the process method: 在流程方法中:

void process (int clientSideSocket, DataCollector* collector) {

int numberOfCharactersRead = 0;
string buffer;

do {
      char msgBuffer[1000];
      numberOfCharactersRead = recv(clientSideSocket, msgBuffer, (1000 - 1), 0);
      if (numberOfCharactersRead < 0) {
          //display error
          close(clientSideSocket);
      }
      else if (numberOfCharactersRead == 0)
          close(clientSideSocket);
      else {
          printf("%s", msgBuffer);
          memset(msgBuffer, 0, 1000);
      }
   } while (numberOfCharactersRead > 0); 
}

However, when I debug the code, I saw that when the processing thread is invoked, the main thread is not accepting connections anymore. 但是,当我调试代码时,我看到当调用处理线程时,主线程不再接受连接。 The data is read inside the process() method only. 仅在process()方法内读取数据。 The main thread seem to be not running anymore. 主线程似乎不再运行了。 What is the issue with the approach I took and any suggestions to correct it? 我采取的方法有什么问题,有什么建议可以纠正吗?

EDIT: I think I found the issue here, and have updated it as an answer. 编辑:我想我在这里找到了问题,并将其更新为答案。 Will not accept it since I answered my own question. 自从我回答自己的问题以来,我不会接受它。 Thank you for the help everyone! 谢谢大家的帮助!

Think I found the issue. 我想我发现了这个问题。 I was using this as a server to accept syslog messages. 我使用它作为服务器来接受系统日志消息。 The code I use for the syslog message generator is as follows: 我用于syslog消息生成器的代码如下:

openlog ("MyProgram", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL0);
cout << "opened the log" << endl;
for (int i = 0 ; i < 10 ; i++) 
{
    syslog (LOG_INFO, "Program started by User %d \n", getuid ());
    syslog (LOG_WARNING, "Beware of the WARNING! \n");
    syslog (LOG_ERR, "fatal ERROR! \n");
}   
closelog ();
cout << "closed the log" << endl;

and I use an entry in the rsyslog.conf file to direct all syslog LOG_LOCAL0 application traffic to be sent to the relevant TCP port where the server is listening. 我在rsyslog.conf文件中使用一个条目来指示所有syslog LOG_LOCAL0应用程序流量被发送到服务器正在侦听的相关TCP端口。 Somehow, syslog allows only one connection to be made, not multiple connections. 不知何故,syslog只允许一个连接,而不是多个连接。 Therefore, it only used one connection in a single thread. 因此,它只在一个线程中使用一个连接。 If that connection was closed, a new connection is craeted. 如果该连接已关闭,则会建立新连接。

I checked with a normal tcp client. 我检查了一个普通的tcp客户端。 That works fine, with multiple threads being spawned for each connection accepted. 这工作正常,为每个接受的连接生成多个线程。

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

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