public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Michael Tokarev <mjt@tls.msk.ru>
To: ranjith kumar <ranjithproxy@gmail.com>
Cc: linux-kernel <linux-kernel@vger.kernel.org>
Subject: Re: how to read one udp packet with more than one recvfrom() calls?
Date: Mon, 08 Nov 2010 10:51:57 +0300	[thread overview]
Message-ID: <4CD7AC1D.4010701@msgid.tls.msk.ru> (raw)
In-Reply-To: <AANLkTi=40758wF4Z6GDfm809miA-hnCnSQdwA9bALX0+@mail.gmail.com>

08.11.2010 10:08, ranjith kumar wrote:
> Hi,
> 
> I  have implemented client and server programs using udp
> protocol(files are attached).
> UDP packet size is 500bytes.
> 
> I want to read these 500bytes in two calls to recvfrom(). First time
> reading 100bytes and second time 400bytes.
> How to do this?

There's no way to do so.  According to the udp(7) manpage:

 All receive operations return only one packet.  When the packet is
 smaller than the passed buffer, only that much data is  returned; when
 it is bigger, the packet is truncated and the MSG_TRUNC flag is set.
 MSG_WAITALL is not supported.

This is intentional, because the kernel does not keep the received
packets, it either delivers them right to the application or drops
them.  There's no sophisticated queue/stream management like for tcp.

For current speeds, one wants to _reduce_ number of system calls
made, not to increase them as you're trying to do, in order to
process as many data in one go as possible.  This is why new
system calls like mrecvfrom are being proposed.

> When I tried to change the third argument of recvfrom(size_t len),
> from 500 to 100, first 100bytes are read correctly.
> But when I call recvfrom() second time with len=400, it is reading the
> first 400bytes of "next udp packet".
> Why? Isn't it possible to read one udp packet in two calls to
> recvfrom()/read()????

This is how UDP is designed to work, and how it works on other
operating systems too.

/mjt

  parent reply	other threads:[~2010-11-08  7:52 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-11-08  7:08 how to read one udp packet with more than one recvfrom() calls? ranjith kumar
2010-11-08  7:12 ` Changli Gao
2010-11-08  7:51 ` Michael Tokarev [this message]
2010-11-08 18:31 ` Rick Jones
2010-11-09  5:35 ` Varun Chandramohan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4CD7AC1D.4010701@msgid.tls.msk.ru \
    --to=mjt@tls.msk.ru \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ranjithproxy@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox