0

I am trying to create a multi-threading application that'll try and send a lot of data to a specific IP and port using sockets in C#. I have completed this, but I need help in getting more information from it.

I would like to know how I could get how much data is being sent a second? In MB if possible, from all threads and all requests?

I am mearly doing this for educational purposes to see how much data can actually be sent and how it all works when doing it with multi -threading

Here is my code:

static void Main(string[] args)
{
    int amountOfThreads = 10;

    while (amountOfThreads > 0)
    {
        Thread thread = new Thread(SendData);
        thread.Start();
    }
}

private static void SendData()
{
    byte[] dataToSend = Encoding.Default.GetBytes("some string that'll be sent many times.");

    string ipAddress = "127.0.0.1";
    int port = 3924;

    IPEndPoint ep = new IPEndPoint(IPAddress.Parse(ipAddress), port);

    Socket socketToSendTo = new Socket(System.Net.Sockets.AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    socketToSendTo.SendTo(dataToSend, ep);
}
Ash Smith
  • 172
  • 1
  • 2
  • 8
  • 1
    `while (amountOfThreads > 0)` will result in potentially endless threads being created until you hit a hard limit. **Pro tip:** using `SendAsync` rather than spinning up an explicit thread scales better –  May 04 '17 at 04:52
  • Sorry, I forgot to add `amountOfThreads--;` to my code. – Ash Smith May 04 '17 at 04:54
  • What have you tried so far for the measuring part? – Ofir Winegarten May 04 '17 at 06:12

0 Answers0