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;

}

相关推荐
QYRdata7 小时前
权威数据披露:2026至2032年边缘云服务CAGR达15.6%,赛道发展驶入快车道
网络·人工智能·云计算·服务发现·边缘计算
Yang96119 小时前
鼎讯信通DXB-2000S OTDR光时域反射仪模块技术拆解:0.8m盲区
网络
中科三方10 小时前
两家域名注册商资质被ICANN终止:企业域名资产安全再受关注
前端·网络·安全·域名
超智算科技10 小时前
2026服贸会现场直击|Net Zero Hub净零算力枢纽全球首发! 超智算受邀深度参与服贸会全球OPC共创节
网络·人工智能·科技·物联网·gpu算力
逐米时代10 小时前
工控网络安全:安全与连续性双目标下的精准防护
网络
Yang961110 小时前
通信广电与无线电管理:鼎讯信通ZN-080A手持式信号综合分析仪能做什么
网络
2601_9494999410 小时前
DT‑1414PTZ如何百分百兼容(安华高)HFBR‑1414PTZ
运维·网络·人工智能·科技·光模块
sibylyue11 小时前
WebSocket双向长连接传输通道
网络·websocket·网络协议
小小、码农12 小时前
C++11右值引用与移动语义 —— 从拷贝资源到转移资源
开发语言·网络·c++·网络协议
Zenova EdgeOS13 小时前
工业网关数据持久化:从同步到 WAL 的工程实战
网络·数据库·oracle