Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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
C#
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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900