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 char[TotalSize];

// 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 buffer[128];

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.Make[packet.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.Model[packet.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;

}

相关推荐
SUGERBOOM几秒前
【网络安全】网络基础第一阶段——第一节:网络协议基础---- OSI与TCP/IP协议
网络·网络协议·web安全
petaexpress17 分钟前
常用的k8s容器网络模式有哪些?
网络·容器·kubernetes
m0_609000422 小时前
向日葵好用吗?4款稳定的远程控制软件推荐。
运维·服务器·网络·人工智能·远程工作
suifen_5 小时前
RK3229_Android9.0_Box 4G模块EC200A调试
网络
铁松溜达py5 小时前
编译器/工具链环境:GCC vs LLVM/Clang,MSVCRT vs UCRT
开发语言·网络
衍生星球10 小时前
【网络安全】对称密码体制
网络·安全·网络安全·密码学·对称密码
掘根10 小时前
【网络】高级IO——poll版本TCP服务器
网络·数据库·sql·网络协议·tcp/ip·mysql·网络安全
友友马11 小时前
『 Linux 』HTTP(一)
linux·运维·服务器·网络·c++·tcp/ip·http
2401_8725149711 小时前
深入探究HTTP网络协议栈:互联网通信的基石
网络·网络协议·http