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:

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
deleydSoftware EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nociSoftware EngineerCommented:
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 EngineerAuthor Commented:
How about if i put the commands in the correct order. Then what would happen?
nociSoftware EngineerCommented:
no the network layer will not forward packets from one socket to another.....
what would happen??? most probably some error codes being produced or at worst: nothing but some open sockets doing nothing.

a socket is a connecton between your program and a "network session"  1:1....
another socket is another network session....

What you can do:

socka.bind(, ...)     # assuming one of YOUR interfaces is
socka.listen()    - now you are ready to accept connection...
sockc = socka.accept()   to actual receive a connection....

in another program / trhead:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nociSoftware EngineerCommented:
If you want to see what is passing through: use wireshark + appropriate filters. (for commandline use: tshark / tcpdump).
David FavorLinux/LXD/WordPress/Hosting SavantCommented:
Maybe describe exactly what you're trying to accomplish, listing all processes you'll be using + exact data flow required.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Networking Protocols

From novice to tech pro — start learning today.