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.
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.
– zentrunix