TCP客户端发送结构体数据

#define _CRT_SECURE_NO_WARNINGS

#include <windows.networking.sockets.h>

#pragma comment(lib, "Ws2_32.lib")

#include <iostream>

#include <string>

using namespace std;

#define BUF_LEN 128

struct Header

{

unsigned int MakeLength;

unsigned int ModelLength;

} head;

struct CarData

{

int Vin; //Unique Vehicle ID

char* Make; //A string containing the make of the car

char* Model; //A string containing the model of the car

float MSRP; //Suggest Manufacturers Retail Price in $

}car;

struct Packet

{

struct Header head;

struct CarData car;

};

char* SerializePacket(const struct Packet& packet, int& TotalSize)

{

TotalSize = sizeof(packet.head) + sizeof(packet.car.Vin) + packet.head.MakeLength + packet.head.ModelLength + sizeof(packet.car.MSRP);

// Allocate a buffer to hold the serialized data

char* buffer = new charTotalSize;

// Copy the data from the CarData struct to the buffer

memcpy(buffer, &packet.head, sizeof(packet.head));

memcpy(buffer + sizeof(packet.head), &packet.car.Vin, sizeof(packet.car.Vin));

memcpy(buffer + sizeof(packet.head) + sizeof(packet.car.Vin), packet.car.Make, packet.head.MakeLength);

memcpy(buffer + sizeof(packet.head) + sizeof(packet.car.Vin) + packet.head.MakeLength, packet.car.Model, packet.head.ModelLength);

memcpy(buffer + sizeof(packet.head) + sizeof(packet.car.Vin) + packet.head.MakeLength + packet.head.ModelLength, &packet.car.MSRP, sizeof(packet.car.MSRP));

return buffer;

}

int main()

{

//starts Winsock DLLs

WSADATA wsaData;

if ((WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0) {

return 0;

}

//initializes socket. SOCK_STREAM: TCP

SOCKET ClientSocket;

ClientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if (ClientSocket == INVALID_SOCKET) {

WSACleanup();

return 0;

}

//Connect socket to specified server

sockaddr_in SvrAddr;

SvrAddr.sin_family = AF_INET; //Address family type itnernet

SvrAddr.sin_port = htons(27000); //port (host to network conversion)

SvrAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); //IP address

if ((connect(ClientSocket, (struct sockaddr*)&SvrAddr, sizeof(SvrAddr))) == SOCKET_ERROR) {

closesocket(ClientSocket);

WSACleanup();

return 0;

}

/***********************************

* Your client code goes here *

************************************/

char buffer128;

memset(buffer, 0, 128);

Packet packet;

memset(&packet, 0, sizeof(Packet));

cout << "Enter Vin: ";

cin >> packet.car.Vin;

cout << "Enter Make: ";

cin >> buffer;

packet.head.MakeLength = strlen(buffer) + 1;

packet.car.Make = (char*)malloc(packet.head.MakeLength);

strcpy(packet.car.Make, buffer);

packet.car.Makepacket.head.MakeLength - 1 = '\0';

memset(buffer, 0, sizeof(buffer));

cout << "Enter Model: ";

cin >> buffer;

packet.head.ModelLength = strlen(buffer) + 1;

packet.car.Model = (char*)malloc(packet.head.ModelLength);

strcpy(packet.car.Model, buffer);

packet.car.Modelpacket.head.ModelLength - 1 = '\0';

cout << "Enter MSRP: ";

cin >> packet.car.MSRP;

int totalSize;

char* serializedPacket = SerializePacket(packet, totalSize);

send(ClientSocket, serializedPacket, totalSize, 0);

delete\[\] serializedPacket;

//closes connection and socket

closesocket(ClientSocket);

//frees Winsock DLL resources

WSACleanup();

return 1;

}

相关推荐
星恒讯工业路由器3 小时前
AGV集群通信架构:从单点到百台级调度的挑战与对策
网络·物联网·信息与通信·工业物联网·通信架构·agv集群·实时调度
夜月yeyue4 小时前
AUTOSAR CP 从上电到 Runnable
c语言·网络·tcp/ip·车载系统
lsh曙光4 小时前
延时at指令和定时cron指令
linux·服务器·网络
treesforest4 小时前
IP定位技术在网络犯罪侦查中的应用与价值
网络·网络协议·tcp/ip·网络安全·ip归属地查询·反欺诈
外滩运维专家5 小时前
HTTPS 证书报错排查手册:6 个高频错误码及解决方法
网络协议·http·https
XR1234567885 小时前
企业全光网络架构选型技术白皮书:从物理层到运维层的全链路分析
运维·网络·架构
起司喵喵6 小时前
通过Certbot自动申请更新HTTPS网站的SSL证书
网络协议·https·ssl
萧瑟余晖6 小时前
Java深入解析篇九之NIO详解
java·网络·nio
小王C语言7 小时前
【7. 实现登录注册模块】:实现会话管理、注册/登录/退出 API
网络·c++
泡沫冰@7 小时前
ECS 的介绍和使用
linux·服务器·网络