Professional Web Applications Themes

A problem with recv - UNIX Programming

As I need to receive several packets in one connection, I wrote the following code. for ( ; ; ) { recv(sockFD, buf, MAX_BUF-1, 0); } My problem is, if the sender only sends a packet with data "abc", the receiver receives the data "abc" indefinitely. It seems to me that recv doesn't really clear the socket buffer that it keep retrieving the same data as if they are new. Do I miss something? Your help is highly appreciated. Thanx....

  1. #1

    Default A problem with recv

    As I need to receive several packets in one connection, I wrote the
    following code.

    for ( ; ; )
    {
    recv(sockFD, buf, MAX_BUF-1, 0);
    }

    My problem is, if the sender only sends a packet with data "abc", the
    receiver receives the data "abc" indefinitely. It seems to me that
    recv doesn't really clear the socket buffer that it keep retrieving
    the same data as if they are new.

    Do I miss something? Your help is highly appreciated. Thanx.
    Calvin Guest

  2. #2

    Default Re: A problem with recv

    >>> Calvin wrote:

    C> As I need to receive several packets in one connection, I wrote the
    C> following code.

    C> for ( ; ; )
    C> {
    C> recv(sockFD, buf, MAX_BUF-1, 0);
    C> }

    C> My problem is, if the sender only sends a packet with data "abc", the
    C> receiver receives the data "abc" indefinitely. It seems to me that
    C> recv doesn't really clear the socket buffer that it keep retrieving
    C> the same data as if they are new.
    This is impossible.

    C> Do I miss something? Your help is highly appreciated. Thanx.
    You don't check recv() return code. If it received nothing until returned,
    buffer isn't overwritten.


    -netch-
    Valentin Guest

  3. #3

    Default Re: A problem with recv

    What I assume is that recv is a blocking call that it shouln't receive
    the same message repeatedly even if I do not check the return code.

    In the actual code, I insert a printf statement right after the recv
    call and it prints the same message repeatedly.

    Valentin Nechayev <kiev.ua> wrote in message news:<nn.kiev.ua>... [/ref]
    >
    > C> As I need to receive several packets in one connection, I wrote the
    > C> following code.
    >
    > C> for ( ; ; )
    > C> {
    > C> recv(sockFD, buf, MAX_BUF-1, 0);
    > C> }
    >
    > C> My problem is, if the sender only sends a packet with data "abc", the
    > C> receiver receives the data "abc" indefinitely. It seems to me that
    > C> recv doesn't really clear the socket buffer that it keep retrieving
    > C> the same data as if they are new.
    > This is impossible.
    >
    > C> Do I miss something? Your help is highly appreciated. Thanx.
    > You don't check recv() return code. If it received nothing until returned,
    > buffer isn't overwritten.
    >
    >
    > -netch-[/ref]
    Calvin Guest

  4. #4

    Default Re: A problem with recv

    in Beitrag google.com schrieb Calvin
    unter com am 07.09.2003 7:12 Uhr:
     

    Try the following:

    for ( ; ; )
    {
    buf[recv(sockFD, buf, MAX_BUF-1, 0)] = '\0';
    printf("%s\n", buf);
    }

    Thomas Rogg

    Thomas Guest

  5. #5

    Default Re: A problem with recv

    Calvin wrote:
     

    Yoy really _should_ check the return code from recv().

    If some error happens recv() will not block - it will return -1
    and IMHO it will leave buf's content as is. Also, if you have a TCP
    socket and the remote side closes connection recv() will not block
    (there is nothing to wait for).

    HTH

    --
    Ilja.


    Ilja Guest

  6. #6

    Default Re: A problem with recv

    Thomas Rogg wrote:
     

    Maybe don't try the above.
    recv() returns -1 on errors... and I doubt that
    buf[-1] = '\0' will keep your program happy...

    HTH

    --
    Ilja.



    Ilja Guest

  7. #7

    Default Re: A problem with recv

    in Beitrag bjhbk6$ivc6j$news.uni-berlin.de schrieb Ilja Tabachnik
    unter com am 08.09.2003 9:37 Uhr:
     
    >
    > Maybe don't try the above.
    > recv() returns -1 on errors... and I doubt that
    > buf[-1] = '\0' will keep your program happy...[/ref]
    Right..

    for ( ; ; )
    {
    int n;
    n = recv(sockFD, buf, MAX_BUF-1, 0);
    if(n > 0)
    {
    buf[n] = '\0';
    printf("%s\n", buf);
    }
    }

    That'll do it.

    -- Thomas

    Thomas Guest

  8. #8

    Default Re: A problem with recv

    On Mon, 08 Sep 2003 02:07:46 +0200
    Thomas Rogg <com> wrote:
     
    >
    > Try the following:
    >
    > for ( ; ; )
    > {
    > buf[recv(sockFD, buf, MAX_BUF-1, 0)] = '\0';
    > printf("%s\n", buf);
    > }[/ref]
    You are asking for a segv when recv returns a -1, and possibly an
    infinite loop when recv returns 0.
    IMHO, always check the return value. As others have said,
    recv returns -1 on error, but can also return 0.


    --
    Jerry Feldman <gaf-nospam-at-blu.org>
    Boston Linux and Unix user group
    http://www.blu.org PGP key id:C5061EA9
    PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
    Jerry Guest

  9. #9

    Default Re: A problem with recv


    "Thomas Rogg" <com> wrote in message
    news:BB8194F2.15B%com...
     

    What happens when 'recv' returns -1?

    DS


    David Guest

Similar Threads

  1. High Recv-Q Netstat
    By sarahCool in forum Macromedia Flash Flashcom
    Replies: 0
    Last Post: January 17th, 11:23 AM
  2. Pb with IO::Socket::INET and recv
    By Sébastien Cottalorda in forum PERL Miscellaneous
    Replies: 1
    Last Post: September 16th, 07:47 AM
  3. recv and MSG_WAITALL
    By Barry Margolin in forum UNIX Programming
    Replies: 1
    Last Post: August 1st, 12:40 AM
  4. no recv
    By David Schwartz in forum UNIX Programming
    Replies: 1
    Last Post: July 9th, 03:38 PM
  5. Receive binary file through recv
    By phil-news-nospam@ipal.net in forum UNIX Programming
    Replies: 0
    Last Post: June 24th, 11:37 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139