gpt4 book ai didi

c++ - 在 C++ 中将 TCP 服务器数据包保存到文件

转载 作者:行者123 更新时间:2023-11-28 05:11:37 24 4
gpt4 key购买 nike

我在 visual studio 中创建了一个带有服务器和客户端的程序。这一切都适用于服务器从客户端接收数据包。现在这可行了,我希望将数据存储在一个文件中,以便对其进行分析。这是服务器的代码。

#include "StdAfx.h"
#include "ServerExample.h"
#include <iostream>


unsigned int ServerExample::client_id;

ServerExample::ServerExample(void)
{
// id's to assign clients for our table
client_id = 0;

// set up the server network to listen
network = new ServerNetwork();
}

ServerExample::~ServerExample(void)
{
}

void ServerExample::update()
{
// get new clients
if (network->acceptNewClient(client_id))
{
printf("client %d has been connected to the server\n", client_id);

//increment client ID for the next client
client_id++;
}

receiveFromClients();

}


void ServerExample::receiveFromClients()
{

Packet packet;

// go through all clients using an iterator
std::map<unsigned int, SOCKET>::iterator iter;

for (iter = network->sessions.begin(); iter != network->sessions.end(); iter++)
{
int data_length = network->receiveData(iter->first, network_data);

if (data_length <= 0)
{
//no data recieved
continue;
}

int i = 0;
while (i < (unsigned int)data_length)
{
packet.deserialize(&(network_data[i]));
i += sizeof(Packet);

//switch based on packet type
switch (packet.packet_type) {

case INIT_CONNECTION:

printf("server received init packet from client\n");


sendActionPackets();
int c;
cout << "Press the return key to continue ";
c = cin.get();
if (c == '\n') {


break;

//HERE ARE THE PACKETS I WANT STORED IN THE FILE
case DATA_EVENT:
printf("clientID %d ", packet.clientID);
printf("speed %d ", packet.speed);
printf("longitude %.f ", packet.longitude);
printf("latitude %.f ", packet.latitude);
printf("temperature %.1f\n ", packet.temperature);
printf("fuelLevel %.1f ", packet.fuelLevel);
sendActionPackets();


if (packet.speed < 20) {
printf(" Speed is too low please report any problems. ");
}
else if (packet.temperature > 29)
{
printf(" Please check temperature. Too high. ");
}
else if (packet.temperature < 10)
{
printf(" Please check temperature. Too low. ");


break;

default:

printf("error in packet types\n");

break;
}
}
}
}
}
}
void ServerExample::sendActionPackets()
{
// send action packet
const unsigned int packet_size = sizeof(Packet);
char packet_data[packet_size];

Packet packet;
packet.packet_type = DATA_EVENT;

packet.serialize(packet_data);

network->sendToAll(packet_data, packet_size);
}

我自己尝试过这样做,但我不知道如何处理服务器从客户端接收到的数据包。这是我的尝试并创建了 fileHandler。

#include "stdafx.h"
#include "FileHandler.h"
#include <iostream>
#include <fstream>

using namespace std;
FileHandler::FileHandler()
{
}
int FileHandler::myFile() {
ofstream PacketFile;
PacketFile.open("packetData.txt");

//This is the part I can't figure out
PacketFile << receiveFromClients();

PacketFile.close();
}

FileHandler::~FileHandler()
{
}

如果有人可以通过使用我的代码向我展示示例来指导我朝着正确的方向前进,我会非常高兴。谢谢。

最佳答案

void 更改 receiveFromClients 的返回类型至 Packet并返回接收到的数据包。

然后创建一个 operator <<对于 Packet类(class)。例如:

std::ostream& operator <<(std::ostream& ostream, const Packet& packet)
{
if(packet.packet_type == DATA_EVENT)
{
ostream << packet.clientID << ";"
<< packet.speed << ";"
<< packet.longitude << ";"
<< packet.latitude << ";"
<< packet.temperature << ";"
<< packet.fuelLevel << std::endl;
}

return ostream;
}

有了这个,将接收到的数据写入您发布的文件的代码就可以工作了。

关于c++ - 在 C++ 中将 TCP 服务器数据包保存到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43384318/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com