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;

}

相关推荐
Likeadust22 分钟前
险情发生,会已经开起来了:私有化音视频实时通信/视频会议平台EasyDSS临时会商如何抢出黄金处置时间
网络·音视频·easydss
moonsims1 小时前
AiBrainBox机器人多传感器标定与测试联合实验室-企业技术牵引·学校教学组织·师生共同实施·联合成果转化-企业、老师、学生组角色分工
大数据·网络·人工智能·无人机·量子计算
耗同学一米八1 小时前
湖南省网络建设与运维赛项指导培训:备赛要点全解析
运维·网络·php·网络运维·网络建设与运维·楚怡杯·湖南技能竞赛
行思理2 小时前
腾讯云报警提示存在恶意文件,文件路径:/tmp/php3BEJwM,威胁等级:严重,解决方法
网络·安全
沫璃染墨2 小时前
《Qt从零入门系列(十七):网络编程基础详解——从Socket原理到TCP/UDP通信API》
开发语言·网络·c++·qt·http·qt5·qt6.3
骑着蜗牛撵大象3272 小时前
一次 sendto 一次 recvfrom:UDP 数据报在 Linux 内核里怎么走完一整圈
linux·网络·网络协议·udp·ux·sk_buff·端口分用
人工干智能3 小时前
实操:办公室里的IP资源
网络·tcp/ip·智能路由器
这里是北京3 小时前
HLS 播放地址有效期只有5分钟,直播到一半会断吗?
运维·前端·网络
2401_879692513 小时前
微信小程序练习:网络和数据库
网络·数据库·微信小程序
JudithHuang3 小时前
Vue3 WebSocket 实时协作
前端·网络·websocket·网络协议·vue