CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2009
    Posts
    14

    RESOLVED. char issue, strange characters

    ----RESOLVED-----

    Hi All,

    This should be an easy one for you.

    I have defined and set a char as follows:

    char *data = "1";

    The information is sent over a SOCKET as such:

    send(testconnection,data,strlen(data),0);

    And when received I essentially have to guess as to what the content it will hold is (because it may not always be "1, it may be "blahblah" etc), so when I receive it I am using:

    char data[512];

    if(recv(client, data, sizeof(data),0)) {
    //data[sizeof(data)] = "\0";]

    cout << "sizeof:" << sizeof(data) << "\n";
    cout << "Menu option:" << data << "\n";

    }


    Now, as I am sure you will know, my cout << "Menu option:" << data << "\n"; returns my character 1. followed by a considerable amount of filler characters (to meet the full 512) as shown below:

    Menu option:1╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
    ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
    ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
    ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
    ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
    ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
    ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠►

    So, how would I go about fixing this so that it shows the value nicely??
    Last edited by skycrazy123; March 27th, 2009 at 05:52 AM. Reason: ISSUE RESOLVED

  2. #2
    Join Date
    Feb 2009
    Posts
    14

    Re: char issue, strange characters

    The problem has been resolved. I added: char data[512] = {'/0'}; at the receiving end.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: char issue, strange characters

    That's not *really* a resolution. What you want to do is send one additional byte on the sending end, so that the terminating NULL goes across the wire.

  4. #4
    Join Date
    Oct 2008
    Location
    Singapore
    Posts
    195

    Re: char issue, strange characters

    Quote Originally Posted by Lindley View Post
    That's not *really* a resolution. What you want to do is send one additional byte on the sending end, so that the terminating NULL goes across the wire.
    I am not sure whether this is a good solution. Network applications are supposed to be independent of OS, language, etc. Just because C/C++ uses NULL terminated strings, does not mean that we should modify the network protocol to send extra NULL in the end.
    I think the problem is printing of the data and not sending of the data. So, while printing, NULL should be added, if one wants to treat it as a NULL terminated string literal.

  5. #5
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: char issue, strange characters

    Quote Originally Posted by rohshall View Post
    I am not sure whether this is a good solution. Network applications are supposed to be independent of OS, language, etc. Just because C/C++ uses NULL terminated strings, does not mean that we should modify the network protocol to send extra NULL in the end.
    I think the problem is printing of the data and not sending of the data. So, while printing, NULL should be added, if one wants to treat it as a NULL terminated string literal.
    Great, so how do you propose to solve the printing problem? This interface on the receive end doesn't have any other way of knowing how much data to expect so there is no way to know where to insert the NULL character. Lindley's idea is the only reasonable solution that has been proposed. The only other way I can think of using this particular mechanism is to pack the number of bytes sent into the first character. Then you'll have to parse the buffer after being copied which would be fairly easy. Better yet, use a structure and define a message with a header and a body so that the receiver can intelligently "unpack" the message.
    Last edited by kempofighter; March 30th, 2009 at 10:13 AM.

  6. #6
    Join Date
    Oct 2008
    Location
    Singapore
    Posts
    195

    Re: char issue, strange characters

    Quote Originally Posted by kempofighter View Post
    The only other way I can think of using this particular mechanism is to pack the number of bytes sent into the first character.
    This is how most of the network protocols do it, I think.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured