Reputation: 115
I am trying to send a small packet via tcp but i have a problem with the size of it. My data is 255 byte buffer:
buffer[0] = 0x00;
buffer[1] = 0x04;
buffer[2] = 0x06;
buffer[3] = 0x08;
buffer[4] = 0x01;
buffer[5] = 0x01;
and i use
send(sockfd,buffer,6,MSG_NOSIGNAL|MSG_DONTWAIT);
but data can not be send when i use 6. If i use send like:
send(sockfd,buffer,sizeof(buffer),MSG_NOSIGNAL|MSG_DONTWAIT);
then data is sent but recevier has to parse extra 250 0x00 byte and i don't want it. Why 6 is no ok. I also try 10 randomly nothing changes.
Here is my receiver code Anything writes at that side:
while(1) {
ret = recv(socket,buffer,sizeof(buffer),0);
if (ret > 0)
printf("recv success");
else
{
if (ret == 0)
printf("recv failed (orderly shutdown)");
else
printf("recv failed (errno:%d)",errno);
}
}
Upvotes: 0
Views: 96
Reputation: 310913
Get rid of MSG_DONTWAIT. This is only useful when you have something else to do when the socket send buffer is full and the data can't be sent. As you aren't checking the result of send(), clearly you aren't interested in that condition. However you must check the result of send anyway, as you may have got an error, so fix your code to do that as well.
Upvotes: 2