Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: udp LAN Download
Hi!
I'm trying to write a stress-tester for lan.
 
Looking at the Network-Load in the Taskmanager it seems i can only transfer about 500MBit/sec on a GBit Networkcard (i checked all the Adapter settings)
Sending from Both Sides gives my a network load of almost 100%.
But sending the received packet back only gives me about 70% network-load.
Why can't I send more packets per second?
The funny thing is, sending from both sides at a packet size of 1024bytes gives me approx 70% load.
Sending a 10240byte (way too big!)Packet Server to client(while sending a 1024byte client to server) gives me almost 100% load.
 
can anyone explain this?
 
Best Regards Daniel
 
Client sending to Server CODE
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Threading;
using System.Timers;
using System.Net;
using System.Diagnostics;
 

 
namespace UDP_StressClient
{
    class Program
    {
        private static System.Timers.Timer timer;
        private static Receiver rcv;
        private static Sender snd;
        private const int PACKET = 1024;
        public const int OVERHEAD = 28; //udp
        private static Stopwatch sw = new Stopwatch();
        static void Main(string[] args)
        {
            Thread supervisor = new Thread(threadSupervisor);
            supervisor.Start();
 
        }
 
        public static void threadSupervisor()
        {
            UdpClient udpClient = new UdpClient(399);
            snd = new Sender(udpClient);
            Thread threadSender = new Thread(snd.Send);
            //threadSender.IsBackground = false;//?
            threadSender.Start();
 
            rcv = new Receiver(udpClient);
            Thread threadReceiver = new Thread(rcv.ReceiveData);
            //threadReceiver.IsBackground = true;//?
            //threadReceiver.Start();

            timer = new System.Timers.Timer();
            timer.Interval = 1000;
            timer.Enabled = true;
            timer.AutoReset = true;
            timer.Elapsed += new ElapsedEventHandler(TimerElapsed);
            timer.Start();
            sw.Start();
        }
 
        private static void TimerElapsed(object sender, EventArgs e)
        {
            try
            {
                long outSpeed = (snd.nPacket * PACKET + OVERHEAD) / 131072;//*8/1024/1024 = mbit = 131072
                snd.nPacket = 0;
                Console.Clear();
                Console.SetCursorPosition(1, 0);
                Console.Write("out: " + outSpeed.ToString() + "Mbit/sec");
                long inSpeed = (rcv.nPacket * PACKET + OVERHEAD) / 131072;//*8/1024/1024 = mbit = 131072
                rcv.nPacket = 0;
                Console.SetCursorPosition(1, 1);
                Console.Write("in:  " + inSpeed.ToString() + "Mbit/sec");
                
            }
            catch (Exception ex)
            {
                //ups
            }
        }
    }
 
    class Sender
    {
        public bool isSending = true;
        private UdpClient udpClient;
        const int BYTES = 1024;//1472 +28 Overhead = MTU 1500
        private static byte[] data = new byte[BYTES];
        public long nPacket;
        public List<string> chksum = new List<string>();
 
        public Sender(UdpClient udpClient)
        {
            this.udpClient = udpClient;
        }
 
        public void Send()
        {
            while (isSending)
            {
 
                //losing a lot of time in create packet or adding to list!
                /*//CreatePacket();
                    
                Random rnd = new Random();
                    
                byte[] bytesToSend = data;
                    
                bytesToSend[0]= (byte)rnd.Next(0,255);
                chksum.Add(bytesToSend[0].ToString());
                    */
                //Random rnd = new Random();
                //rnd.NextBytes(data);
                udpClient.Send(data, data.Length, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("192.168.0.3"), 399));
                nPacket++;
               
            }
        }
 
        private byte[] CreatePacket()
        {
            byte[] data = new byte[BYTES];
            Random rnd = new Random();
            rnd.NextBytes(data);
            chksum.Add(CalculateChecksum(data));
            return data;
        }
 
        private string CalculateChecksum(byte[] dataToCalculate)
        {
            byte[] byteToCalculate = dataToCalculate;
            int checksum = 0;
            foreach (byte chData in byteToCalculate)
            {
                checksum += chData;
            }
            checksum &= 0xff;
            return checksum.ToString("X2");
        }
 
    }
 
    class Receiver
    {
 
        public bool isReceiving = true;
        private UdpClient udpClient;
        const int BYTES = 2048;
        private static byte[] buffer = new byte[BYTES];
        public long nPacket;
        public List<string> chksum = new List<string>();
 
        public Receiver(UdpClient udpClient)
        {
            this.udpClient = udpClient;
        }
 
        public void ReceiveData()
        {
            IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 399);
            while (isReceiving)
            {
                nPacket++;
                byte[] buffer = udpClient.Receive(ref RemoteIpEndPoint);
                //chksum.Add(buffer[0].ToString());
            }
        }
    }
}
 
 
Server sending to Client
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Timers;
namespace UDP_StressServer
{
    class Program
    {
        private static Receiver rcv;
        private static System.Timers.Timer timer = new System.Timers.Timer();
        private static UdpClient udpClient;
        private static IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Parse("192.168.0.2"), 399);
 
        static void Main(string[] args)
        {
            UdpClient udpClient = new UdpClient(399);
            IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Parse("192.168.0.2"), 399);
 
            Receiver rcv = new Receiver(udpClient);
            Thread threadReceiver = new Thread(rcv.ReceiveData);
            Thread threadSender = new Thread(rcv.SendData);
            threadReceiver.Start();
            threadSender.Start();           
        }   
    }
 
    
    class Receiver
    {
        public bool isReceiving = true;
        UdpClient udpClient;
        public List<byte[]> inBuffer= new List<byte[]>();
        private byte[] buffer = new byte[1024];
 
        public Receiver(UdpClient udpClient)
        {
            this.udpClient = udpClient;
        }
 
        public void ReceiveData()
        {
            IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Parse("192.168.0.2"), 399);
 
            while (isReceiving)
            {
                //inBuffer.Add(udpClient.Receive(ref remoteIPEndPoint));

                /* use sender */
                udpClient.Send(buffer, buffer.Length, remoteIPEndPoint);
                /*
                for (int i = 0; i < inBuffer.Count; i++)
                {
                   udpClient.Send(inBuffer[i], inBuffer[i].Length, remoteIPEndPoint);
                    
                    inBuffer.RemoveAt(i);
                }
                 /**/
            }
        }
 
        public void SendData()
        {
            IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Parse("192.168.0.2"), 399);
 
            while (true)
            {
 
               // udpClient.Send(inBuffer.Take(inBuffer.Count), inBuffer.Take(inBuffer.Count).Count(), remoteIPEndPoint);
                //udpClient.Send(buffer, buffer.Length, remoteIPEndPoint);
            }
        }
    }
 
}
 
Posted 12-Jun-13 23:55pm
Emence235

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 105
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 13 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100