- 苏萦
-
不知道哪里不对...你对比一下.看有没有缺的吧
#include <stdio.h>
#include <string.h>
#include <Winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
typedef struct SktInfoTag
{
int skt;
unsigned int ip;
unsigned short port;
char buf[2048];
int bufsize;
}SktInfo;
static int thread_proc( LPVOID param );
SktInfo siInfo;
void main()
{
WSADATA wsaData;
unsigned long threadflag;
int ret = 0;
unsigned long unblock =1;
void* thrd = INVALID_HANDLE_VALUE;
ret = WSAStartup(MAKEWORD(2,2), &wsaData);
siInfo.ip = inet_addr("192.168.0.1");
siInfo.port = 5001;
siInfo.bufsize = 2048;
//创建socket
siInfo.skt = socket(AF_INET, SOCK_DGRAM, 0);
if( siInfo.skt == -1 )
{
printf("socket create failed! ");
goto END;
}
//非阻塞
ioctlsocket(siInfo.skt, FIONBIO, &unblock);
if( 224 <= *((unsigned char*)&siInfo.ip) && *((unsigned char*)&siInfo.ip) <= 240 )
{
struct sockaddr_in addr;
struct ip_mreq mreq;
int ttl = 127;
int flag = 1;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = siInfo.port;
//绑定本地接收端口
ret = bind( siInfo.skt, (struct sockaddr*)&addr, sizeof(addr));
if(ret < 0)
{
printf("bind socket failed! ");
goto END;
}
memset(&mreq, 0, sizeof(mreq));
mreq.imr_multiaddr.s_addr = siInfo.ip;
mreq.imr_interface.s_addr = INADDR_ANY;
//设置缺省参数
setsockopt(siInfo.skt, IPPROTO_IP, IP_MULTICAST_TTL, (char*)&ttl, sizeof(ttl));
setsockopt(siInfo.skt, IPPROTO_IP, IP_MULTICAST_LOOP, (char*)flag, sizeof(flag));
//加入组播
ret = setsockopt(siInfo.skt, IPPROTO_IP, IP_ADD_MEMBERSHIP,(char*)&mreq, sizeof(mreq));
if( ret < 0 )
{
printf("setsockopt socket failed! ");
goto END;
}
}
else
{
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = siInfo.ip;
addr.sin_port = siInfo.port;
//绑定本地接收地址端口
ret = bind(siInfo.skt, (struct sockaddr*)&addr, sizeof(addr));
if( ret < 0 )
{
printf("bind socket failed! ");
goto END;
}
}
//创建接收线程
if( INVALID_HANDLE_VALUE == (thrd = CreateThread(NULL, 1024, (LPTHREAD_START_ROUTINE)thread_proc, (LPVOID)&siInfo, 0, &threadflag)) )
{
goto END;
}
while(1)
{
Sleep(200);
}
END:
if( thrd != INVALID_HANDLE_VALUE )
{
CloseHandle(thrd);
thrd = NULL;
}
}
int thread_proc( LPVOID param )
{
fd_set rset;
int ret = 0;
int size = 0;
SktInfo* info = (SktInfo*)¶m;
struct sockaddr_in from;
int slen = sizeof(from);
struct timeval tv;
unsigned char *recvip;
unsigned short *recvport;
int timeout = 100;
if( info->skt == -1)
goto EXIT_END;
tv.tv_sec = timeout/1000;
tv.tv_usec = (timeout%1000)*1000;
while(1)
{
memset(&from, 0, sizeof(from));
FD_ZERO(&rset);
FD_SET(info->skt, &rset);
//检测可接收
ret = select(info->skt+1, &rset, NULL, NULL, &tv);
if( ret < 0 )
{
goto SLEEP;
}
if( FD_ISSET(info->skt, &rset))
{
//接收数据
size = recvfrom(info->skt, info->buf, info->bufsize, 0, (struct sockaddr*)&from, &slen);
if( size <= 0 )
{
goto EXIT_END;
}
recvip = (unsigned char*)(&from.sin_addr.S_un.S_addr);
recvport = &from.sin_port;
printf("recv form %d.%d.%d.%d:%d %d byte data ", recvip[0], recvip[1], recvip[2], recvip[3], ntohs(*recvip), size);
}
SLEEP:
Sleep(5);
}
EXIT_END:
return -1;
}
- 余辉
-
为啥要端口不同?端口不同 肯定收不到啊,端口要一致才能收到
- clc1
-
按照理解,你的SOCKET是UDP的了,你就发了一个包,怎么可能会接收到两个包呢。还有你发送给的哪个端口哪个与之绑定的SOCKET才能接到。