我想使用UDP向环境为vxworks的可视盒中发送一条消息,我测试我的客户端代码是否运行良好,计算机和可视盒可以相互ping通。但是,select()函数始终在我的接收器中返回0 ,我必须关闭防火墙
这是我的代码:
/* Create socket */
if ((sFd = socket (AF_INET, SOCK_DGRAM, 0)) == ERROR)
{
perror ("socket");
close (sFd);
return (ERROR);
}
/* Build socket address */
bzero ((struct sockaddr_in *) &myAddr, sockAddrSize);
myAddr.sin_family = AF_INET;
myAddr.sin_port = htons (8882); //vxport
myAddr.sin_addr.s_addr = htonl(INADDR_ANY); //local ip
bzero ((struct sockaddr_in *) &remoteAddr, sockAddrSize);
remoteAddr.sin_family = AF_INET;
remoteAddr.sin_port = htons (8881); //vs port
remoteAddr.sin_addr.s_addr = inet_addr ("192.168.73.1"); //vs IP
/* Bind socket to local address */
if (bind (sFd, (struct sockaddr *) &myAddr, sockAddrSize) == ERROR)
{
perror ("bind");
close (sFd);
return (ERROR);
}
memset(recvBuff,0,sizeof(recvBuff)); //clear the receiver buffer
while(1)
{
FD_ZERO(&rfd);
FD_SET(sFd,&rfd);
printf("sFd = %d\n",sFd);
rc = select(sFd + 1,&rfd,NULL,NULL,&wait);
if(rc == 0){
printf("time out\n");
// return -1;
}
else if(rc <0/*== ERROR*/){
perror("select()");
break;
}
else if(FD_ISSET(sFd,&rfd)){
if ((nbytes = recvfrom (sFd, recvBuff, sizeof (recvBuff), 0,
(struct sockaddr *) &remoteAddr,&sockAddrSize)) < 0)
{
perror ("recvfrom");
close (sFd);
return (ERROR);
}
else{
recvBuff[nbytes] = "\0";
printf("is recieved the message\n");
}
}
}
close (sFd); }
有人可以找到问题所在吗?非常感谢..