I will copy the data I received with recv() and the maximum size is MAX_PACKET for in/out buffers.
Is it safe to copy it with the fixed size MAX_PACKET? Is it necessary for me to set the right size of the buffer when I use memcpy ?
recv(sock,recvbuf,MAX_PACKET,0);
memcpy(inbuffer,recvbuf,MAX_PACKET);
you need to declare inbuffer at least as many bytes as the size of MAX_PACKET
char * inbuffer = new char[MAX_PACKET];
and place before each recv()
memset(inbuffer,0,MAX_BUFFER);
to zero out the buffer so you don’t mistakenly see the tail end of a previous packet in the scenario you received two packets where the 2nd is shorter than the 1st.
if your incoming packet has no unique termination byte ie ‘\r’
you need to add
int recvbytes ;
recvbytes =new recv(…)
since recv returns the number of bytes received on the wire