UDP协议的数据传输

           UDP,TCP协议是典型的网络通信协议,本文采用C语言实现数据的UDP传输。

以下为C代码

客户端实现:

// sender.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <Winsock2.h>
#pragma comment(lib,"ws2_32")
#include <stdio.h>
#define PORT 1129
#define ADDRESS "192.168.1.110"
struct CarTemp
{
	float Latitude;
	float Longitude;
	float Altitude;
	float VelocityNorth;
	float VelocityEast;
	float VelocityDown;
	float Heading;
}gps;
int main()
{
	gps.Latitude=120.11;
	gps.Longitude=36.23;
	gps.Altitude=22.30;
	gps.VelocityNorth=16.31;
	gps.VelocityEast=8.12;
	gps.VelocityDown=0.36;
	gps.Heading=0.69;

//加载winsock库
WSADATA wsaData;
WORD sockVersion = MAKEWORD(2,2);
if(WSAStartup(sockVersion,&wsaData)!=0)
   return 0;

//创建套接字
SOCKET sClient = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(sClient==INVALID_SOCKET)
{
   printf("socket error\n");
   return 0;
}

//socket中装入远程地址信息
SOCKADDR_IN remote;
remote.sin_family = AF_INET;
remote.sin_port = htons(1129);
remote.sin_addr.S_un.S_addr = inet_addr(ADDRESS);

if(connect(sClient,(SOCKADDR *)&remote,sizeof(SOCKADDR))==SOCKET_ERROR)
{
   printf("connect error\n");
   closesocket(sClient);
   return 0;
}

while(true)
{
   char SendData[7];
   int i,ch;
   send(sClient,(char*)(&gps),sizeof(gps),0);  
}

closesocket(sClient);
WSACleanup();
return 0;
}


服务端实现:

#include "stdafx.h"
#include <Winsock2.h>
#pragma comment(lib,"ws2_32")
#include <stdio.h>
//#include <iostream.h>
struct CarTemp
{
	float Latitude;
	float Longitude;
	float Altitude;
	float VelocityNorth;
	float VelocityEast;
	float VelocityDown;
	float Heading;
}gpss;
int main(int argc,char *[])
{
//gpss info;
//加载winsock库
WSADATA wsaData;
WORD sockVersion = MAKEWORD(2,2);
if(WSAStartup(sockVersion,&wsaData)!=0)
   return 0;

//创建套接字
SOCKET sServer = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(sServer==INVALID_SOCKET)
{
   printf("socket error\n");
   return 0;
}

//socket中装入地址信息
sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(1129);
sin.sin_addr.S_un.S_addr = INADDR_ANY;

//bind套接字
if(bind(sServer,(LPSOCKADDR)&sin,sizeof(sin)) == SOCKET_ERROR)
{
   printf("bind error!\n");
   return 0;
}

//listen模式
if(listen(sServer,5) == SOCKET_ERROR)
{
   printf("listen error!\n");
   closesocket(sServer);
   return 0;
}

//循环接受连接请求
sockaddr_in remoteAddr;
SOCKET sClient;
int nAddrLen=sizeof(SOCKADDR_IN);
char revData[7];
while(1)
{
   sClient = accept (sServer,(SOCKADDR*)&remoteAddr,&nAddrLen);
   if(sClient==INVALID_SOCKET)
   {
    printf("accept error\n");
    continue;
   }
   printf("接受来自%s的连接.\n",inet_ntoa(remoteAddr.sin_addr));
   while(true)
   {
    //int ret = recv(sClient,revData,7,0);
    int ret = recv(sClient, (char*)(&gpss), sizeof(gpss),0);
    if(ret==0||(ret==SOCKET_ERROR&&WSAGetLastError()==WSAECONNRESET))
    {
     printf("%s关闭连接\n",inet_ntoa(remoteAddr.sin_addr));
     closesocket(sClient);
     break;
    }
    else
    {
     revData[ret]='\0';
    FILE* fp1 = fopen("D:\\2.txt","a");
     printf("%f,%f,%f", gpss.Altitude,gpss.Longitude,gpss.Latitude);
    if(fp1==NULL)
        return 1;
	 fprintf(fp1,"%f,%f,%f,%f,%f,%f,%f\n",gpss.Longitude,gpss.Latitude,gpss.Altitude,gpss.VelocityNorth,gpss.VelocityEast,gpss.VelocityDown,gpss.Heading);
     //fwrite(revData, sizeof(char)*10, 1, fp1);
     //fputc('\x0D', fp1);
     // fputc('\x0A', fp1);
    fclose(fp1);
    }
   }

}

closesocket(sServer);
WSACleanup();
return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值