C++套接字:当客户端调用connect()时,accept()挂起,但accept(()响应HTTPGET请求



我正在尝试用C++编写一个演示服务器/客户端程序。我首先在我的Macbook上运行服务器程序,打开ngrok,并将互联网上的公共地址转发到我机器上的本地地址。当我尝试运行客户端程序连接到服务器时,我看到了一些我不理解的东西:

  1. 如果客户端尝试在服务器程序中定义的本地端口连接到localhost,则服务器按预期接受,客户端成功连接
  2. 如果客户端尝试连接到端口80处的ngrok服务器地址(ngrok的默认地址(,则客户端连接,但服务器在接受调用时仍被阻止。(我不明白!(
  3. 如果我向ngrok服务器地址发送HTTP GET请求,则服务器成功地接受了连接

为什么我会看到这些?在理想的情况下,我希望我的服务器接受来自客户端程序的连接,而不仅仅是响应HTTPGET请求。

如果有帮助的话,下面是我的代码:对于客户端,

#include "helpers.hh"
#include <cstdio>
#include <netdb.h>
// usage: -h [host] -p [port]
int main(int argc, char** argv) {
const char* host = "x.xx.xx.xx"; // use the server's ip here.
const char* port = "80";
// parse arguments
int opt;
while ((opt = getopt(argc, argv, "h:p:")) >= 0) {
if (opt == 'h') {
host = optarg;
} else if (opt == 'p') {
port = optarg;
}
}
// look up host and port
struct addrinfo hints, *ais;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;        // use IPv4 or IPv6
hints.ai_socktype = SOCK_STREAM;    // use TCP
hints.ai_flags = AI_NUMERICSERV;
if (strcmp(host, "ngrok") == 0) {
host = "xxxx-xxxx-xxxx-1011-2006-00-27b9.ngrok.io";
}
int r = getaddrinfo(host, port, &hints, &ais);
if (r != 0) {
fprintf(stderr, "getaddrinfo: %sn", gai_strerror(r));
exit(1);
}
// connect to server
int fd = -1;
for (auto ai = ais; ai && fd < 0; ai = ai->ai_next) {
fd = socket(ai->ai_family, ai->ai_socktype, 0);
if (fd < 0) {
perror("socket");
exit(1);
}
r = connect(fd, ai->ai_addr, ai->ai_addrlen);
if (r < 0) {
close(fd);
fd = -1;
}
}
if (fd < 0) {
perror("connect");
exit(1);
}
freeaddrinfo(ais);
// 
printf("Connection established at fd %dn", fd);
FILE* f = fdopen(fd, "a+");
fwrite("!", 1, 1, f);
fclose(f);
while (true) {
}

}

对于服务器:

#include "helpers.hh"
void handle_connection(int cfd, std::string remote) {
(void) remote;
printf("Received incoming connection at cfd: %dn", cfd);
usleep(1000000);
printf("Exitingn");
}

int main(int argc, char** argv) {
int port = 6162;
if (argc >= 2) {
port = strtol(argv[1], nullptr, 0);
assert(port > 0 && port <= 65535);
}
// Prepare listening socket
int fd = open_listen_socket(port);
assert(fd >= 0);
fprintf(stderr, "Listening on port %d...n", port);
while (true) {
struct sockaddr addr;
socklen_t addrlen = sizeof(addr);
// Accept connection on listening socket
int cfd = accept(fd, &addr, &addrlen);
if (cfd < 0) {
perror("accept");
exit(1);
}
// Handle connection
handle_connection(cfd, unparse_sockaddr(&addr, addrlen));
}
}

与在本地路由器中进行的典型端口转发相反,ngrok不是传输级别(TCP(的端口转发器,而是HTTP级别的请求转发器。

因此,如果客户端通过TCP连接到外部ngrok服务器,则不会转发任何内容。只有在客户端发送HTTP请求后,才会确定目的地,然后将此请求发送到内部机器上的ngrok连接器,后者将启动与内部服务器的连接并转发请求。

最新更新