简单的非阻塞多线程TCP服务器

时间:2019-06-17 01:03:27

标签: c++ multithreading tcp winsock winsock2

我正在学习C ++,这个周末我开始玩套接字和线程。 Bellow是我根据一些教程制作的简单多线程服务器。

我面临的问题是,当我与2个telnet客户端连接时,只有第一个连接的击键才会出现在服务器上。一旦第一个telnet连接关闭,从第二个telnet连接发送的任何击键都会突然出现。有人可以告诉我我在这里做错了什么吗?

#include <iostream>
#include <string>
#include <thread>
#include <winsock2.h>
#include <ws2tcpip.h>

#pragma comment (lib, "ws2_32.lib")

void clientSocketHandler(SOCKET clientSocket, std::string client_ip) {

    char buf[4096];

    std::thread::id thread_id = std::this_thread::get_id();
    std::cout << thread_id << " - " << client_ip << ": connected" << std::endl;

    while (true)
    {

        ZeroMemory(buf, 4096);

        int bytesReceived = recv(clientSocket, buf, 4096, 0);

        if (bytesReceived == 0)
        {

            std::cout << thread_id << " - " << client_ip << ": disconnected" << std::endl;

            break;

        }

        if (bytesReceived > 0) 
        {

            std::cout << thread_id << " - " << client_ip << ": " << std::string(buf, 0, bytesReceived) << std::endl;

            //send(clientSocket, buf, bytesReceived + 1, 0);

        }

    }

    std::cout << thread_id << " - " << client_ip << ": closing client socket & exiting thread..." << std::endl;

    closesocket(clientSocket);

}

void waitForConnections(SOCKET serverSocket) {

    sockaddr_in hint;

    hint.sin_family = AF_INET;
    hint.sin_port = htons(1337);
    hint.sin_addr.S_un.S_addr = INADDR_ANY;

    bind(serverSocket, (sockaddr*)&hint, sizeof(hint));
    listen(serverSocket, SOMAXCONN);

    while (true) {

        sockaddr_in client;

        int clientSize = sizeof(client);

        SOCKET clientSocket = accept(serverSocket, (sockaddr*)&client, &clientSize);

        if (clientSocket != INVALID_SOCKET) 
        {

            char host[NI_MAXHOST];      // Client's remote name

            ZeroMemory(host, NI_MAXHOST); // same as memset(host, 0, NI_MAXHOST);

            std::string client_ip = inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);
            std::thread t(clientSocketHandler, clientSocket, client_ip);

            t.join();

        }

        Sleep(100);

    }

}

int main()
{
    // Initialze winsock
    WSADATA wsData;
    WORD ver = MAKEWORD(2, 2);

    int wsOk = WSAStartup(ver, &wsData);

    if (wsOk != 0)
    {
        std::cerr << "Can't Initialize winsock! Quitting..." << std::endl;

        return 1;
    }

    // Create a socket
    SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    if (serverSocket == INVALID_SOCKET)
    {
        WSACleanup();
        std::cerr << "Can't create a socket! Quitting..." << std::endl;

        return 1;
    }

    // If serverSocketMode = 0, blocking is enabled; 
    // If serverSocketMode != 0, non-blocking mode is enabled.
    u_long serverSocketMode = 1;

    if (ioctlsocket(serverSocket, FIONBIO, &serverSocketMode) != NO_ERROR) 
    {
        WSACleanup();
        std::cerr << "Can't set socket to non-blocking mode! Quitting..." << std::endl;

        return 1;
    }

    // Disables the Nagle algorithm for send coalescing.
    // This socket option is included for backward 
    // compatibility with Windows Sockets 1.1
    BOOL flag = TRUE;

    if (setsockopt(serverSocket, IPPROTO_TCP, TCP_NODELAY, (const char *)&flag, sizeof(flag)) != NO_ERROR)
    {
        WSACleanup();
        std::cerr << "Can't set socket NO_DELAY option! Quitting..." << std::endl;

        return 1;
    }

    // Start listening for connections
    waitForConnections(serverSocket);

    // Cleanup winsock
    WSACleanup();

    system("pause");

    return 0;

}

1 个答案:

答案 0 :(得分:0)

这应该有效。我删除了毫无意义的事情,例如将套接字设置为非阻塞并禁用Nagle算法。后者仅应用于需要低毫秒级交互性的事情。

但是,应该解决您的问题的实质性更改是将join更改为detach。使用join会使程序在继续执行之前等待线程完成。使用detach表示“该线程将在后台运行,并且我以后不在乎它的命运。”。

如果您不使用两者之一,并且::std::thread对象被销毁,则系统将引发异常,因为您销毁的唯一途径是您可以获得有关是否退出线程的信息出现某种错误,说您不在乎此类信息,或明确要求它。

我没有Windows,因此无法对其进行测试:

#include <iostream>
#include <string>
#include <thread>
#include <winsock2.h>
#include <ws2tcpip.h>

#pragma comment (lib, "ws2_32.lib")

void clientSocketHandler(SOCKET clientSocket, std::string client_ip)
{
   char buf[4096];

   std::thread::id thread_id = std::this_thread::get_id();
   std::cout << thread_id << " - " << client_ip << ": connected" << std::endl;

   while (true)
   {

      ZeroMemory(buf, 4096);

      int bytesReceived = recv(clientSocket, buf, 4096, 0);

      if (bytesReceived == 0)
      {

         std::cout << thread_id << " - " << client_ip << ": disconnected" << std::endl;

         break;

      }

      if (bytesReceived > 0)
      {

         std::cout << thread_id << " - " << client_ip << ": " << std::string(buf, 0, bytesReceived) << std::endl;

         //send(clientSocket, buf, bytesReceived + 1, 0);

      }

   }

   std::cout << thread_id << " - " << client_ip << ": closing client socket & exiting thread..." << std::endl;

   closesocket(clientSocket);

}

void waitForConnections(SOCKET serverSocket)
{

   sockaddr_in hint;

   hint.sin_family = AF_INET;
   hint.sin_port = htons(1337);
   hint.sin_addr.S_un.S_addr = INADDR_ANY;

   bind(serverSocket, (sockaddr*)&hint, sizeof(hint));
   listen(serverSocket, SOMAXCONN);

   while (true) {

      sockaddr_in client;

      int clientSize = sizeof(client);

      SOCKET clientSocket = accept(serverSocket, (sockaddr*)&client, &clientSize);

      if (clientSocket != INVALID_SOCKET)
      {

         char host[NI_MAXHOST];      // Client's remote name

         ZeroMemory(host, NI_MAXHOST); // same as memset(host, 0, NI_MAXHOST);

         std::string client_ip = inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);
         std::thread t(clientSocketHandler, clientSocket, client_ip);

         t.detach();

      }

      Sleep(100);

   }

}

int main()
{
    // Initialze winsock
    WSADATA wsData;
    WORD ver = MAKEWORD(2, 2);

    int wsOk = WSAStartup(ver, &wsData);

    if (wsOk != 0)
    {
        std::cerr << "Can't Initialize winsock! Quitting..." << std::endl;

        return 1;
    }

    // Create a socket
    SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    if (serverSocket == INVALID_SOCKET)
    {
        WSACleanup();
        std::cerr << "Can't create a socket! Quitting..." << std::endl;

        return 1;
    }

    // Start listening for connections
    waitForConnections(serverSocket);

    // Cleanup winsock
    WSACleanup();

    system("pause");

    return 0;

}