Click here to Skip to main content
15,897,090 members

Comments by Member 8677306 (Top 4 by date)

Member 8677306 18-Aug-21 15:23pm View    
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?
Member 8677306 18-Aug-21 12:23pm View    
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 11:35am View    
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.
Member 8677306 18-Aug-21 9:39am View    
Deleted
Also if I want to create a client socket IP for each new one I think I can bind each one before making the connection(s).