Sending UDP packets at a specified rate

  • Permalink
  • submit to reddit
  • Email
  • Follow


Hello,

I am trying to understand how to write a simple network program that
sends UDP packets at a specified rate.

For example, let's say I want to send 500-byte packets at 40 Mbit/s

This means I want to send one packet every 0.1 ms

In my mind, the naive way to do that would be to write:

(pseudo code...)

while (1)
{
  send(500-byte UDP packet);
  sleep(0.1 ms);
}

But I don't think any of the sleep functions (nanosleep?) will let me
sleep for such a short period, will they?

Maybe I could send 10 packets, then sleep 1 ms? That might solve the
sleep problem, but my traffic would be very bursty.

I suppose I could waste time in a busy loop, but I don't like that solution.

Is there a (simple?) way to send 500-byte packets every 0.1 ms without
pegging the CPU to 100%?
0
Reply Spoon 4/18/2006 10:57:55 PM

See related articles to this posting


(Originally posted to comp.unix.programmer)

Spoon wrote:

> I am trying to understand how to write a simple network program that
> sends UDP packets at a specified rate.
> 
> For example, let's say I want to send 500-byte packets at 40 Mbit/s
> 
> This means I want to send one packet every 0.1 ms
> 
> In my mind, the naive way to do that would be to write:
> 
> (pseudo code...)
> 
> while (1)
> {
>   send(500-byte UDP packet);
>   sleep(0.1 ms);
> }
> 
> But I don't think any of the sleep functions (nanosleep?) will let me
> sleep for such a short period, will they?
> 
> Maybe I could send 10 packets, then sleep 1 ms? That might solve the
> sleep problem, but my traffic would be very bursty.
> 
> I suppose I could waste time in a busy loop, but I don't like that solution.
> 
> Is there a (simple?) way to send 500-byte packets every 0.1 ms without
> pegging the CPU to 100%?
0
Reply Spoon 4/19/2006 12:30:11 PM

Spoon wrote:
> Hello,
>
> I am trying to understand how to write a simple network program that
> sends UDP packets at a specified rate.
>
> For example, let's say I want to send 500-byte packets at 40 Mbit/s
>
> This means I want to send one packet every 0.1 ms
>
> In my mind, the naive way to do that would be to write:
>
> (pseudo code...)
>
> while (1)
> {
>   send(500-byte UDP packet);
>   sleep(0.1 ms);
> }
>
> But I don't think any of the sleep functions (nanosleep?) will let me
> sleep for such a short period, will they?

you could use usleep() but the timing won't be exactly right on a
non-real time os unless maybe you use a hardware timer and create an
interupt every X amount of time.

> Maybe I could send 10 packets, then sleep 1 ms? That might solve the
> sleep problem, but my traffic would be very bursty.

if that's an option it doesn't even matter if it's real time, use
usleep() that's what you are looking for.

> I suppose I could waste time in a busy loop, but I don't like that solution.

sure you could do that too.

> Is there a (simple?) way to send 500-byte packets every 0.1 ms without
> pegging the CPU to 100%?

using usleep().  cheers.

0
Reply purple_stars 4/19/2006 6:37:50 PM

> you could use usleep() but the timing won't be exactly right on a
> non-real time os unless maybe you use a hardware timer and create an
> interupt every X amount of time.

and, just as with nanosleep(), not all systems that offer a usleep()
actually have that fine a granularity. To wit, from the usleep manpage
under HP-UX 11i:

      Implementations may place limitations on the granularity of
      timer values. For each interval timer, if the requested timer
      value requires a finer granularity than the implementation
      supports, the actual timer value will be rounded up to the next
      supported value.

rick jones
-- 
oxymoron n, commuter in a gas-guzzling luxury SUV with an American flag
these opinions are mine, all mine; HP might not want them anyway... :)
feel free to post, OR email to rick.jones2 in hp.com but NOT BOTH...
0
Reply Rick 4/19/2006 9:44:41 PM

purple_stars wrote:

> Spoon wrote:
> 
>>I am trying to understand how to write a simple network program that
>>sends UDP packets at a specified rate.
>>
>>For example, let's say I want to send 500-byte packets at 40 Mbit/s
>>
>>This means I want to send one packet every 0.1 ms
>>
>>In my mind, the naive way to do that would be to write:
>>
>>(pseudo code...)
>>
>>while (1)
>>{
>>  send(500-byte UDP packet);
>>  sleep(0.1 ms);
>>}
>>
>> But I don't think any of the sleep functions (nanosleep?) will let me
>> sleep for such a short period, will they?
> 
> you could use usleep() but the timing won't be exactly right on a
> non-real time os unless maybe you use a hardware timer and create an
> interupt every X amount of time.

