How to empty the variable buffer in recv() before accepting string

Discussion in 'C Programming' started by Aditya, May 29, 2007.

  1. Aditya

    Aditya Guest

    Hi
    I am using recv() from socket.h in one of my TCP-client projects.
    The problem is that the buffer variable in recv(socketDescriptor,
    buffer, flags) points to some stray location and when the incoming
    string is filled into it, there are trailing junk characters also.

    The code portion refering to it is somewhat like:

    int rcvStatus;
    char *buffer;
    rcvStatus = recv(socketDescriptor, buffer, flags);
    std::cout<< buffer << std::endl;

    If I initialize the buffer variable to NULL or "0", the recv() returns
    -1, that is it doesn't accept the incoming string.
    So, Please help me to clean the buffer variable, before it is being
    filled with the incoming string.

    Thanks
    Aditya
     
    Aditya, May 29, 2007
    #1
    1. Advertising

  2. In article <>,
    Aditya <> wrote:
    >Hi
    >I am using recv() from socket.h in one of my TCP-client projects.


    recv(), socket.h, and TCP clients are not defined by the C language and
    are therefore beyond the scope of comp.lang.c. It does appear, though,
    that your problem is actually C-related (probably by accident), so:

    >The problem is that the buffer variable in recv(socketDescriptor,
    >buffer, flags)


    I think you're either getting the interface for recv wrong or using a
    socket API that's completely and irredeemably broken.
    Shouldn't there be some way to tell it how many bytes you're expecting
    it to read?

    > points to some stray location and when the incoming
    >string is filled into it, there are trailing junk characters also.


    recv is, if it behaves like any sane socket read would (but see above),
    similar to fread in its behavior: It reads a series of bytes, not a
    string. If you want a string, you need to null-terminate it yourself.
    One way to do this is to zero-fill the buffer before calling it; another
    is to look at how much you actually read and write a null terminator
    for the string at the end of that. In either case you need to make sure
    there's enough room in your buffer for it.


    >The code portion refering to it is somewhat like:


    If you have a problem with your code, post the actual code you're having
    problems with. Not "something like" it.


    >int rcvStatus;
    >char *buffer;
    >rcvStatus = recv(socketDescriptor, buffer, flags);


    I certainly hope that the code you're actually trying to use is somewhat
    less broken than this. Where is buffer pointing when you give it to recv?

    >std::cout<< buffer << std::endl;


    This is not C.


    dave

    (posting, for the first time somewhere more widely read than uw.test,
    using a new trn I built to replace the one that vanished from the
    somebody-else's-hobby-project server I read news on; please let me know
    if something looks odd.)

    --
    Dave Vandervies


    I've taken a vow of poverty. To annoy me, send me money.
     
    Dave Vandervies, May 29, 2007
    #2
    1. Advertising

  3. Aditya <> writes:
    > I am using recv() from socket.h in one of my TCP-client projects.
    > The problem is that the buffer variable in recv(socketDescriptor,
    > buffer, flags) points to some stray location and when the incoming
    > string is filled into it, there are trailing junk characters also.


    recv() is not a standard C function. Try comp.unix.programmer.

    > The code portion refering to it is somewhat like:
    >
    > int rcvStatus;
    > char *buffer;
    > rcvStatus = recv(socketDescriptor, buffer, flags);
    > std::cout<< buffer << std::endl;


    And that's C++, not C.

    [snip]

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, May 29, 2007
    #3
  4. Aditya

    Tor Rustad Guest

    Aditya wrote:
    > Hi
    > I am using recv() from socket.h in one of my TCP-client projects.


    Socket programming is *OFF-TOPIC*!


    > The problem is that the buffer variable in recv(socketDescriptor,
    > buffer, flags) points to some stray location and when the incoming
    > string is filled into it, there are trailing junk characters also.
    >
    > The code portion refering to it is somewhat like:
    >
    > int rcvStatus;
    > char *buffer;
    > rcvStatus = recv(socketDescriptor, buffer, flags);


    You must be kidding???

    Do you really think the recv() API is broken like this? IF recv() really
    did the memory allocation for you, you must have passed in the address
    of 'buffer'!

    Consult your man page for recv(), before posting more crap and while at
    it, order Stevens "Network Programming":

    http://www.amazon.com/UNIX-Network-Programming-Interprocess-Communications/dp/0130810819



    > std::cout<< buffer << std::endl;


    Grrrrrrrr

    --
    Tor <torust [at] online [dot] no>
     
    Tor Rustad, May 29, 2007
    #4
  5. Aditya

    Aditya Guest

    On May 29, 1:55 pm, Tor Rustad <> wrote:
    > Aditya wrote:
    > > Hi
    > > I am using recv() from socket.h in one of my TCP-client projects.

    >
    > Socket programming is *OFF-TOPIC*!
    >
    > > The problem is that the buffer variable in recv(socketDescriptor,
    > > buffer, flags) points to some stray location and when the incoming
    > > string is filled into it, there are trailing junk characters also.

    >
    > > The code portion refering to it is somewhat like:

    >
    > > int rcvStatus;
    > > char *buffer;
    > > rcvStatus = recv(socketDescriptor, buffer, flags);

    >
    > You must be kidding???
    >
    > Do you really think the recv() API is broken like this? IF recv() really
    > did the memory allocation for you, you must have passed in the address
    > of 'buffer'!
    >
    > Consult your man page for recv(), before posting more crap and while at
    > it, order Stevens "Network Programming":
    >
    > http://www.amazon.com/UNIX-Network-Programming-Interprocess-Communica...
    >
    > > std::cout<< buffer << std::endl;

    >
    > Grrrrrrrr
    >
    > --
    > Tor <torust [at] online [dot] no>


    Thanks!!!!
     
    Aditya, May 29, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    10,585
  2. Aditya
    Replies:
    4
    Views:
    366
    Aditya
    May 29, 2007
  3. deepak nayak
    Replies:
    3
    Views:
    426
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
    Oct 25, 2007
  4. Giampaolo Rodola'
    Replies:
    3
    Views:
    550
    Greg Copeland
    Nov 15, 2008
  5. Joel VanderWerf

    Socket#recv into a buffer

    Joel VanderWerf, May 16, 2008, in forum: Ruby
    Replies:
    1
    Views:
    235
    Roger Pack
    May 18, 2008
Loading...

Share This Page