11

I've a server with several IP Addresses assigned to the network adapter.

On that server is a client app to connect to another server app via TCPClient. For all outgoing communications my servers default IP address is being used, however for this one application I'd like the outgoing communication to be send out on another local IP address.

Is it possible when communicating out to specify another locally assigned IP?

I'm trying to make the remote server app think it's from another IP so it will pass through firewalls etc....

Thanks in advance

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
Dave Hogan
  • 3,201
  • 6
  • 29
  • 54

1 Answers1

19

You can use the constructor of TcpClient that accepts a local endpoint address:

TcpClient c=new TcpClient(new System.Net.IPEndPoint(...));

For example:

TcpClient c=new TcpClient(new IPEndPoint(IPAddress.Parse("192.168.1.1"), 0);

Reference: TcpClient Constructor (IPEndPoint)

Aviad P.
  • 32,036
  • 14
  • 103
  • 124
  • Thanks for that, I wrongly assumed the constructor was the endpoint for the remote host. Out of interest is there a similar thing for the System.Net.Sockets.Socket class? It has a LocalEndPoint Property but it's only a getter and not a setter. – Dave Hogan Jan 06 '10 at 22:46
  • It's OK - I found the Bind method as explained here: http://stackoverflow.com/questions/1508804/setting-a-sockets-local-endpoint Many Thanks for your help Aviad – Dave Hogan Jan 06 '10 at 22:52
  • What if the the other IP that I want to use is not a local IP? – gunakkoc May 22 '14 at 03:25
  • A communication channel is composed of two sockets, each socket has an address, this question deals with the socket at the local end. If you want to connect to somewhere remote, you're asking about the remote end. To specify the target of the connection you use the `Connect` method. – Aviad P. May 22 '14 at 06:07