-1

我需要帮助来组织新线程中传入连接的每个请求处理(代码在本主题的底部)。

我根本不知道如何手动组织(不使用 boost/threadpool)队列来处理每个请求?我应该如何解决不使用 boost 等的问题?

原因,我想手动做,我不明白下一步怎么做:

  • 监听每个新连接
  • 如果我有新连接,则在新线程中发送处理
  • 当线程结束处理过程时,关闭该线程

我试图用while(true); 来做这样的事情;但不知道如何组织好请求队列来处理每个 HTTP 请求。

我的代码是:

#include <iostream>
#include <Windows.h>

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

typedef struct Header
{
friend struct Net;

private:
    WORD wsa_version;
    WSAData wsa_data;

    SOCKET sock;
    SOCKADDR_IN service;

    char *ip;
    unsigned short port;

public:
    Header(void)
    {
        wsa_version = 0x202;

        ip = "0x7f.0.0.1";
        port = 0x51;

        service.sin_family = AF_INET;
        service.sin_addr.s_addr = inet_addr(ip);
        service.sin_port = htons(port);
    }

} Header;

typedef struct Net
{
private:
    int result;

    HANDLE thrd;
    DWORD exit_code;

    void WSAInit(WSAData *data, WORD *wsa_version)
    {
        result = WSAStartup(*wsa_version, &(*data));

        if(result != NO_ERROR)
        {
            std::cout << "WSAStartup() failed with the error: " << result << std::endl;
        }
        else
        {
            std::cout << (*data).szDescription << " " << (*data).szSystemStatus << std::endl;
        }
    }

    void SocketInit(SOCKET *my_socket)
    {
        (*my_socket) = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

        if((*my_socket) == INVALID_SOCKET)
        {
            std::cout << "Socket initialization failed with the error: " << WSAGetLastError() << std::endl;
            WSACleanup();
        }
        else
        {
            std::cout << "Socket initialization successful!" << std::endl;
        }
    }

    void SocketBind(SOCKET *my_socket, SOCKADDR_IN *service)
    {
        result = bind((*my_socket), (SOCKADDR*)&(*service), sizeof(*service));

        if(result == SOCKET_ERROR)
        {
            std::cout << "Socket binding failed with the error: " << WSAGetLastError() << std::endl;
            closesocket((*my_socket));
            WSACleanup();
        }
        else
        {
            std::cout << "Socket binding successful!" << std::endl;
        }

        result = listen(*my_socket, SOMAXCONN);

        if(result == SOCKET_ERROR)
        {
            std::cout << "Socket listening failed with the error: " << WSAGetLastError() << std::endl;
        }
        else
        {
            std::cout << "Listening to the socket..." << std::endl;
        }
    }

    void SocketAccept(SOCKET *my_socket)
    {
        SOCKET sock_accept = accept((*my_socket), 0, 0);

        if(sock_accept == INVALID_SOCKET)
        {
            std::cout << "Accept failed with the error: " << WSAGetLastError() << std::endl;
            closesocket(*my_socket);
            WSACleanup();
        }
        else
        {
            std::cout << "Client socket connected!" << std::endl;

            thrd = CreateThread(NULL, 0, &Net::Threading, &sock_accept, 0, NULL);
        }
    }

    static void HandleRequest(char response[], int length)
    {
        std::cout << std::endl;

        for(int i = 0; i < length; i++)
        {
            std::cout << response[i];
        }

        std::cout << std::endl;
    }

    static DWORD WINAPI Threading(LPVOID lpParam)
    {
        SOCKET *my_socket = (SOCKET*)lpParam;

        char data[0x400];
        int result = recv((*my_socket), data, sizeof(data), 0);
        HandleRequest(data, result);
        char *response = "HTTP/1.1 200 OK\r\nServer: Amegas.sys-IS/1.0\r\nContent-type: text/html\r\nSet-Cookie: ASD643DUQE7423HFDG; path=/\r\nCache-control: private\r\n\r\n<h1>Hello World!</h1>\r\n\r\n";
        result = send((*my_socket), response, (int)strlen(response), 0);

        if(result == SOCKET_ERROR)
        {
            std::cout << "Sending data via socket failed with the error: " << WSAGetLastError() << std::endl;
            closesocket((*my_socket));
            WSACleanup();
        }
        else
        {
            result = shutdown((*my_socket), 2);
        }

        return 0;
    }

public:
    Net(void)
    {
        Header *obj_h = new Header();

        WSAInit(&obj_h->wsa_data, &obj_h->wsa_version);

        SocketInit(&obj_h->sock);
        SocketBind(&obj_h->sock, &obj_h->service);

        SocketAccept(&obj_h->sock);

        delete obj_h;
    }
} Net;

int main(void)
{
    Net *obj_net = new Net();

    delete obj_net;

    return 0;
}
4

1 回答 1

2

您的操作系统将处理 accept() 排队 - 不要太担心它。简单的同步服务器倾向于这样运行:

socket listeningSocket:=socket.create;
listeningSocket.bind('0.0.0.0',80);  // address/port
listeningSocket.listen;

while(true){
  socket serverClientSocket=accept(listeningSocket);
  createThread(&serverClientThread,serverClientSocket);
}

void serverClientThread(void *param)
{
  inBuffer char[256];
  socket myServerClientSocket=(socket)param;
  while(true){
     int bytesRx=recv(myServerClientSocket,&inBuffer,size(inBuffer));
     if (bytesRx>0){
        if doSomethingWith(&inBuffer,bytesRx) // not necessarily size(inBuffer) bytes!!
        {
           send(myServerClientSocket,"Reply from server\r\n");
        }

     }
     else
       return; // on error or connection closed
  }
}

一个监听线程(可以是控制台应用程序中的主线程)运行 accept() 循环。单独的 serverClientThread 实例一直运行,直到它们的客户端断开连接或发生其他错误。

于 2012-04-29T22:09:41.270 回答