We help IT Professionals succeed at work.

socket connect() & bind() together?

deleyd
deleyd asked
on
177 Views
Last Modified: 2019-04-02
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?
Comment
Watch Question

nociSoftware Engineer
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
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.
deleydSoftware Engineer

Author

Commented:
How about if i put the commands in the correct order. Then what would happen?
Software Engineer
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
nociSoftware Engineer
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
If you want to see what is passing through: use wireshark + appropriate filters. (for commandline use: tshark / tcpdump).
David FavorFractional CTO
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Maybe describe exactly what you're trying to accomplish, listing all processes you'll be using + exact data flow required.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.