windows socket编程,接收一个控制组播包,用来控制对外发出的组播包。其中用到了多线程和socket相关知识。代码如下:
// RadarTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "comdef.h"
#include <WINSOCK.H>
#include <stdio.h>
#define HELLO_PORT 3840
#define HELLO_GROUP "228.3.1.1"
#define RECV_PORT 9411
#define RECV_GROUP "234.2.2.2"
#pragma comment(lib, "WSOCK32.lib")
char HJbuff[1024]={0};
int HJbuff_len=500;
int updateTime=0;
char Recvbuff[1024]={0};
int Recvbuff_len=10;
int global_send_flag=0;
void genHJbuf()
{
memset(HJbuff,0,1024);
int _mubiaoNum=10;
HJbuff[0]=2;
HJbuff[8]=7;
HJbuff[29]=_mubiaoNum;
HJDA_Struct _hjItem;
int itemSize=sizeof(HJDA_Struct);
for(int i=0; i<_mubiaoNum; i++)
{
_hjItem.mubiaohao=i+1;
_hjItem.mubiaostatus=1;
_hjItem.mubiaojuli=100.0*updateTime;
_hjItem.mubiaofangwei=30.0*i;
_hjItem.mubiaoyangjiao=i*10.0;
_hjItem.jingxiangsudu=0;
_hjItem.fangweisudu=0;
_hjItem.yangjiaosudu=0;
memcpy((char*)&HJbuff[30+i*47],(char*)&_hjItem,itemSize); //每个航迹点47个字节
//memcpy((char*)&_hjItem,(char*)&HJbuff[30+i*47],sizeof(HJDA_Struct));
}
updateTime++;
}
int ThreadF0(LPVOID lpParam)
{
int iRet = 0;
int i=0;
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
sockaddr_in addr;
addr.sin_family = AF_INET;
//addr.sin_addr.S_un.S_addr = INADDR_ANY;
addr.sin_addr.S_un.S_addr = inet_addr("192.168.4.169");
addr.sin_port = htons(RECV_PORT);
bool bOptval = true;
iRet = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&bOptval, sizeof(bOptval));
if(iRet != 0){
printf("setsockopt fail:%d", WSAGetLastError());
return -1;
}
iRet = bind(sock, (sockaddr*)&addr, sizeof(addr));
if(iRet != 0){
printf("bind fail:%d", WSAGetLastError());
return -1;
}
printf("socket:%d bind success\n", sock);
// 加入组播
ip_mreq multiCast;
//multiCast.imr_interface.S_un.S_addr = INADDR_ANY;
multiCast.imr_interface.S_un.S_addr = inet_addr("192.168.4.169");
multiCast.imr_multiaddr.S_un.S_addr = inet_addr(RECV_GROUP);
iRet = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&multiCast, sizeof(multiCast));
if(iRet != 0){
printf("setsockopt fail:%d", WSAGetLastError());
return -1;
}
printf("udp group start\n");
int len = sizeof(sockaddr);
char strRecv[1024] = {0};
while(true)
{
memset(strRecv, 0, sizeof(strRecv));
iRet = recvfrom(sock, strRecv, sizeof(strRecv) - 1, 0, (sockaddr*)&addr, &len);
if(iRet <= 0){
printf("recvfrom fail:%d", WSAGetLastError());
return -1;
}
if(iRet>0)
{
if((strRecv[1])==1)
{
global_send_flag=1;
}
else
{
global_send_flag=0;
}
}
}
closesocket(sock);
WSACleanup();
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsaData;
WORD wVersionRequested;
wVersionRequested = MAKEWORD(1,1);
// Initialize Windows socket library
WSAStartup(0x0202, &wsaData);
struct sockaddr_in addr;
int fd;
int t = 0;
HANDLE htd0;
DWORD thrdID0;
htd0 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadF0, (void*)&t, 0, &thrdID0);
/* create what looks like an ordinary UDP socket */
if ((fd=socket(AF_INET,SOCK_DGRAM,0)) < 0)
{
perror("create send socket error");
exit(1);
}
/* set up destination address */
memset(&addr,0,sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=inet_addr(HELLO_GROUP);
addr.sin_port=htons(HELLO_PORT);
/* now just sendto() our destination! */
while (1)
{
if(global_send_flag==1)
{
genHJbuf();
if (sendto(fd,HJbuff, HJbuff_len, 0, (struct sockaddr *) &addr, sizeof(addr)) < 0)
{
perror("sendto");
exit(1);
}
printf("Send %d times.\n",updateTime);
Sleep(50);
}
}
return 0;
}
|