The man page for usleep in Linux states:

   This function is obsolete. Use nanosleep(2) or setitimer(2) instead.

>> Maybe I could send 10 packets, then sleep 1 ms? That might solve the
>> sleep problem, but my traffic would be very bursty.
> 
> if that's an option it doesn't even matter if it's real time, use
> usleep() that's what you are looking for.

I'll give nanosleep() a try.

>> I suppose I could waste time in a busy loop, but I don't like that solution.
> 
> sure you could do that too.

But I would hog the CPU even though I only need 2-3% of it on average...

Ugly.
0
Reply Spoon 4/20/2006 9:04:12 AM

Spoon wrote:
> purple_stars wrote:
[snip]
> > if that's an option it doesn't even matter if it's real time, use
> > usleep() that's what you are looking for.
>
> I'll give nanosleep() a try.

i learned something too, i didn't know there was such a thing as
nanosleep() :)

0
Reply purple_stars 4/20/2006 2:59:21 PM

The easy way to do it is just to keep track of the amount of bandwidth
you are using, and if it gets to high, sleep a bit before you send the
next bit.

If you are operating in contexts where you can't sleep, then just keep
a linked list of packets you need to send. Send them as appropriate.

For example, your send logic can go like this:

1) Is bandwidth usage high? If not, go to step 4.

2) Are there too many packets queued, if yes, return an error.

3) Queue the packet for later, return.

4) Is the bandwidth usage low and there are packets queued? If so, send
some of them and update the bandwidth estimate.

5) If we emptied the queue, send this packet (updating the bandwidth
estimate) and return. If not, queue this packet and return.

DS

0
Reply davids 4/20/2006 4:25:01 PM

You may find that the nanosleep function uses a 10 ms. precision, such
that the smallest delay such as 1 ms. takes at least 10 ms.

You can work around this limitation by tracking the actual sleep time
and feeding any excess sleep time into the next delay calculation.
Gettimeofday returns very accurate time information.  This is how I
solved this very problem.  

Bill Lindquist

0
Reply William 4/24/2006 3:04:28 PM

William.Lindquist@lmco.com wrote:
> You may find that the nanosleep function uses a 10 ms. precision,
> such that the smallest delay such as 1 ms. takes at least 10 ms.

> You can work around this limitation by tracking the actual sleep
> time and feeding any excess sleep time into the next delay
> calculation.

Don't you still end-up with bursts as the excess sleep time becomes no
sleeps for a while?

> Gettimeofday returns very accurate time information.  This is how I
> solved this very problem.

Careful there - I have seen - in the past anyway - gettimeofday only
increment by "large" chunks on some platforms - old Irix comes to mind
- I think it was an issue for SPECweb96 benchmarking.  And just about
any "gettimeofday" replacement under Windows may only increment in 10
ms chunks. (eg glib g_get_current_time() and its use of
GetSystemTimeAsFileTime())

When one doesn't need actual wall-clock time, going with something a
bit more platform-specific - at least for measuring "time" may be
useful/accurate/precise.  gethrtime() on those platforms which have
it, or something along those lines.  It doesn't solve the problem of
getting short sleep times, but it will tend to allow much finer
granularity of "time" measurement.

rick jones

I'm always confusing accuracy and precision

If one has a multiple CPU system, or even multiple systems on the
network, one could I suppose "sacrifice" one CPU to sit and spin
watching time pass and "chime" to affect the short timeouts by sending
messages to the other processes which wanted to have short timeouts
but not sit and spin.  Probably still burn a lot of cycles, but
perhaps not as many as having everyone spinning

-- 
No need to believe in either side, or any side. There is no cause.
There's only yourself. The belief is in your own precision.  - Jobert
these opinions are mine, all mine; HP might not want them anyway... :)
feel free to post, OR email to rick.jones2 in hp.com but NOT BOTH...
0
Reply Rick 4/24/2006 5:28:55 PM

Gettimeofday is much more accurate then usleep (using IRIX 6.5.4 and
LINUX).  And there is no guarantee that the process will be immediately
restarted if other processes are competing.

Since usleep never returned to me in less than 10 ms. I adjusted the
delay time on the next iteration to compensate.  This causes some
acceptable burstiness.  So I might ask for a 5 ms. delay, get 10 ms.
and skip the delay the next iteration.  Overall the throughput gets me
what I want.

