How to send an object using socket c++ visual studio

Asked

Viewed 40 times

1

I am trying to send an object to a server socket. The client and the server have the same class that I use to store the data. It’s all working this way, but I’m having problems on the server side.

That’s the class I’m wearing.

class tMensagem{

    private:
       string destinatario;
       string mensagem;

    public:
       tMensagem();
       ~tMensagem();

       string getMensagem();
       void setMensagem(string mensagemTMP);
       string getDestinatario();
       void setDestinatario(string destinatarioTMP);
    };

But when the string has more than 15 characters, I get some error to read on the server side.

inserir a descrição da imagem aqui

To receive the data I am using reinterpret_cast

tMensagem * t2 = reinterpret_cast<tMensagem *>(myMessage);

And my recv():

recv(ClientSocket, (char *)t2, 1024, 0);
  • You cannot simply do a "reinterpret_cast" because instances of the "string" class are not mapped directly to an array of characters. You will have to parse the received message, separate the bytes for each string ("message" and "recipient"), create instances of "message" "recipient" as string, and then create an instance of the tMensage class.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.