what does socket.connect() do internally?
<p>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. </p>

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

<p>For the TCP counterparts the resources suggest that the TCP echo client uses </p>

<blockquote>
  <p>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. </p>
</blockquote>

<p><strong>Questions</strong></p>

<ol>
<li>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?</li>
<li><p>Similarly, for the UDP counterparts, the <a href="https://pymotw.com/2/socket/udp.html" rel="nofollow">resources</a> 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.</p></li>
<li><p>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.</p></li>
</ol>

<blockquote>
  <p>Please help me understand what does it actually mean to say connection oriented implementation wise?</p>
</blockquote>

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

<blockquote>
  <p>And how does the connect(), listen() and accept() APIs help achieve this?</p>
</blockquote>

<p>I have never used this Python socket library but I think my fairly basic knowledge regarding sockets will suffice to explain this. The <code>connect</code> 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.</p>

<blockquote>
  <p>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 ?</p>
</blockquote>

<p>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:</p>

<p><em>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.</em></p>

<p>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 <code>recvfrom</code> method. The client uses this method as well to receive messages from the server.</p>

<blockquote>
  <p>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 ?</p>
</blockquote>

<p>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.</p>

python sockets tcp udp