Specify the outgoing IP address to use with TCPClient / Socket in C#

10,858

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)

Share:
10,858
Dave Hogan
Author by

Dave Hogan

I'm an experienced software developer based on the Isle of Wight, South of the UK. My primary skills are mostly centred around back-end software and solutions utilising the Microsoft .NET stack. However, I am always open to using the right tools and technologies to reach the most suitable solution for a given problem. I also have experience in managing projects and small teams. In my current role, I am a Lead .NET Solutions Developer working for Liz Earle Beauty Co Ltd. As the lead, my responsibilities include, managing a highly technical team, prioritising tasks, working closely with cross-functional teams and project managers and delivering pragmatic solutions to meet business requirements, often under tight timescales. I have worked across many technologies since 2002, from monolithic legacy systems to cutting edge. I thoroughly enjoy working with technology and discovering newer and better ways of developing robust and adaptable software. Outside of my professional life, I enjoy spending time with my family, kayaking, walking, power-lifting and learning to play the guitar. Specialities: C#, Windows & Web Development, JavaScript / jQuery, CSS Bootstrap, ASP.NET MVC, APIs, Integration, WebAPI / Services, MS SQL Server and the Azure platform. I also have experience with build and deployment automation with Azure DevOps, TeamCity and Octopus Deploy and have a foundation level experience with F#

Updated on June 05, 2022

Comments

  • Dave Hogan
    Dave Hogan almost 2 years

    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

  • Dave Hogan
    Dave Hogan over 14 years
    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
    Dave Hogan over 14 years
    It's OK - I found the Bind method as explained here: stackoverflow.com/questions/1508804/… Many Thanks for your help Aviad
  • gunakkoc
    gunakkoc almost 10 years
    What if the the other IP that I want to use is not a local IP?
  • Aviad P.
    Aviad P. almost 10 years
    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.