Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello,

I am writing a client and server program using C on Linux.

I send a message with "send" command and on the other side I recieve the message with "recv" command. The problem is that the message that I want to send may contain '\0' characters inside it. Such messages when recieved are truncated at the first '\0' character. So I can not recieve the entire message.

What can I do? Is there another command that can solve my problem?

Thanks!
Posted
Updated 2-May-10 0:11am
v2

send() and recv() will definitely NOT stop at a \0 character and they are the right functions to use. Without seeing your source code it's hard to help, the problem could be in the client or the server side.

Maybe have a look if you're using any of the normal string handling functions that stop at 0 character, like strlen() or strcpy(). You could add some debugging output how much bytes you are sending and receiving. Also see Beej's guide to networking programming[^], there are client/server examples in chapter 6.

Hope this helps. :)
 
Share this answer
 
v3
As far as I know, the recv function does NOT stop when the first '\0' is received, hence the problem is in your code. If you must deal with variable length messages in your applications, then send the message length as (first) part of the message, this way the receiver will be able to ask for the whole info.
:)
 
Share this answer
 
When using TCP you can't assume any relation between data block lengths in send() and recv(). You may need multiple recv() calls to get the data from a single send() or you might just as well get multiple send() data in a single recv() call. You must keep calling recv() until you have received the whole message.
 
Share this answer
 
Can't you not specify the length of the transmited data?

I did some code a time ago to connect a PC with a PLC and faced the same problem, but then I added fixed length for the telegrams (150 bytes for my app). With that fixed length I was always sending the 150 bytes, no matter if the bytes were data or "\0", the only thing is that in the PC I had to declare a fixed Input-buffer length as well and ignore the empty spaces and the "\0".

I think you could give it a try.
 
Share this answer
 
'\0' 
be aware with this small thing,
C uses
'\0' 
for end of string.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900