Otherwise, I would usleep for 5 ms. and delay 10 ms. each iteration
halving my throughput.

0
Reply William 4/25/2006 11:53:27 PM

or you could use the "select" system call with all fields empty except
for the timeout. timeout is of struct timeval* type, so you could put
there as many/few usec you want.

0
Reply doina 4/26/2006 8:51:14 AM

doina babu wrote:
> or you could use the "select" system call with all fields empty except
> for the timeout. timeout is of struct timeval* type, so you could put
> there as many/few usec you want.
> 
The minimum timeout for select is still limited by the system tick.

-- 
Ian Collins.
0
Reply Ian 4/26/2006 9:11:58 AM
comp.unix.programmer 10668 articles. 64 followers. Post

11 Replies
1417 Views

Similar Articles

[PageSpeed] 47


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

Sending UDP packets
Hi, I wonder if there is a way to send UDP/TCP packets to other hosts from within plpgsql functions? As far as I know there's no such built-in function. (Am I right?) My question is: is there any third party library for Windows (.dll) or Linux (.so) that brings such functionality to plpgsql? Thanks in advance, mrupio mrupio <baal3_WSTAWKA_ANTYSPAMOWA_@op.pl> wrote: > Hi, > > I wonder if there is a way to send UDP/TCP packets to other hosts from > within plpgsql functions? > As far as I know there's no such built-in function. (Am I right?) >...

UDP packet send / receive
i am unable to send an UDP packet to embedded board on which IP was not implemented. MAC Address and port number were implemented. please let me known how to send UDP packet to Embedded board through MAC Address and port. please help me balajiyalam@gmail.com wrote: > > i am unable to send an UDP packet to embedded board on which IP was not > implemented. MAC Address and port number were implemented. > > please let me known how to send UDP packet to Embedded board through > MAC Address and port. > > please help me You are far from getting UDP datagrams throug...

Sending UDP packets over Ethernet
I've been tasked to write some code for an FPGA to interface to 10BASE- T Ethernet using differential drivers and receivers. The information is one way, transmit only! I will have an IP address within the network range, give myself a MAC number and know the MAC and IP address of the destination PC. Do I require any ARP or any other protocol? Will it just work, with the destination PC receiving UDP packets? Fred Hi, On Mon, 2008-09-29 at 08:10 -0700, Fred wrote: > Do I require any ARP or any other protocol? Will it just work, with > the destination PC receiving UDP packets? Y...

Sending big UDP packet
I'm actually working with UDP1.0.9 to send UDP packets. Them problem is: It seems the biggest possible packet size for tcludp is 4096 B (4 KB). But in the current situation I need to send packets with 15-20 KB size in the local net. Is there any chance to send such big packets? Using Tcl 8.5 Hope you can help me. Horst2007 wrote: > I'm actually working with UDP1.0.9 to send UDP packets. > > Them problem is: It seems the biggest possible packet size for tcludp > is 4096 B (4 KB). > But in the current situation I need to send packets with 15-20 KB size...

How to send udp packets over the internet.
I want to make a program using c/c++ that can make certain requests to a web server in the internet. For example, GET / HEADER / 1.1 or it can request for a file(downloading file). I had made client and server programs where both maintain the same protocols. But I'm bit confused how to do it for the internet where I don't know what should be the packet structure and other constraints! Please give me hints when can I start from. ================ Newbie, Just for Fun >I want to make a program using c/c++ that can make certain requests to >a web server in the internet. For exam...

Sending Hop Limited UDP packets
Is there a way to send a UDP packet with a specific TTL however itmust be a unicast address instead of multicast address. Any ideas?regardsUsman Ismail Ashoka! wrote:> Is there a way to send a UDP packet with a specific TTL however it> must be a unicast address instead of multicast address. Any ideas?> > regards> Usman Ismail> I think you would have to write your own DatagramSocketImpl. But I'm curious as to why you would want a TTL on a unicast UDP packet. Do you not want it to get to it's address?-- Knute Johnsonemail s/nospam/knute/ On Tue, 10 Apr 2007 09:15:49 -...

xpc udp packets uneven rate
I have an xpc target application sending udp packets every 40 msec (time step in udp send block is .04 s). On the other side I am timing the packets and 2/3 of the packets come in every 40 msec as expected (+- 2 msec), but then there is a delay of 280 msec and the rest of the packets come piled up in a burst. This cycle repeats every 1 second (the slowest rate of the application is 1 second so that may explain the regularity). I know the problem is in the xpc application and not the network because I run different test apps with no load (just 3 blocks for sending a dummy packet at 40 msec) a...

