udp 发送和接收结构体数据

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_29422251/article/details/53390587

 同一主机环境下测试,发送端口和接收端口不一样:

server端发送数据:

#include <stdio.h>
#include <string>
#include <iostream>
#include <winsock.h>
using namespace std;
//创建新的套接字之前需要调用一个引入Ws2_32.dll库的函数,否则服务器和客户端连接不上
#pragma comment(lib,"ws2_32.lib")

typedef struct CamCoord{
	double a;//the angle of camera turning
	double x;
	double y;
}camcoord,*pcam;
int main(int argc, char* argv[])
{
	camcoord udpPack;
	udpPack.a = 5.1;
	udpPack.x = 15.23;
	udpPack.y = 65.34;

	char *pPack = (char *) &udpPack;
	WSADATA wsaData;                                    //指向WinSocket信息结构的指针
	SOCKET serSocket;                                //创建套接字
	SOCKADDR_IN serUdpServ,clientAddr;                              //指向通信对象的结构体指针                          

	//*************************** 第一步初始化Winsock   *****************************//
	if(WSAStartup(MAKEWORD(2,2), &wsaData )!=0)     //进行WinSocket的初始化
	{
		printf("Can't initiates windows socket!Program stop.\n");//初始化失败返回-1
		return -1;
	}
	//********************   第二步建立一个数据报类型的UDP套接字  ******************//
	serSocket=socket(PF_INET,SOCK_DGRAM,0);     
	
	serUdpServ.sin_family = AF_INET;
	serUdpServ.sin_addr.s_addr=inet_addr("172.21.51.1");
	//saUdpServ.sin_addr.s_addr = htonl ( INADDR_BROADCAST );
	serUdpServ.sin_port = htons (8001);               //发送用的端口,可以根据需要更改

	clientAddr.sin_family=AF_INET;
	clientAddr.sin_port=htons(7001);
	clientAddr.sin_addr.s_addr=inet_addr("172.21.51.1");
	if(bind(serSocket, (sockaddr *)&serUdpServ, sizeof(serUdpServ)) == SOCKET_ERROR)
	{
		printf("bind error !");
		closesocket(serSocket);
		return 0;
	}
	while(1){
		Sleep(1000);
		//**********************  第三步使用sendto函数进行通信    *************************// 
		sendto(serSocket,pPack,24,0,(struct sockaddr*)&clientAddr,sizeof(clientAddr)); 
		
	}
	//*********************   第四步关闭socket  ***************************************//
	closesocket(serSocket);         //关闭监听socket
	WSACleanup();
	return 0;
}

client端接收数据:

#include <stdio.h>
#include<winsock.h>
#include <conio.h>
#pragma comment(lib,"ws2_32.lib")
typedef struct CamCoord{
	double a;//the angle of camera turning
	double x;
	double y;
}camcoord,*pcam;
int GetCamCollectionData(short serverProt,CamCoord &obj)
{
	CamCoord werror = {-1.0};
	WSADATA wsaData;                                   //指向WinSocket信息结构的指针
	SOCKET sockListener;
	SOCKADDR_IN sin,saClient;                          //设置两个地址,sin用来绑定

	char cRecvBuff[100]={0};                               //定义接收缓冲区
	int nSize,nbSize;
	int iAddrLen=sizeof(saClient);
	if(WSAStartup(MAKEWORD(2,2), &wsaData )!=0)           //进行WinSocket的初始化
	{
		printf("Can't initiates windows socket!Program stop.\n");//初始化失败返回-1
		return -1;
	}
	if((sockListener = socket(AF_INET,SOCK_DGRAM,0)) <= 0)  
	{  
		printf("Create socket fail!\n");  
		return -1;  
	}  
	sin.sin_family = AF_INET;
	sin.sin_port = htons(serverProt);  
	//sin.sin_rt = htons(7001);             //发送端使用的发送端口,可以根据需要更改
	sin.sin_addr.s_addr = inet_addr("172.21.51.1");
	if(bind( sockListener, (SOCKADDR FAR *)&sin, sizeof(sin))!=0)
	{
		printf("Can't bind socket to local port!Program stop.\n");//初始化失败返回-1
		return -1;
	}

	nSize = sizeof ( SOCKADDR_IN );
	if((nbSize=recvfrom (sockListener,cRecvBuff,100,0,
		(SOCKADDR FAR *) &saClient,&nSize))==SOCKET_ERROR) //若接收失败则提示错误
	{
		printf("Recive Error\n");
		return -1;
	}
	memcpy(&obj,cRecvBuff,sizeof(obj)+1);  
	//printf_s("%f%f%f\n",obj.angle,obj.x,obj.y);  
	closesocket(sockListener);
	return 0;
}


展开阅读全文

没有更多推荐了,返回首页