Click here to Skip to main content
15,072,051 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm working on a C# winforms application that would simulate n-number of client socket connections to an asynchronous socket server. The form would have a NumericUpDown control where the user could select from 1 to 20 clients. And when executed create that many client socket connections to the server. I understand how to do this with one client socket but not sure how I would do this making client sockets on the fly. How do I dynamically create the client sockets at run time?

What I have tried:

I have got as far as making one client socket connection to the socket server.
Posted
Updated 18-Aug-21 3:56am
Comments
PIEBALDconsult 18-Aug-21 9:52am
   
You can do it any way you want, there is no one way to do it.
Gerry Schmitz 18-Aug-21 14:51pm
   
x = new TcpClient(...). Add to a collection. Repeat

1 solution

Just create a loop, and inside the loop create a separate thread or task for each client.
In the thread code, create the socket, connect it, and do whatever you have to with it.

This is probably a good starting place as it allows progress reporting back to teh original UI thread for display updates: BackgroundWorker Class (System.ComponentModel) | Microsoft Docs[^]
   
Comments
Member 8677306 18-Aug-21 11:35am
   
Thank you thats helpful.

For the single socket I'm declaring is at the top of my code as such and using it in a few different methods.

private static readonly Socket ClientSocket = new Socket
(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

If I move that into a loop its no longer accessible to the other methods. Disconnect button etc.
OriginalGriff 18-Aug-21 11:42am
   
Did you read what I said, or just gloss over the details?
Member 8677306 18-Aug-21 12:23pm
   
I didn't gloss over it, maybe I just don't fully understand it. I'm working in implementing what you said and I encountered an issue. Specifically this:

"In the thread code, create the socket..."

There's all different levels of programmers on here.
Member 8677306 18-Aug-21 15:23pm
   
How do I differentiate the loop-created sockets one from the other?

private static readonly Socket ClientSocket = new Socket
(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

public void CreateSockets()
{
Parallel.For(1, Convert.ToInt32(numericUpDown1.Value) + 1, i =>
{
new Thread(() => Connect(i)).Start();
});
}

In my Connect() method I create a new ClientSocket and the first socket connects then I get an error:

"A connect request was made on an already connected socket 192.168.0.100:4500"

What in the loop can be done to differentiate the sockets?
pdoxtader 22-Aug-21 23:00pm
   
You aren't giving us enough information to answer that, because we can't see your code. From the error you are showing us, it looks like you aren't actually creating a new socket in your Connect() method, and are actually attempting to connect using the same socket. Troubleshooting this is pretty basic, and if you're having trouble with it then maybe you need to get more comfortable using the debugger in visual studio before attempting to tackle a more complicated subject like TCP/IP communications.

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