我有如下测试程序,加载了一个任务如下,与PC机测试程序通讯,发现装置接收没有问题,但发送有问题,奇怪的是sendto返回的发送数据个数也对,但我的PC就是收不到.UDP通讯,IP地址肯定正确,双方都使用9999端口.请大家分析一下
void subTask()
{
int rc, len;
SOCKET gHSocket;
struct sockaddr_in toAddr;
struct sockaddr_in selfAddr;
memset( &selfAddr, 0, sizeof(selfAddr) );
selfAddr.sin_family = AF_INET;
selfAddr.sin_addr.s_addr = htonl(INADDR_ANY);
selfAddr.sin_port = 9999;
gHSocket = socket(AF_INET, SOCK_DGRAM, 0);
if( gHSocket == -1 )
return;
if( bind(gHSocket, (struct sockaddr *)&selfAddr, sizeof(selfAddr)) == -1 )
return;
for(;;)
{
taskDelay(10);
len = 0;
rc = ioctl(gHSocket, FIONREAD, (int )&len);
if( len > 0 )
{
toAddr.sin_family = AF_INET;
toAddr.sin_port = htons(9999);
toAddr.sin_addr.s_addr = htonl(INADDR_ANY);
len = sizeof(toAddr);
rc = recvfrom( gHSocket, (char *)gRxdBuf, sizeof(gRxdBuf), 0, (struct sockaddr *)&toAddr, &len );
if( rc > 0 )
{
//处理...
}
continue;
}
if( gTxdNum )
{
toAddr.sin_family = AF_INET;
toAddr.sin_addr.s_addr = htonl(0xc0a8060c);
toAddr.sin_port = htons(9999);
rc = sendto( gHSocket, (char *)gTxdBuf, gTxdNum, 0, (struct sockaddr *)&toAddr, sizeof(toAddr) );
if( rc > 0 )
gTxdNum = 0;
continue;
}
}
}