Joshua Jones
Joshua Jones

Reputation: 35

How do I set the outbound/sending IP address in a C# console app?

My dedicated server has 5 usable, static IP addresses. This server's primary objective is to create and maintain several hundred connections to a single destination. Understandably, after a few hundred connections have been established, I am confronted with socket read/write exceptions and mass disconnects. This likely occurs because the destination either recognizes the single origin IP address and temporarily blocks it, or because the single link between A and B becomes unstable.

In effect, I'd like to witness the results of dividing the connections between multiple source IP addresses.

My research so far has led me to questions and answers like this one, which suggest using ServicePoint.BindIPEndPointDelegate.

However, I would like to bind the entire console app to a specific IP address. How do I accomplish this?

Note: I am using FluorineFX at the moment. If a solution exists to tell FluorineFX which IP address to make connections from, that would work as well.

Upvotes: 1

Views: 520

Answers (1)

Iridium
Iridium

Reputation: 23731

It's quite easy to specify the source IP address when using the TCP functionality in .NET. If you're using TcpClient, simply use the TcpClient(IPEndPoint localEndPoint) constructor, which will bind it to the specified local IP address. For connections made using the Socket class, you need to use the Bind(EndPoint localEP) method on the socket to associate it with a local address.

Upvotes: 1

Related Questions