我目前正在学习有关互联网的知识。我正在尝试设置一个简单的代理服务器,它只是将一个请求从服务器端转发到它的客户端。我目前正在关注this教程。这就是我所学到的:
#define MYPORT "3490" // the port users will be connecting to
#define BACKLOG 10 // how many pending connections queue will hold
int main(int argc, const char* argv[]) {
struct addrinfo hints;
struct addrinfo *res;
int sockIn;
int sockOut;
memset(&hints, 0, sizeof hints); // make sure its empty
hints.ai_family = AF_UNSPEC; // use IPv4 or IPv6, whichever
hints.ai_socktype = SOCK_STREAM; // what kind of socket
hints.ai_flags = AI_PASSIVE; // fill in my IP for me
//listens on the hosts ip address:
getaddrinfo(NULL, MYPORT, &hints, &res);
// make a socket, bind it, and listen on it:
sockIn = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
bind(sockIn, res->ai_addr, res->ai_addrlen);
listen(sockIn, BACKLOG);
freeaddrinfo(res); // free the linked-list
struct sockaddr_storage their_addr;
socklen_t addr_size;
char buf[512];
while(1) {
addr_size = sizeof their_addr;
struct sockaddr *addr = (struct sockaddr *)&their_addr;
sockOut = accept(sockIn, addr, &addr_size);
recv(sockOut, buf, sizeof buf, 0);
for (auto ch : buf) {
cout << ch;
}
close(sockOut);
}
}现在我只是在我访问的每个页面上显示"hi“。在实现代理的客户端之前,我想显示浏览器发送给代理的服务器端的HTTP Get请求。我的问题是我不知道如何检索它。我使用的指南并没有提到这一点。
Edit:我添加了一个recv调用,该调用用于将套接字中的所有内容读取到缓冲区中。不幸的是,它什么也不能解释。
https://stackoverflow.com/questions/41940568
复制相似问题