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?
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()????
Thanks in advance.
On Mon, Nov 8, 2010 at 3:08 PM, ranjith kumar <[email protected]> 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?
>
> 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()????
>
recvmsg(2):
MSG_PEEK
This flag causes the receive operation to return data from the
beginning of the receive queue without removing that data from
the queue. Thus, a subsequent receive call will return the same
data.
--
Regards,
Changli Gao([email protected])
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
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?
You can't. Certainly not in antyhing remotely portable. Receipt of a UDP
datagram is a one-shot proposition - the bytes from the UDP datagram that do not
fit in the buffer(s) provided in the receive call are discarded. You might look
into readv() or recvmsg() if you want to get bytes placed into different buffers.
> 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()????
SOCK_DGRAM+UDP provides datagram semantics, not byte-stream semantics.
rick jones
try MSG_PEEK option.
Regards,
Varun
On Monday, November 08, 2010 12:38:47 pm 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?
>
> 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()????
>
> Thanks in advance.
>