Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set local IP when originating TCP/IP connections?

Tags:

c

linux

glibc

Using Linux and glibc, when originating a TCP/IP connection (calling connect() on a socket), how can I choose explicitly from which of my IP local adresses I want to originate that connection, providing I have more than one interface (with different IPs) from where I can reach the remote host?

Is it possible to do so, or it is always up to the operating system IP stack to chose the route automatically?

like image 672
lvella Avatar asked Dec 11 '25 03:12

lvella


1 Answers

Bind to a specific address instead of 0.0.0.0 or ::.

like image 85
Ignacio Vazquez-Abrams Avatar answered Dec 12 '25 20:12

Ignacio Vazquez-Abrams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!