Thread: read string from socket

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    6

    read string from socket

    I hardly ever touch C so apologies if this is totally basic!

    After reading data from a socket all I want to do is print the string to the screen.

    I have this code, as part of a larger script:

    Code:
    void read_socket(int s) {
       int status;
       char result[1024];
       
       status = read(s,result,sizeof(result));
       
       if ( status < 0 ) {
          fprintf(stderr,"Error reading from socket.");
          exit(0);
       }
       
       printf("string: %s\nbytes: %d\n", result, status);
    
    }
    The output of this is:

    string:
    bytes: 948

    As far as I can see 'result' should be holding the string which is 948 bytes in size but when trying to print it to the screen I am getting nothing, any ideas please?

    Thanks, Geester

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    And what is the contents of the string? maybe there is just 0 as the first character?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    6
    Quote Originally Posted by vart View Post
    And what is the contents of the string? maybe there is just 0 as the first character?
    It is a small string of xml, I have it working fine in PHP, looking at it with that script I just noticed that the beginning of the string contains some strange characters:

    �

    I assume this may be causing the problem? Any idea how to get around this please?

    Thanks, Grant

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Try fo the start to print several chars at the beginnign of the string in hex to see if you receive something
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    6
    Hmm, not sure if I did this correctly but I tried:

    Code:
    printf("%x\n",result);

    And this is the output : a297b80

    ????


    Thanks, Grant

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    read does not put a null terminator on the end of the string.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by geester View Post
    Hmm, not sure if I did this correctly but I tried:

    Code:
    printf("%x\n",result);

    And this is the output : a297b80

    ????


    Thanks, Grant
    result is an array, and when used by itself it almost always decays down to a pointer to the first element, and %x means "print this integer in hexadecimal format". So, you printed the address of your buffer.

    Maybe
    Code:
    printf("%c%c%c%c", result[0], result[1], result[2], result[3]);
    to see the first characters of your result? And if you wanted more you could loop.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Note that you could use something like this to limit printf() to printing the right number of characters:
    Code:
    printf("&#37;.*s", length, string);
    where length is status and string is result in your case.

    Of course, that only works if there is actually something in the string, and would stop immediately upon finding a NULL in the string. Perhaps fwrite() or write() would be better.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    6
    Thanks for all your help, Im only testing something at the moment and just need the first part of the string so looping this, as provided above:

    Code:
    printf("%c", result[x]);
    does the job.

    Thanks, Geester

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Replies: 1
    Last Post: 05-30-2003, 02:31 AM