Package for sending SNMP , UDP packets
I have an equipment that understand SNMP and UDP packets . The equipment is having a Network Management System to access it. The NMS actually sends SNMP packets when I click on any button in its GUI. I want to do the same using TCL script. I want to know is there any package that exist which can help me create, send and receive SNMP and/or UDP packets. I am not getting any idea how to do it as I have never done Socket programming. Ranjan wrote: > I want to know is there any package that exist which can help me > create, send and receive SNMP and/or UDP packets. http://wiki.tcl.tk/sc...

HELP!!! How do I send an ACK packet in UDP?????
I need my udp server to send an ACK back to the client when it successfully receives data from the client to let it know not to retry the send (yes, I do know this is how TCP works but must be in UDP) I am using this example code I found on the net for the server, I need to figure out how to get the ip and port that the client transmitted from and return an ack response. Any help would be greatly appreciated.......... from socket import * # Set the socket parameters host = "localhost" port = 21567 buf = 1024 addr = (host,port) # Create socket and bind to address UDPSock = socket(A...

Sending data types in a UDP packet
In the specific protocol I'm writing a script for, I need to send a lot of different data types through the packet, such as U32, and S8. (Unsigned 32 bit, signed 8 bit). Does ruby support these types of variables? How can I send them through a packet? I'm still learning about networking and ruby, so if I said anything that doesn't make sense, please forgive me. -- Posted via http://www.ruby-forum.com/. Isaac Wagner wrote: > In the specific protocol I'm writing a script for, I need to send a lot > of different data types through the packet, such as U32, and S8. > (Un...

tools for sending bytes at a specified rate
Hi All, I tried googling ... but maybe much choice of keywords were not good enough. I am searching for a tool similare to unix "netcat" where I could pipe the contents of a file and it would send it to a port. But I also wish to control the speed of transmission. I need this for testing my program, to estimate the max. rate that it could handle. Please provide any pointers in this regard. Thanks in advance. try hping2 http://sourceforge.net/projects/hping2/ You can construct packets with command line options and contol how often packets are sent by ano...

Sending UDP packets to boradcast with pnet?
Hi, is it possible to send UDP packets to boradcast with pnet.m? I have following matlab code. %Close any stale sockets before trying to create new ones pnet('closeall'); IPaddr = '10.0.0.255'; pktDataTx = [1 111]; %Prepare the socket udp_Sync = pnet('udpsocket', 9001); pnet(udp_Sync, 'setreadtimeout', 1); pnet(udp_Sync, 'udpconnect', IPaddr, 70); pnet(udp_Sync, 'write', int32(pktDataTx)); pnet(udp_Sync, 'writepacket', IPaddr, 70); and when I try to run it I get following error. sendto() / send(): Permission deni...

Send UDP packets over a certain interface
Hi, I can reach a host through two different gateways (I have redundant Internet connection using 2 interfaces). I know it's useless to have two default routes, but how can I force certain UDP packets to use one of the interfaces I choose? For example, I want to send a UDP packet via "eth1" and the next one via "ppp0". How can I do that? Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.100.101 * 255.255.255.255 UH 0 0 0 ppp0 10.0.0.0 * 2...

Sending UDP packets to local machine
Since Windows XP SP2 has broken some of the raw socket support (and therefore our application), I am looking for a way around this problem: Our application needs to send multicast packets with a spoofed source address to other standard applications (e.g. Windows Media Player) on the local machine (and NOT onto the network). Currently we do this by using a raw socket to send an IP packet from the machine with multicast TTL=0 (which stops packets being sent onto the physical interface, but allows them to be looped back to the local machine). However, Windows XP SP2 will no longer a...

sending raw packet from specified interface in solaris
Hi All, I have to network interfaces configured on same subnet. but I want to send particular packet from specified interface. can you please tell me any available mechanism..? I tried with normal socket programming , SOCK_RAW but it is not working ..... any help is welcome.. Thanks... LinuxGuy schrieb: > Hi All, > > I have to network interfaces configured on same subnet. but I want to > send particular packet from specified interface. can you please tell me > any available mechanism..? > > I tried with normal socket programming , SOCK_RAW but it is not working >...

How to format/send/receive UDP packet in C?
I'd like to ask about how to format/send/receive UDP datagram packet in C language. The server that I'd like to send the packet to is listening on port 1500. When I succesfully connect to that port, it's gonna create and send a packet back to the client. The only code I need to write is the client-side code. The problem is, I don't have any idea how to: 1. Send data packet to server (I'm going to send the data in Hex format) 2. Receive data back from that server I also don't have any idea how to format the packet. Please give me a simple explanation. Thank you so much...

Drawbacks of sending UDP packets larger than MTU?
Hi list, I'm wondering about the reliability of sending UDP packets that are larger than the MTU of the link. I know that, contrary to TCP, UDP does not do fragments, but IP has some fragmentation ability, so it should take care of that regardless. Even though IPv6 with it's PMTU discovery doesn't fragment on intermediate nodes, the endpoint stacks supposedly still do fragmentation, if I've read that correctly, so the IP version shouldn't really matter. Therefore, it would be possible to send UDP packets that are larger than the MTU, and still have them arrive either prop...

How can i attach a user-specified header to each UDP packet?
Hello, &nbsp; i have here a question about UDP: &nbsp; i want to stream live-videos using UDP. To avoid/observe some possible problems that could occur (e.g. wrong packet-order, packet loss etc.)&nbsp;i am planning to add a header to each UDP packet with e.g. frame-nr. , frame-size, packet-nr... &nbsp; Question: &nbsp; 1. (How) Can i control the UDP packet size in LabVIEW? 2. If possible, how can i add a user specified header to each/certain (e.g. the first or the last&nbsp;packet of the frame)&nbsp;packet? &nbsp; Thanks! &nbsp; WLAN &nbsp; &nbsp;Mes...

Does anyone know how to send UDP packet through HTTP proxy?
Does anyone know how to send UDP packet through HTTP proxy? thanks a lot On Feb 12, 1:29 am, "feiman" <lfeiman...@gmail.com> wrote: > Does anyone know how to send UDP packet through HTTP proxy? > > thanks a lot Your question doesn't make sense. Why don't you tell us what your actual problem is rather than trying to get us to make your suggested solution work. It's like someone asked, "Does anyone know how to get a car to hold 50 tons of rock?". Odds are there's a way to solve whatever your problem is, but getting...

How to trigger the sending of udp packet with xpc target / matlab
Hello everyone, I have written a control algorithm with matlab simulink running on a realtime xpc target mashine, communication with another computer. It's puropse is to calculate position data out of given inputs and send this data to the other mashine, when being asked to. My problem is, that the xpc UDP block is sending a packet at each sample interval every 0.01 seconds. I tried everything using enable and triggered subsystems, but could not get the UDP block to only send a packet when I want it to. It seems to be programmed in such a way, that it always sends a packet every 0.01s, n...

How can I send a UDP packet to a group of IP Address
Hi There, How can I send a UDP packet to a Group of IP address on the same Subnet for Example 192.168.1.30 to 192.168.1.40 with out using multicast Anand V.T. In article <bb8a013.0501312138.6762f37@posting.google.com>, Anand <anand.vt@gmail.com> wrote: :How can I send a UDP packet to a Group of IP address on the same :Subnet :for Example 192.168.1.30 to 192.168.1.40 :with out using multicast Solution 1: for (octet=30; octet <= 40; octet++ ) { IP = 0xc0780100 | octet; send_packet_to_IP( IP ); } Solution 2: IP = 0xc07801ff; send_pa...

udp packet send and receive through real-time windows target
i am trying to make an xpc and a real-time windows target talk.. xpc machine is sending out udp packets (which would be uint8 data of certain length) and is also can accept udp packet through another port (again, the uint8 packets, which would get converted into 'double' values and then subsequently used in the model). i can easily send and receive the udp packets in the command line window by creating udp object, and also in the simulink using the send and receive blocks - thus can talk to xpc without any troubles. i know real-time windows target has started supporting udp packets s...

Tuning net parameters for sending a maximum number of UDP packets
Hello, I am trying to send a large number of UDP packets through one nick. Each packet is smaller than 200 byes. I am working with a gigabit ethernet on a LAN. I am testing it with x86_64 intel processor and with Intel e1000 nic. The nic works with coalescent interrupts, aka NAPI. I tried to send about 50,000 packets and I see that there is a loss of about 5%-10%. I want to be able to send more than 50,000 packets, to tune the machine and be able to squeeze resources and send as much as possible. I had looked for linux networking tunables which will allow me to be able to send more packets...