Sending 2k udp packets a second

Mar 28, 2012 at 5:50 AM

I need to send 2000 udp packets a second, the maxium packet size is 1400 bytes.

I am trying to do this with pcap.net using the SendBuffer.

Below is the code I am using and I can not send out the number of packets needed in the time, I need to send a packet every 2 milliseconds.  When I send a list of 100 I see times over 100 milliseconds being reported.  What am I doing wrong?  Is my required send rate to high?

IList<LivePacketDevice> allDevices;
        PacketCommunicator outputCommunicator;
        PacketBuilder packetBuilder;
        EthernetLayer ethernetLayer;
        IpV4Layer ipV4Layer;
        UdpLayer udpLayer;
        uint sendBufferLength = 1000000;
        PacketSendBuffer sendBuffer;
        PayloadLayer payloadLayer;
 
        public bool Startup(IPAddress multicastAddress, int port)
        {
            allDevices = LivePacketDevice.AllLocalMachine;
 
            outputCommunicator = allDevices[0].Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000);
 
            // Ethernet Layer
            ethernetLayer = new EthernetLayer
            {
                Source = new MacAddress("D0:27:88:7D:49:9B"),
                Destination = new MacAddress("01:00:5E:01:01:6C"),
            };
 
            // IPv4 Layer
            ipV4Layer = new IpV4Layer
            {
                Protocol = IpV4Protocol.Udp,
                Ttl = 255,
                Destination = new IpV4Address(multicastAddress.ToString()),
                Source = new IpV4Address("10.127.0.77"),
                Identification = 0,
            };
 
            udpLayer = new UdpLayer();
            udpLayer.DestinationPort = (ushort)port;
            udpLayer.SourcePort = (ushort)port;
            udpLayer.CalculateChecksumValue = true;
 
            payloadLayer = new PayloadLayer();
 
            sendBuffer = new PacketSendBuffer(sendBufferLength);
 
            return true;
        }
 
        public bool Shutdown()
        {
            if (sendBuffer != null)
            {
                sendBuffer.Dispose();
                sendBuffer = null;
            }
 
            if (ipV4Layer != null)
                ipV4Layer = null;
 
            if (outputCommunicator != null)
            {
                outputCommunicator.Dispose();
                outputCommunicator = null;
            }
 
            return true;
        }
 
        public bool SendData(byte[] data)
        {
            return SendData(data, data.Length);
        }
 
        public bool SendData(byte[] data, int dataLength)
        {
            try
            {
                payloadLayer.Data = new Datagram(data);
 
                // Create the builder that will build our packets
                packetBuilder = (new PacketBuilder(ethernetLayer, ipV4Layer, udpLayer, payloadLayer));
 
 
                Packet packet = packetBuilder.Build(DateTime.Now);
                //outputCommunicator.SendPacket(packet);
                sendBuffer.Enqueue(packet);
 
                return true;
            }
            catch (Exception ex)
            {
                TraceQueue.Trace("Exception sending pcap data: " + ex.Message, TracingLevel.Warning, this);
                return false;
            }
        }
 
	//called when the packets need to be sent
        public void TransmistPackets()
        {
            if (sendBuffer != null && outputCommunicator != null)
            {
                outputCommunicator.Transmit(sendBuffer, false);
                sendBuffer.Dispose();
                sendBuffer = new PacketSendBuffer(sendBufferLength);
            }
        }
Coordinator
Mar 30, 2012 at 6:27 PM

Hi blackendnutria,

 

How do you know when did the packets arrived?

You might have a delay in capturing the packets, if you can elaborate on what are the devices being used, how are they connected and how do you capture the packets that would be helpful.

Also, please note that 2000 packets of 1400 bytes per second is over 21Mbps, which is not supported in every network, and may cause delay in sending the packets.

 

Boaz.