sin_port中的getaddrinfo返回错误 [英] sin_port returned in getaddrinfo wrong

查看:159
本文介绍了sin_port中的getaddrinfo返回错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个服务器 - 客户端程序,并在服务器中我使用getaddrinfo和getsockname在服务器上,以获取有关本地IP地址和本地绑定端口号的信息。
使用这个信息,我开始我的客户端程序,使用的getaddrinfo,然后就打印出servinfo数据结构中的返回值:
    的getaddrinfo(argc个[1],的argc [2],&放大器;提示,&放大器; servinfo); >>服务器主机名和服务器端口号通过命令行通过。

I am writing a server-client program and in the server I use getaddrinfo and getsockname on the server to get info about the local IP addr and locally bound port number . Using this info, I start my client program and use the getaddrinfo and then just print out the returned values in the servinfo data structure: getaddrinfo(argc[1], argc[2], &hints, &servinfo); >> server hostname and server port number are passed via command line.

不过,我注意到在servinfo的sin_port是不是我通过命令行传递的端口。
1)这是否返回的getaddrinfo正在使用的客户端的源端口号的端口号?
2)失败的的getaddrinfo和插座的来电之后,我的连接调用。我不知道为什么。如何调试呢?

But I notice that the sin_port in servinfo is not the port I am passing via the command line. 1) Does this getaddrinfo return the port number being used by the client as the source port number ? 2) My connect call after the getaddrinfo and socket calls in failing. I do not know why. How do I debug it ?

我的客户code片断:

My client code snippet:

memset(&hints, 0 ,sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags =  AI_CANONNAME | AI_NUMERICSERV;
getaddrinfo(argc[1], argc[2], &hints, &servinfo);

for (p = servinfo till p!=NULL)
    sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)
    connect(sockfd, p->ai_addr, p->ai_addrlen) >>>>> Connect not going through.

我开始我的客户端程序是这样的:
./a.out myservername 18844

I start my client program like this: ./a.out myservername 18844

谢谢!

推荐答案

新的答案:您与只有一个参数调用程序,所以的argv [1] 包含18844的argv [2] 是一个空指针。这是要尝试连接到使用数字IP 18844和未指定的端口号(这将最终被0和失败的主机)。

New answer: You are invoking your program with only one argument, so argv[1] contains "18844" and argv[2] is a null pointer. This is going to try to connect to a host with numeric IP 18844 and an unspecified port number (which will end up being 0 and failing).

旧的答案:(相关,但不是你的问题) sin_port 和整个 SOCKADDR_IN 结构中的网络字节顺序的。你需要的端口转换与 ntohl 来使用它作为一个数字,但你会过得更好从未触及的sockaddr 结构内部任何责任。相反,使用则getnameinfo NI_NUMERICHOST NI_NUMERICSER​​V 来获得解决回一个基于字符串的数字形式,那么与strtol 读端口(服务)的数量成一个整数。这个工程即使对于非IPv4网络地址/协议族(如IPv6),不需要额外的code支持新的。

Old answer: (relevant but not your problem) sin_port and the whole sockaddr_in structure is in network byte order. You'll need to convert the port with ntohl to use it as a number, but you would be better off never touching sockaddr structures' internals whatsoever. Instead, use getnameinfo with NI_NUMERICHOST and NI_NUMERICSERV to get the address back into a string-based numeric form, then strtol to read the port ("service") number into an integer. This works even for non-IPv4 network address/protocol families (like IPv6) and requires no additional code for supporting new ones.

这篇关于sin_port中的getaddrinfo返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