void* StartDiscoveryThread(void *arg)
{
int so_broadcast=1;
struct sockaddr_in toAddr; //客户端的网络地址。
struct sockaddr_in serverAddr; //服务器端的网络地址。
char inBuff[DD_BUFF_LEN];
int sockaddrLen = 0;
int inLen;
int ret;
NETPARAM stRespond = {0};
int rsock = createUDPSokcet(TF6x0_PORT);
while(1)
{
sockaddrLen = sizeof(toAddr);
inLen = recvfrom(rsock, inBuff, DD_BUFF_LEN, 0, (struct sockaddr*)&toAddr,(socklen_t*)&sockaddrLen);
if(inLen<=0)
{
DD_DEBUG("recv failed! skip, continue ...");
sleep(1);
}
else
{
ret = memcmp(inBuff,CONST_MESSAGE,strlen(CONST_MESSAGE));
if(0!=ret)
{
continue;
}
else
{
填充需要发送的信息
netGetIPAddr(DD_IFace, &(stRespond.ip));
stRespond.dev_model = 1111;
///toAddr.sin_addr.s_addr = inet_addr("255.255.255.255");
///toAddr.sin_port = htons(TF6x0_PORT);
sockaddrLen = sizeof(struct sockaddr);
ret = sendto(rsock,&stRespond,sizeof(stRespond),0,(struct sockaddr*)&toAddr, sizeof(toAddr));
if(ret < 0)
{
printf("send failed!\n");
}
else
{
printf("server sned to client(ip:%s port:%d)success ret=%d %d\n", inet_ntoa(toAddr.sin_addr),ntohs(toAddr.sin_port), ret, __LINE__);
continue;
}
}
}
sleep(3);
}
close(rsock);
///close(wsock);
return NULL;
}
int DiscoveryClientHandle(std::list &lis)
{
int sock = -1, retVal = -1;
int fBroadcast;
struct sockaddr_in addrSrc;
struct sockaddr_in addrDst;
int sockaddrLen = sizeof(struct sockaddr_in);
NETPARAM resp;
char buf[1024] = {0};
if(sock<0)
{
sock = createUDPSokcet(LOCAL_PORT);
memset(&addrSrc, 0, sockaddrLen);
addrSrc.sin_family = AF_INET;
addrSrc.sin_addr.s_addr = htonl(INADDR_BROADCAST);
addrSrc.sin_port = htons(LOCAL_PORT);
memset(&addrDst, 0, sockaddrLen);
addrDst.sin_family = AF_INET;
addrDst.sin_addr.s_addr = htonl(INADDR_BROADCAST);
addrDst.sin_port = htons(TF6x0_PORT);
retVal = sendto(sock, CONST_MESSAGE,strlen(CONST_MESSAGE),0,(struct sockaddr*)&addrDst, sizeof(addrDst));
if (retVal <0)
{
printf("DiscoveryClientHandle send faile\n");
}
else
{
do
{
sockaddrLen = sizeof(addrSrc);
printf("client send %s to(ip:%s, prot:%d) success, now accept the server info line:%d \n",CONST_MESSAGE, inet_ntoa(addrDst.sin_addr),ntohs(addrDst.sin_port), __LINE__);
retVal = recvfrom(sock, buf, 1024, 0, (struct sockaddr *)&addrSrc, (socklen_t*)&sockaddrLen);
if(-1 == retVal)
{
printf("recvfrom error------------------\n");
break;
}
else
{
printf("get ip(%d.%d.%d.%d) dev_model=%d!\r\n", (resp.ip)&0xff, (resp.ip>>8)&0xff, (resp.ip>>16)&0xff, (resp.ip>>24)&0xff,resp.dev_model);
lis.push_back(resp);
}
usleep(1000);
}while(1);
}
}
close(sock);
return 1;
}
int devDiscovery(void)
{
int ret;
if(0!=Discoveryid) return -1;
ret= pthread_create(&Discoveryid,0,StartDiscoveryThread,0);
return ret;
}
上面代码有一个是个服务端 被搜索的代码,客户端广播一个搜索信号,如果设备接收到客户端的搜索信息,就把设备的ip以及一些信息发送到客户端,客户端对这些信息进行保存,说白了就是一个搜索设备的两端,现在的问题是客户端接收不到信息,一直卡在recvfrom这里,我这里暂时没做超时处理和非阻塞。我想问这里到底什么问题???????????