结构体数据传输
#include< stdio.h >
#include< string.h >
#include< winsock.h >
#include< windows.h >
#include <iostream>
#pragma comment( lib, "ws2_32.lib" )
#define PORT 2046
#define BACKLOG 10
#define TRUE 1
#define MAXDATASIZE 1000
using namespace std;
typedef struct GPS {
float longitude;
float latitude;
float altitude;
char WE;
char NS;
char CRC;
};
float ByteToFloat(char* byteArray)
{
return *((float*)byteArray);
}
void main(void)
{
int iServerSock;
int addr_len;
int numbytes;
char buf[MAXDATASIZE];
struct sockaddr_in ServerAddr;
struct sockaddr_in ClientAddr;
GPS info;
WSADATA WSAData;
if (WSAStartup(MAKEWORD(1, 1), &WSAData))
{
printf("initializationing error!\n");
WSACleanup();
exit(0);
}
iServerSock = socket(AF_INET, SOCK_DGRAM, 0);
if (iServerSock == INVALID_SOCKET)
{
printf("创建套接字失败!\n");
WSACleanup();
exit(0);
}
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(PORT);
ServerAddr.sin_addr.s_addr = INADDR_ANY;
memset(&(ServerAddr.sin_zero), 0, sizeof(ServerAddr.sin_zero));
if (bind(iServerSock, (struct sockaddr *)&ServerAddr, sizeof(struct sockaddr)) == -1)
{
printf("bind调用失败!\n");
WSACleanup();
exit(0);
}
while (1) {
addr_len = sizeof(struct sockaddr);
numbytes = recvfrom(iServerSock, buf, MAXDATASIZE, 0, (struct sockaddr *) & ClientAddr, &addr_len);
if (numbytes == -1)
{
printf("recvfrom调用失败!\n");
WSACleanup();
exit(0);
}
GPS info;
printf("got packet from %s\n", inet_ntoa(ClientAddr.sin_addr));
printf("packet is %d bytes long\n", numbytes);
buf[numbytes] = '\0';
memcpy(&info, buf, sizeof(buf) + 1);
printf("longitude = %f, latitude = %f, altitude = %f, WE =%c, NS= %c, CRC = %c\n",
info.longitude, info.latitude, info.altitude, info.WE, info.NS, info.CRC);
}
closesocket(iServerSock);
WSACleanup();
}
#include< stdio.h >
#include< stdlib.h >
#include< windows.h >
#include< winsock.h >
#include< string.h >
#include <iostream>
#pragma comment( lib, "ws2_32.lib" )
#define PORT 2046
#define MAXDATASIZE 100
using namespace std;
typedef struct GPS {
float longitude;
float latitude;
float altitude;
char WE;
char NS;
char CRC;
};
void main(void)
{
int iClientSock;
struct sockaddr_in ServerAddr;
GPS info;
int numbytes;
WSADATA WSAData;
if (WSAStartup(MAKEWORD(1, 1), &WSAData))
{
printf("initializationing error!\n");
WSACleanup();
exit(0);
}
if ((iClientSock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
printf("创建套接字失败!\n");
WSACleanup();
exit(0);
}
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(PORT);
ServerAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(&(ServerAddr.sin_zero), 0, sizeof(ServerAddr.sin_zero));
while (1)
{
cin >> info.longitude;
cin >> info.latitude;
cin >> info.altitude;
cin >> info.WE;
cin >> info.NS;
cin >> info.CRC;
char *buf = (char *)&info;
numbytes = sendto(iClientSock, buf, 16, 0, (struct sockaddr *) & ServerAddr, sizeof(struct sockaddr));
}
if (numbytes == -1)
{
printf("sendto调用失败!\n");
WSACleanup();
exit(0);
}
printf("sent %d bytes to %s\n", numbytes, inet_ntoa(ServerAddr.sin_addr));
closesocket(iClientSock);
WSACleanup();
}
|