Avatar of deleyd
deleyd
Flag for United States of America asked on

socket connect() & bind() together?

My understanding is for sockets, connect sets the destination IP address, and bind sets the return IP address.

What would happen if I created two sockets, socka and sockb, and did:
socka.connect(10.1.1.2);
socka.bind(99.0.0.1);
sockb.connect(99.0.0.1);

Open in new window

If I now send some data to sockb, will the data go to socka and then will socka forward that data to IP address 10.1.1.2?
Networking ProtocolsTCP/IP

Avatar of undefined
Last Comment
David Favor

8/22/2022 - Mon
noci

A bind needs to PRECEED connect.

socka = socket(....)
socka.bind(srcaddress...)     (set source to use, needs to be one of the local interface assigned IP addresses)
socka.connect(dstaddress.....)   uses socka bound source address to connect to destination.....

So NO that will not work as you intended.

if you need to forward data YOU need to program that yourself....
read from sockb and then write to sock a & vice versa.

You can check the sources of netcat / socat for network copy functiones.
deleyd

ASKER
How about if i put the commands in the correct order. Then what would happen?
ASKER CERTIFIED SOLUTION
noci

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
noci

If you want to see what is passing through: use wireshark + appropriate filters. (for commandline use: tshark / tcpdump).
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
David Favor

Maybe describe exactly what you're trying to accomplish, listing all processes you'll be using + exact data flow required.