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

相关推荐

  1. TCP客户发送结构数据

    2024-03-15 18:50:03       20 阅读
  2. TCP服务主动向客户发送数据

    2024-03-15 18:50:03       14 阅读
  3. TCP、UDP客户

    2024-03-15 18:50:03       18 阅读
  4. Python3 TCP 客户

    2024-03-15 18:50:03       27 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-15 18:50:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-15 18:50:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-15 18:50:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-15 18:50:03       20 阅读

热门阅读

  1. 数仓开发之ODS层

    2024-03-15 18:50:03       20 阅读
  2. 微信小程序canvas画布不清晰解决方法

    2024-03-15 18:50:03       20 阅读
  3. PyMySQL

    2024-03-15 18:50:03       20 阅读
  4. 设置docker和docker容器开机自启

    2024-03-15 18:50:03       21 阅读
  5. C语言(数组)单元练习二

    2024-03-15 18:50:03       18 阅读
  6. 有趣之matlab-烟花

    2024-03-15 18:50:03       22 阅读
  7. 关于Mysql锁知识的整理,全面了解Mysql加锁规则

    2024-03-15 18:50:03       20 阅读
  8. 从中国到印尼海运申报和清关流程

    2024-03-15 18:50:03       20 阅读
  9. 证券期权业务知识

    2024-03-15 18:50:03       15 阅读