c# Create tcp client simulator

#1
Hey there,

i want to make a some performance tests for my tcp server. So i need to write a tool, thats can handle multiple client connects to the server. But everytime it crashed. I dont know how to handle / to write it :/ Hope somebody can help me.

This my code:

Code:
private void button1_Click(object sender, EventArgs e)
{
   for (int i = 0; i < Convert.ToInt32(textBoxCount.Text); i++)
   {
      var deviceId = "SIMULATOR" + i.ToString("0000");
      new Thread(()=>ConnectClient(deviceId)).Start();
   }
}

private void ConnectClient(string deviceId)
{
   var client = new Client();
   client.Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
   client.Socket.Connect(new IPEndPoint(IPAddress.Parse(textBoxIp.Text),Convert.ToInt32(textBoxPort.Text)));
   Clients.Add(deviceId, client);
}

public static void ReceiveClientData(object socket)
{
   var clientSocket = (Socket)socket;
   try
   {
      while (clientSocket.Connected)
      {
      }
   }
   catch (Exception e)
   {
      AdvancedLogging.WriteLog(e.Message);
   }
}
}

public class Client
{
    public Socket Socket { get; set; }

    public Client()
    {
        new Thread(() => Form1.ReceiveClientData(Socket)).Start();
    }

    public Client(Socket clientSocket)
    {
        Socket = clientSocket;
        new Thread(() => Form1.ReceiveClientData(Socket)).Start();
    }
}
 
Top