标签:time mil IV amp 设置 协议 from ast smart
smartconfig 实际就是 手机端发送多播一串经过处理的数据,然后接收端,接受到这个数据之后,解析到wifi密码,连接上wifi,在实现过程中可能遇到的问题如下:
1. send socket的设置
SOCKET sendSock = socket(AF_INET,SOCK_DGRAM,0); if (sendSock == INVALID_SOCKET) { printf("socket error\n"); return -1; } int n = 1; ret = setsockopt(sendSock, IPPROTO_IP, IP_MULTICAST_TTL, (char*)&n, sizeof(n)); if (0 != ret) { printf("setsockopt error\n"); return -1; }
需要将sock设置为广播模式,在 将数据组装以后,发送数据时:
ssize_t ret = sendto(sendSock, buf, strlen(buf) + 1, 0, (sockaddr*)&addr, sizeof(sockaddr)); if (ret == SOCKET_ERROR) { printf("sendto error!\n"); }
其中地址,为你多播的地址,不同的协议发送的地址,不一样,看你的芯片。
2. 手机接受返回数据如下:
SOCKET recvSock = socket(AF_INET, SOCK_DGRAM, 0); if (INVALID_SOCKET == recvSock) { printf("socket error\n"); return -1; } sockaddr_in recvAddr; recvAddr.sin_family = AF_INET; recvAddr.sin_addr.s_addr = INADDR_ANY; recvAddr.sin_port = htons(8068); int ret = bind(recvSock, (sockaddr*)&recvAddr, sizeof(recvAddr)); if (ret != 0) { printf("bind error\n"); return -1; } // o”???è≤??≤ ip_mreq multiCast; multiCast.imr_interface.s_addr = INADDR_ANY; multiCast.imr_multiaddr.s_addr = inet_addr("236.236.238.50"); ret = setsockopt(recvSock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&multiCast, sizeof(multiCast)); if (0 != ret) { printf("setsockopt error\n"); return -1; }
socket的创建;
socket接受时的处理:
do { timeout.tv_sec=0; timeout.tv_usec=500000; FD_ZERO(&readset); FD_SET(recvSock,&readset); int result=select(recvSock+1,&readset,NULL,NULL,&timeout); printf("the result is %d\n",result); if(FD_ISSET(recvSock,&readset)>0) { socklen_t len = sizeof(sockaddr); char buf[100] = {0}; printf("start recv\n"); ssize_t ret = recvfrom(recvSock, buf, sizeof(buf) - 1, 0, (sockaddr*)&recvAddr, &len); if (ret == SOCKET_ERROR) { printf("recvfrom error!\n"); return -1; } printf("recv len is %d\n,the data is %s\n the strlen is %ld",len,buf,strlen(buf));
//处理数据 }while(1)
标签:time mil IV amp 设置 协议 from ast smart
原文地址:https://www.cnblogs.com/RoysPhoneBlog/p/9188514.html