简体   繁体   中英

Saving TCP Server data packets to a File in C++

I have created a program with a server and client in visual studio. This all works the server receives data packets from the client. Now that this works I want the data to be stored in a file so that it can be analysed. Here is the code for the Server.

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

I have has an attempt at doing it myself but I can't figure out how to do it for data packets received by a server from a client. Here is my attempt and creating the 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()
{
}

I would great if someone could guide me in the right direction by showing me an example using my code. Thanks.

Change the return type of the receiveFromClients from void to Packet and return the received packet.

Then create an operator << for the Packet class. For example:

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

With this the code to write the received data to a file you posted will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM