lundi 27 juin 2016

Receive a structure with boost socket


I am newer with network programming. I'm trying to send and receive a structure using boost serialization. At the server side, I'm using a vector of structure for sending the information and at the receiver side, I don't know how to receive the vector in a suitable way and deserialize it. Now, I get segmentation fault. Any help is greatly appreciate.

Server side:

   struct packet{
        std::string pos;
        int size;
        int Lid;
        int Tid;
        int Qid;
        std::string type;
        std::string disc;
        std::string trunc;
        int nframe;
        template<typename Archive>
            void serialize(Archive&ar, const unsigned int version){
                ar & pos;
                ar &size;
                ar&Lid;
                ar&Tid;
                ar&Qid;
                ar&type;
                ar&disc;
                ar&trunc;
                ar&nframe;
            }
    };

    int main()
    {
    try
    {
        boost::asio::io_service io_service;
        tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(),50013));
        cout<<"Server waiting for connections: ....."<<endl;

        //send data to the receiver
        for(;;){ //infinite loop(always listening, the session is not closed)
            //creates a socket
            tcp::socket socket(io_service);
            //wait and listen
            acceptor.accept(socket);

            //prepare message to send back to the client
            std::string message="Connection accepted! n";

            boost::system::error_code ignored_error;
            //write the message
            boost::asio::write(socket, boost::asio::buffer(message),ignored_error);

         //open the trace file
                 char pos[20],type[20],trunc[20],disc[20];
                 int L,T,Q,size,nframe;
                 FILE *tracefile;
                 const char * cpath = path.c_str();
                 tracefile=fopen(cpath,"r");
                 fscanf(tracefile,"%s %d %d %d %d %s %s %s %d",pos,&size,&L,&T,&Q,type,disc,trunc,&nframe);
                 std::vector<packet> sendpacket;
                 sendpacket.push_back(packet());

                 string spos(pos);
                 string stype(type);
                 string sdisc(disc);
                 string strunc(trunc);
                 sendpacket[0].pos=spos;
                 sendpacket[0].size=size;
                 sendpacket[0].Lid=L;
                 sendpacket[0].Tid=T;
                 sendpacket[0].Qid=Q;
                 sendpacket[0].type=stype;
                 sendpacket[0].disc=sdisc;
                 sendpacket[0].trunc=strunc;
                 sendpacket[0].nframe=nframe;
                 cout<<sendpacket[0].pos<<endl;
                 cout<<sendpacket[0].size<<endl;

                 socket.send(boost::asio::buffer(sendpacket));


}

    }

Receiver Side:

struct packet{
    std::string pos;
    int size;
    int Lid;
    int Tid;
    int Qid;
    std::string type;
    std::string disc;
    std::string trunc;
    int nframe;

};

    int main(int argc, char* argv[])
    {

    //Receive the first packet
                 std::vector<packet> receivedpacket;
                //data received from the server
                 socket.receive(boost::asio::buffer(receivedpacket));
                 cout<<receivedpacket[0].pos<<endl;
                 cout<<receivedpacket[0].size<<endl;
                 cout<<receivedpacket[0].Lid<<endl;
                 cout<<receivedpacket[0].Tid<<endl;

    }

Aucun commentaire:

Enregistrer un commentaire