Link to home
Start Free TrialLog in
Avatar of deleyd
deleydFlag 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?
Avatar of noci
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.
Avatar of deleyd

ASKER

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

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you want to see what is passing through: use wireshark + appropriate filters. (for commandline use: tshark / tcpdump).
Maybe describe exactly what you're trying to accomplish, listing all processes you'll be using + exact data flow required.