您好,为什么recvfrom函数得到的IP地址不对?

#define SERVER_PORT_NUM 5002
#define REQUEST_MSG_SIZE 1024
struct request
{
int display;
char message[REQUEST_MSG_SIZE];
};
#include "vxworks.h"
#include "sockLib.h"
#include "inetLib.h"
#include "stdioLib.h"
#include "strLib.h"
#include "hostLib.h"
#include "ioLib.h"

STATUS udpServer()
{
struct sockaddr_in serverAddr;
struct sockaddr_in clientAddr;
struct request clientRequest;
int sockAddrSize_s;
int sockAddrSize_c;
int sFd;
char inetAddr[1024];
sockAddrSize_s=sizeof(struct sockaddr_in);
sockAddrSize_c=sizeof(struct sockaddr_in);
printf("size =%d\n",sockAddrSize_c);
bzero((char*)&serverAddr,sockAddrSize_s);
/*serverAddr.sin_len=(u_char)sockAddrSize_s;*/
serverAddr.sin_family=AF_INET;
serverAddr.sin_port=htons(SERVER_PORT_NUM);
serverAddr.sin_addr.s_addr=inet_addr("0.0.0.0"); 
 if((sFd=socket(AF_INET,SOCK_DGRAM,0))==ERROR)
{
perror("error in create server socket!\n");
return(ERROR);
}
bind(sFd,(struct sockaddr*)&serverAddr,sockAddrSize_s);
printf("waitting...\n");
for(;;)
{
recvfrom(sFd,(char*)&clientRequest,sizeof(clientRequest),0,(struct sockaddr*)&clientAddr,&sockAddrSize_c);
if(clientRequest.display)
{
inet_ntoa_b(clientAddr.sin_addr,inetAddr);
printf("MSG FROM CLIENT (internet address %s ,port %d):\n%s\n",inet_ntoa(clientAddr.sin_addr),ntohs(clientAddr.sin_port),clientRequest.message);
}

}
}
服务器是在目标机上运行的,目标机的IP是:169.254.43.254,主机(客户端)是:169.254.43.215,
但是通过recvfrom得到的IP地址却是169.254.43.254,而不是169.254.43.215???请教大家了,忙了一下午都没解决这个问题。谢谢了!!!

阿晨1998
浏览 460回答 2
2回答

繁花不似锦

while(1){recvfrom(sockfd, readbuff, UDP_CMD_BUFF_LENGTH - 1, 0, (struct sockaddr *)&Glob_remot_88_port_add, &len);PRINT("[recevudp]len %d IP is %s\n", len, inet_ntoa(Glob_remot_88_port_add.sin_addr));}这个函数, 第一次接受打印出的发送方IP地址总是0.0.0.0;解决方法:在recvfrom之前加一句: len = sizeof(struct sockaddr);即可

慕森王

169.254.x.x的地址是没设定IP地址时,机器产生的伪地址。给网卡真正设个地址再看看
打开App,查看更多内容
随时随地看视频慕课网APP