简体   繁体   中英

what does socket.connect() do internally?

So I am trying to understand networking in general and especially sockets at this point in time. I am using the Python socket library to play with things.

I came across many examples on the internet that demonstrate TCP and UDP sockets via simple ECHO servers as examples.

For the TCP counterparts the resources suggest that the TCP echo client uses

socket_object.connect() to connect to the server and exchange data over it. Similarly, the TCP echo server uses listen() and accept() to handle connections and interactions with the client.

Questions

  1. Now these resources mention that it is a TCP connection and hence connection oriented (which I understand conceptually). Please help me understand what does it actually mean to say connection oriented implementation wise? And how does the connect(), listen() and accept() APIs help achieve this?
  2. Similarly, for the UDP counterparts, the resources suggest that the UDP echo client does not use any connect() and nor does the UDP echo server use any listen() or accept(). This helps it achieve connectionless behavior. Please help me understand how exactly ? Or may be the answer the question 1 above would help me understand this as well.

  3. Also, in case of UDP echo client in our case, why would it not need to bind to a socket. Even to be able to send data to a remote server, will it still not need a socket endpoint to actually send the data and also receive data when the server sends it back ? Or essentially, I guess, an understanding of what bind() really does internally will help me understand this.

Please help me understand what does it actually mean to say connection oriented implementation wise?

It means that the TCP protocol relies on an open connection to work. In other words, there must be an open connection through which the packages of a message sent via socket go.

And how does the connect(), listen() and accept() APIs help achieve this?

I have never used this Python socket library but I think my fairly basic knowledge regarding sockets will suffice to explain this. The connect method is used to open this very same connection I mentioned in the previous answer. The other two methods are used by the server to receive messages sent from the client through this open connection.

Similarly, for the UDP counterparts, the resources suggest that the UDP echo client does not use any connect() and nor does the UDP echo server use any listen() or accept(). This helps it achieve connectionless behavior. Please help me understand how exactly ?

You probably know that the UDP protocol, different from the TCP protocol, does not rely on an open connection to work. This means that message packages are sent "atomically" (independent from each other), which would make the previous methods useless. As stated in the docs you linked in your question:

Since there is no connection, per se, the server does not need to listen for and accept connections. It only needs to use bind() to associate its socket with a port, and then wait for individual messages.

This means that all that a server using UDP for sockets communication must do is register the port from which it will expect socket messages, which will be sent from the client and received through the recvfrom method. The client uses this method as well to receive messages from the server.

Even to be able to send data to a remote server, will it still not need a socket endpoint to actually send the data and also receive data when the server sends it back ?

The API probably does not expose a method to do that client-side because clients pick the port dynamically and prevent other programs from being a server in that port, or at least they should.

Maybe I'm wrong, but as I can understand connect() just checking IP and Port are correct and app from another side is ready. listen() and bind() sends info to OS about port - "if some package will come to port xxx,so please send it to me". accept() sends info to first app that everything is ok and we can talk with each other. This methods (instead accept) just inform OS to accept or decline packages from another app. Accept inform another app that everything is ok and we can start speak or not

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM