[英]inet_ntop() not printing url
I believe the inet_ntop() is supposed to convert a struct sockaddr address to a string, currently I have :我相信 inet_ntop() 应该将 struct sockaddr 地址转换为字符串,目前我有:
sin_size = sizeof their_addr;
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)&their_addr)->sin6_addr), s, sizeof s);
printf("server: got connection from %s\n", s);
If I make a connection from http://localhost:3490/thispage.html, I expect the code to print out http://localhost:3490/thispage.html but all it prints out is "server: got connection from ::1".如果我从 http://localhost:3490/thispage.html 建立连接,我希望代码打印出 http://localhost:3490/thispage.html 但它打印出来的只是“server: got connection from :: 1”。
I don't understand why this is happening - basically what I'm trying to do is get the html page name from the url and I wanted to extract it from the returned string, but the string is not returning what I was expected.我不明白为什么会发生这种情况 - 基本上我想要做的是从 url 获取 html 页面名称,我想从返回的字符串中提取它,但该字符串没有返回我预期的内容。
The inet_ntop
function takes a structure containing an IP address and converts it to a string format. inet_ntop
函数采用包含 IP 地址的结构并将其转换为字符串格式。 It doesn't know anything about URLs.它对 URL 一无所知。
When accept
returns, all that's happened at this point is that a TCP connection was made, and their_addr
contains the IP address of the endpoint which connected, in this case ::1, which is what is printed.当
accept
返回时,此时发生的所有事情就是建立了 TCP 连接,而their_addr
包含连接的端点的 IP 地址,在本例中为 ::1,这就是打印的内容。
You need to further read from the socket, which presumably contains HTTP, to learn what URL was called.您需要进一步阅读可能包含 HTTP 的套接字,以了解调用的 URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.