Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C Linux Networking
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 12-Apr-10 21:30pm
Guy Tal333
Edited 2-May-10 1:11am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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. Smile | :)
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
Smile | :)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

'\0' 
be aware with this small thing,
C uses
'\0' 
for end of string.
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 13 Oct 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100