Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default socket receive

    Hi, all,

    I have several problems when i develop the socket receive code, i am
    using recv() to get the data with gcc on the linux platform. The
    questions are:
    1) do i need flush the receive buffer every time recv() runs?
    2) how to avoid losing data using recv(), 'cause the buffer size is
    limited(1K in my case), using a bigger buffer? Is there any better
    ways?

    Can anyone help me out? Thanks a lot!

    Roy
    roy Guest

  2. Similar Questions and Discussions

    1. Can't download, just receive a red X
      I've tried and tried to download Flash Player and when I tell it to download and install all I get is a box with a red X. I am running Windows XP...
    2. receive e-mails
      hi guys I am looking for a way to read & download my e-mails from my pop3 account from another server by using ASP(vb) If you know anything about...
    3. Socket.accept problem via Socket.for_fd($stdin.fileno)
      Hi, I am experiencing a rather infuriating problem with Socket.accept on Windows XP. The problem exists when I try to create a Socket from...
    4. Distinguishing between socket buffer full & socket disconnected
      I am using the IO::Select method can_write() to flow control the writing of a large amount of data to a socket, where the writer may well run ahead...
    5. Dynamically receive IPs
      Hello, I have developed Firewall on FreeBSD 4.7. When any packet is received by Firewall, IP is search in relational database. If firewall finds...
  3. #2

    Default Re: socket receive

    >>> roy wrote:

    r> I have several problems when i develop the socket receive code, i am
    r> using recv() to get the data with gcc on the linux platform.

    You don't get the data with gcc. You possibly compile program with gcc,
    there are "two big differences" (tm) here.

    r> The
    r> questions are:
    r> 1) do i need flush the receive buffer every time recv() runs?

    You can't flush receive buffer unless you are kernel developer.

    r> 2) how to avoid losing data using recv(), 'cause the buffer size is
    r> limited(1K in my case), using a bigger buffer? Is there any better
    r> ways?

    You don't lose any data unless socket is datagram one and a datagram is
    larger than 1K.


    -netch-
    Valentin Nechayev Guest

  4. #3

    Default Re: socket receive


    "roy" <roy_liao2001@yahoo.ca> wrote in message
    news:5c23f6ff.0307141759.1a201b67@posting.google.c om...
    > Hi, all,
    > I have several problems when i develop the socket receive code, i am
    > using recv() to get the data with gcc on the linux platform. The
    > questions are:
    > 1) do i need flush the receive buffer every time recv() runs?
    Huh? What does this mean?
    > 2) how to avoid losing data using recv(), 'cause the buffer size is
    > limited(1K in my case), using a bigger buffer? Is there any better
    > ways?
    Why do you think you're going to lose data? I would suggest using at
    least a 4Kb buffer unless you have some special reason to use a smaller one.
    A 1Kb buffer may mean more calls to 'recv' than a 4Kb buffer, so performance
    may suffer.

    DS


    David Schwartz Guest

  5. #4

    Default Re: socket receive

    Hi,

    Yeah, I am using gcc compile tool.

    I am developing the code for the client, send the request to the
    server and get the data, Telnet protocol is implemented here.
    1)The reason for flushing the receive buffer is that I am worrying
    about conflicts between two reads, which means if the first time the
    recv buf got 900bytes, and the second time just 100bytes, is there any
    conflicts here?
    2)After the client sent the request to server, the data is sent
    continuouly from server, I want to get the data, do processing, and
    get the new data, which is sth like a close loop control (feedback)
    system. In this case, if the client receive is not bigger enough or
    not grab the data in time, some data will be lost.

    Thanks a lot.
    Roy
    roy Guest

  6. #5

    Default Re: socket receive


    "roy" <roy_liao2001@yahoo.ca> wrote in message
    news:5c23f6ff.0307161014.200e25ff@posting.google.c om...
    > For the second one, during debugging, I found the receive buffer was
    > full sometimes, recv() function may discard some data in case of
    > overflow?
    Your question doesn't make sense. Assuming the receive buffer is full,
    why would the 'recv' function discard any data?
    > Or due to TCP flow control, the data won't be sent out from
    > server in case of client recv buf is full?
    The TCP implementation can do any number of things, but it won't lose
    data unless it's broken.

    DS


    David Schwartz Guest

  7. #6

    Default Re: socket receive

    In article <5c23f6ff.0307171036.2ad6159a@posting.google.com >,
    roy <roy_liao2001@yahoo.ca> wrote:
    >I am using
    >
    >char receiveBuf[];
    >recv(handle, receiveBuf, length, 0);
    >
    >Here, receiveBuf is what I am talking about. It's program's buffer.
    >For now, length=1024, shall I make it longer? How do I select the
    >value of length?
    You need to specify a size in the array declaration. Length should be
    whatever that size is. E.g.

    #define RCVBUFSIZE 1024
    char receiveBuf[RCVBUFSIZE];
    int bytes_read;
    bytes_read = recv(handle, receiveBuf, RCVBUFSIZE, 0);

    If the sender sends more than 1024 bytes, you should call recv() in a loop,
    getting up to 1024 bytes each time. bytes_read will contain tha number of
    bytes that were returned in each call.

    Nothing will ever be lost, you just may have to call recv() several times
    to get it all.

    --
    Barry Margolin, [email]barry.margolin@level3.com[/email]
    Level(3), Woburn, MA
    *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
    Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
    Barry Margolin Guest

  8. #7

    Default Re: socket receive


    "roy" <roy_liao2001@yahoo.ca> wrote in message
    news:5c23f6ff.0307171036.2ad6159a@posting.google.c om...
    > char receiveBuf[];
    > recv(handle, receiveBuf, length, 0);
    > Here, receiveBuf is what I am talking about. It's program's buffer.
    > For now, length=1024, shall I make it longer? How do I select the
    > value of length?
    It depends upon three factors:

    One is the protocol. If the protocol consists of very short messages
    that almost always require a reply before the protocol can continue, then
    larger buffers won't matter because there will almost never be much data
    pending.

    Two is memory availability. If you're handling 10,000 connections, using
    a 64Kb buffer might not be very practical if you use a different buffer for
    each connection. If you use one buffer for all connections, then this isn't
    much of an issue, but you have to store the 'leftovers' (data you read but
    can't use immediately) somewhere.

    Last is performance. If you use a 1Kb buffer, you may need to call
    'recv' up to twice as many times as if you use a 2Kb buffer. You may also
    need to enter your buffer processing code twice as many times (though with
    half as much data each time). So larger buffers generally equate to higher
    performance.

    Here are some rules of thumb:

    First, if the protocol your using has some limit on how large a single
    command/transaction can be, try to pick a buffer size larger than that
    limit. Otherwise, you may get a situation where your buffer is full buy you
    have to read more data from the socket before you can process any of it.
    Ouch.

    Otherwise, 8Kb should be enough to get maximum performance. The cost of
    copying/processing 4Kb or more of data probably drowns out the cost of a
    system call. Plus, 8Kb is sufficiently larger than common packet sizes that
    it's unlikely a larger buffer will actually receive any more data at one
    time anyway.

    Lastly, if there's some reason you want to go lower than 8Kb, don't go
    below 2Kb. Typical MTU sizes are around 1,500 bytes and getting too close to
    (or below) the MTU will cause excessive generation of small packets and
    excessive Nagling (increased latency due to suboptimal prediction of future
    data sizes).

    If you know you are or might be dealing with larger MTUs along the whole
    path, say GigE with jumbo frames, and your protocol may require sending
    large amounts of data in one direction without waiting for data in the other
    direction, it may make sense to use much larger buffer sizes, up to twice
    the maximum frame size.

    DS


    David Schwartz Guest

Posting Permissions

  • You may not post new threads
  • You may 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