Ho riscritto il codice cercando di inserire prima nel buf e poi copiare l'intero nella struttura.
codice:
char buf [MAXRECV];
string line;
MSG* pMsg = new MSG;
memset ( pMsg, 0, sizeof(MSG));
memset(buf,0,MAXRECV);
//Devo leggere l'intero e lo inserisco nel buf
int status = ::recv ( sock, buf, sizeof(HEADER), MSG_NOSIGNAL);
if ( status < 0) {
cout << " Error recv" << endl;
}
// lo copio nella struttura header.
memcpy(&(pMsg->m_header.m_textLength), buf, sizeof(HEADER));
int lengthMessage = pMsg->m_header.m_textLength;
cout << "CCommunication: " << pMsg->m_header.m_textLength << endl;
// faccio la prima recv per inserirla nel buf
int byteReads = ::recv ( sock, pMsg->m_buf, sizeof(MSG) - sizeof(HEADER) , MSG_NOSIGNAL );
// vedo per vedere se la stringa letta è stata presa tutta altrimenti devo leggere ancora
while (byteReads < lengthMessage) {
//Messaggio spezzettato
byteReads = ::recv ( sock, pMsg->m_buf , lengthMessage - byteReads, MSG_NOSIGNAL );
}
if ( byteReads < -1 ) {
cout << " Error recv" << endl;
}
line = pMsg->m_buf;
return line;
Per capire l'intero non lo prende bene, ho mandato un char buff = {'A','0','0','0','C','i','A','\0'}
ma mi dà un numero strano.