WLG/utility/tcp_cgi.cpp

60 lines
1.7 KiB
C++
Raw Normal View History

2024-10-22 20:56:21 +08:00
#include "tcp_cgi.hpp"
2024-10-22 19:04:25 +08:00
2024-10-23 19:51:01 +08:00
TcpCgi::TcpCgi() { zlog_info(zbt,"TcpCgi Init"); }
2024-10-22 19:04:25 +08:00
void TcpCgi::startCgiServer() {
int listenfd, connfd;
int mw_optval = 1;
struct sockaddr_in servaddr;
char buff[40960];
int n;
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
2024-10-23 19:51:01 +08:00
zlog_info(zct,"create socket error: %s(errno: %d)", strerror(errno), errno);
2024-10-22 19:04:25 +08:00
return;
}
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (char *)&mw_optval, sizeof(mw_optval));
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(7305);
if (bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) {
2024-10-23 19:51:01 +08:00
zlog_info(zct,"bind socket error: %s(errno: %d)", strerror(errno), errno);
2024-10-22 19:04:25 +08:00
return;
}
if (listen(listenfd, 10) == -1) {
2024-10-23 19:51:01 +08:00
zlog_info(zct,"listen socket error: %s(errno: %d)", strerror(errno), errno);
2024-10-22 19:04:25 +08:00
return;
}
while (1) {
if ((connfd = accept(listenfd, (struct sockaddr *)NULL, NULL)) == -1) {
2024-10-23 19:51:01 +08:00
zlog_info(zct,"accept socket error: %s(errno: %d)", strerror(errno), errno);
2024-10-22 19:04:25 +08:00
continue;
}
n = recv(connfd, buff, 40960, 0);
if (n <= 0) {
2024-10-23 19:51:01 +08:00
zlog_info(zct,("recv fail");
2024-10-22 19:04:25 +08:00
close(connfd);
} else {
buff[n] = '\0';
std::string recvData = std::string(buff);
std::string reqData = cidwServer->HandleCgi_cmd(recvData);
if (send(connfd, reqData.c_str(), reqData.length(), 0) < 0) {
2024-10-23 19:51:01 +08:00
zlog_error(zct,"send msg error: %s(errno: %d)", strerror(errno), errno);
2024-10-22 19:04:25 +08:00
}
close(connfd);
}
}
close(listenfd);
return;
}
TcpCgi::~TcpCgi() {}