gpt4 book ai didi

c - 如何在新线程中为每个请求处理组织队列?

转载 作者:行者123 更新时间:2023-11-30 18:59:32 24 4
gpt4 key购买 nike

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

我根本不知道如何手动组织(不使用 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;
}

最佳答案

你的操作系统将处理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 实例将运行,直到其客户端断开连接或发生其他错误。

关于c - 如何在新线程中为每个请求处理组织队列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10375530/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com