当前位置: 技术问答>linux和unix
关于socket编程中inet_ntoa的怪异问题
来源: 互联网 发布时间:2017-01-04
本文导语: 今天在研究socket编程的时候,应用inet_ntoa,发现了一个异常奇怪的问题, 描述如下: 在当前terminal里用阻塞方式打起socket,另一个termianl用telnet连接,然后在当前 terminal里面打印出连接的ip和port。 当前terminal的代码...
今天在研究socket编程的时候,应用inet_ntoa,发现了一个异常奇怪的问题, 描述如下:
在当前terminal里用阻塞方式打起socket,另一个termianl用telnet连接,然后在当前
terminal里面打印出连接的ip和port。
当前terminal的代码如下:
#include
#include
#include
#include
#define MYPORT 8888
int main()
{
int sockid, newsockid;
struct sockaddr_in ina;
struct sockaddr dst;
int len;
char d[20];
char *d1;
if((sockid = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
return -1;
}
ina.sin_family = AF_INET;
ina.sin_port = htons(MYPORT);
ina.sin_addr.s_addr = htonl(INADDR_ANY);
bzero(&(ina.sin_zero), 8);
if((bind(sockid, (struct sockaddr *)&ina, sizeof(struct sockaddr))) == -1)
{
perror("bind");
return -1;
}
if((listen(sockid, 10)) == -1)
{
perror("listen");
return -1;
}
if((newsockid = accept(sockid, &dst, &len)) == -1)
{
perror("accept");
return -1;
}
d1 = inet_ntoa(((struct sockaddr_in *)&dst)->sin_addr);
printf("got a connection from %s, port is %d, len is %dn", d1, ntohs(((struct sockaddr_in *)&dst)->sin_port), len);
//printf("got a connection from %s, port is %d, len is %dn", inet_ntoa(((struct sockaddr_in *)&dst)->sin_addr), ntohs(((struct sockaddr_in *)&dst)->sin_port), len);
//inet_ntop(AF_INET, &(((struct sockaddr_in *)&dst)->sin_addr), d, 20);
//printf("got a connection from %s, port is %d, len is %dn", d, ntohs(((struct sockaddr_in *)&dst)->sin_port), len);
return 0;
}
然后在另外一个窗口:telnet 127.0.0.1 8888
问题出现了,只有在我解注掉这一行代码的时候,
//printf("got a connection from %s, port is %d, len is %dn", inet_ntoa(((struct sockaddr_in *)&dst)->sin_addr), ntohs(((struct sockaddr_in *)&dst)->sin_port), len);
我才能得到正确的值:
got a connection from 127.0.0.1, port is 37448, len is 16
got a connection from 127.0.0.1, port is 37448, len is 16
否则无论如何,都不能得到正确的结果:
got a connection from 184.31.131.191, port is 1032, len is 16
但是我认为这两句话本身是等效的阿:
d1 = inet_ntoa(((struct sockaddr_in *)&dst)->sin_addr);
printf("got a connection from %s, port is %d, len is %dn", d1, ntohs(((struct sockaddr_in *)&dst)->sin_port), len);
//printf("got a connection from %s, port is %d, len is %dn", inet_ntoa(((struct sockaddr_in *)&dst)->sin_addr), ntohs(((struct sockaddr_in *)&dst)->sin_port), len);
哪位大侠能帮助看一下~多多感谢~!
在当前terminal里用阻塞方式打起socket,另一个termianl用telnet连接,然后在当前
terminal里面打印出连接的ip和port。
当前terminal的代码如下:
#include
#include
#include
#include
#define MYPORT 8888
int main()
{
int sockid, newsockid;
struct sockaddr_in ina;
struct sockaddr dst;
int len;
char d[20];
char *d1;
if((sockid = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
return -1;
}
ina.sin_family = AF_INET;
ina.sin_port = htons(MYPORT);
ina.sin_addr.s_addr = htonl(INADDR_ANY);
bzero(&(ina.sin_zero), 8);
if((bind(sockid, (struct sockaddr *)&ina, sizeof(struct sockaddr))) == -1)
{
perror("bind");
return -1;
}
if((listen(sockid, 10)) == -1)
{
perror("listen");
return -1;
}
if((newsockid = accept(sockid, &dst, &len)) == -1)
{
perror("accept");
return -1;
}
d1 = inet_ntoa(((struct sockaddr_in *)&dst)->sin_addr);
printf("got a connection from %s, port is %d, len is %dn", d1, ntohs(((struct sockaddr_in *)&dst)->sin_port), len);
//printf("got a connection from %s, port is %d, len is %dn", inet_ntoa(((struct sockaddr_in *)&dst)->sin_addr), ntohs(((struct sockaddr_in *)&dst)->sin_port), len);
//inet_ntop(AF_INET, &(((struct sockaddr_in *)&dst)->sin_addr), d, 20);
//printf("got a connection from %s, port is %d, len is %dn", d, ntohs(((struct sockaddr_in *)&dst)->sin_port), len);
return 0;
}
然后在另外一个窗口:telnet 127.0.0.1 8888
问题出现了,只有在我解注掉这一行代码的时候,
//printf("got a connection from %s, port is %d, len is %dn", inet_ntoa(((struct sockaddr_in *)&dst)->sin_addr), ntohs(((struct sockaddr_in *)&dst)->sin_port), len);
我才能得到正确的值:
got a connection from 127.0.0.1, port is 37448, len is 16
got a connection from 127.0.0.1, port is 37448, len is 16
否则无论如何,都不能得到正确的结果:
got a connection from 184.31.131.191, port is 1032, len is 16
但是我认为这两句话本身是等效的阿:
d1 = inet_ntoa(((struct sockaddr_in *)&dst)->sin_addr);
printf("got a connection from %s, port is %d, len is %dn", d1, ntohs(((struct sockaddr_in *)&dst)->sin_port), len);
//printf("got a connection from %s, port is %d, len is %dn", inet_ntoa(((struct sockaddr_in *)&dst)->sin_addr), ntohs(((struct sockaddr_in *)&dst)->sin_port), len);
哪位大侠能帮助看一下~多多感谢~!
|
int
accept(int s, struct sockaddr * restrict addr,
socklen_t * restrict addrlen);
The argument addr is a result argument that is filled-in with the address
of the connecting entity, as known to the communications layer. The
exact format of the addr argument is determined by the domain in which
the communication is occurring. A null pointer may be specified for addr
if the address information is not desired; in this case, addrlen is not
used and should also be null. Otherwise, the addrlen argument is a
value-result argument; it should initially contain the amount of space
pointed to by addr; on return it will contain the actual length (in
bytes) of the address returned. This call is used with connection-based
socket types, currently with SOCK_STREAM.
if((newsockid = accept(sockid, &dst, &len)) == -1)
==>
len = sizeof(dst);
if((newsockid = accept(sockid, &dst, &len)) == -1)