为什么第一个客户看到有0.0.0.0的源IP? [英] Why the first client sees to have source ip of 0.0.0.0?

查看:865
本文介绍了为什么第一个客户看到有0.0.0.0的源IP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 client.c server.c 在Linux上。两个我初始化一个socket:

  =的sockfd插座(AF_INET,SOCK_DGRAM,IPPROTO_UDP)

在服务器我补充:

  listen_addr.sin_family = AF_INET;
listen_addr.sin_port = htons(端口);
listen_adrr.sin_addr.s_addr = htonl(INADDR_ANY);

server.c 通话(阻塞方式),以 recvform

 如果(recvfrom的(的sockfd,buf_get,BUFLEN,0,(结构sockaddr *)及talker_addr,&安培; SLEN)== -1)
            犯错(recvfrom的());

以及 client.c 发送数据包:

 如果(SENDTO(的sockfd,buf_sent,BUFLEN,0,(结构sockaddr *)及serv_addr,SLEN)== -1)
        犯错(SENDTO());


  1. 的问题是,从第一个呼召 SENDTO
    client.c ,服务器看到客户端的IP为 0.0.0.0 ,后
    ,在第二次,第三次......调用 client.c 获得IP和
    有一个合法的IP,如 127.0.0.3:3212

  2. 另一个奇怪的是,如果我启动第二个新的客户端它会
    从第一次IP。


解决方案

请确保你调用recvfrom的之前要设置SLEN到talker_addr结构的大小。这将设置的值(这也许可以解释为什么它在后续调用工作)在recvfrom的,但如果有一个不好的初始值,你可能会得到垃圾第一个电话。

  = SLEN的sizeof(结构SOCKADDR_IN);

I have a client.c server.c on linux. on both I init a socket:

sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)

in the server I add:

listen_addr.sin_family = AF_INET;
listen_addr.sin_port = htons(port);
listen_adrr.sin_addr.s_addr = htonl(INADDR_ANY);

the server.c calls (blocking way) to recvform:

if (recvfrom(sockfd, buf_get, BUFLEN, 0, (struct sockaddr*)&talker_addr, &slen) == -1)
            err("recvfrom()");

And the client.c sends packets with:

if (sendto(sockfd, buf_sent, BUFLEN, 0, (struct sockaddr*)&serv_addr, slen) == -1)
        err("sendto()");

  1. The problem is that on the first calling to sendto from client.c, the servers sees the client's ip as 0.0.0.0, after that on the second, third,... calls the client.c get an ip and have a legal ip such as 127.0.0.3:3212.
  2. another weird thing is that if I start a second new client it gets ip from the first time.

解决方案

Make sure you are setting slen to the size of the talker_addr struct before you call recvfrom. It will set the value (which may explain why it works in subsequent calls) in recvfrom but if there is a bad initial value, you may get garbage the first call.

slen = sizeof(struct sockaddr_in);

这篇关于为什么第一个客户看到有0.0.0.0的源IP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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