TCP客户端发送结构体数据

本文介绍了如何使用C++编程语言,包括Windows套接字库,定义结构体来存储车辆信息(如VIN、型号、制造商和MSRP),并实现序列化功能以通过TCP连接发送给服务器。展示了从用户输入获取数据,构造Packet对象,序列化数据以及关闭连接的过程。
摘要由CSDN通过智能技术生成

#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;
}
 

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

2193410903

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值