title: How can the same socket object serve different clients?
body: <p>As is mentioned in the documentation: <a href="https://docs.python.org/3/library/socket.html#socket.socket.accept" rel="nofollow noreferrer">Python socket.accept()</a></p>
  
  <blockquote>
    <p>Accept a connection. The socket must be bound to an address and
    listening for connections. The return value is a pair (conn, address)
    where conn is a new socket object usable to send and receive data on
    the connection, and address is the address bound to the socket on the
    other end of the connection.</p>
    
    <p>The newly created socket is non-inheritable.</p>
    
    <p>Changed in version 3.4: The socket is now non-inheritable.</p>
  </blockquote>
  
  <p>The server code is</p>
  
  <pre><code>&gt;&gt;&gt; from socket import *
  &gt;&gt;&gt; sock = socket(AF_INET, SOCK_STREAM)
  &gt;&gt;&gt; sock.bind(("localhost", 20000))
  &gt;&gt;&gt; sock.getsockname()
  ('127.0.0.1', 20000)
  &gt;&gt;&gt; sock.listen(1)
  &gt;&gt;&gt; while True: 
  ...     conn, address = sock.accept()
  ...     print("Address of client : {0}".format(address))
  ...     print("Address of socket : {0}".format(conn.getsockname()))
  ... 
  Address of client : ('127.0.0.1', 47165)
  Address of socket : ('127.0.0.1', 20000)
  Address of client : ('127.0.0.1', 47166)
  Address of socket : ('127.0.0.1', 20000)
  </code></pre>
  
  <p>The client code is</p>
  
  <pre><code>&gt;&gt;&gt; from socket import *
  &gt;&gt;&gt; sclient1 = socket(AF_INET, SOCK_STREAM)
  &gt;&gt;&gt; sclient2 = socket(AF_INET, SOCK_STREAM)
  &gt;&gt;&gt; sclient1.connect(("localhost", 20000))
  &gt;&gt;&gt; sclient2.connect(("localhost", 20000))
  </code></pre>
  
  <p>The address of the new <code>socket</code> object that is returned, is always the same as the original <code>socket</code> which was actually accepting connections.</p>
  
  <p>I always thought that the server would create a new <code>socket</code> object with a different random port, but as can be seen above, even for multiple clients, the address and port of new <code>conn</code> object is still the same. How is then the server able to handle multiple clients?</p>
  
  <hr>
  
  <h3>Edit</h3>
  
  <p>I know the above code is blocking. If I use multiple threads to handle different client connections, I'll have to do send the new socket object, and client address to my thread function. Therefore, multiple threads are then handling multiple clients using the same server address and port.</p>
  
  <p><em>Threaded server</em></p>
  
  <pre><code>&gt;&gt;&gt; from socket import *    
  &gt;&gt;&gt; import threading
  &gt;&gt;&gt; def handler(conn, address):
  ...     print("Address of client : {0}".format(address))
  ...     print("Address of socket : {0}".format(conn.getsockname()))
  ... 
  &gt;&gt;&gt; sock = socket(AF_INET, SOCK_STREAM)
  &gt;&gt;&gt; sock.bind(("localhost", 20000))
  &gt;&gt;&gt; sock.listen(1)
  &gt;&gt;&gt; while 1:
  ...     conn, address = sock.accept()
  ...     t = threading.Thread(target=handler, args=[conn, address])
  ...     t.start()
  ... 
  Address of client : ('127.0.0.1', 47169)
  Address of socket : ('127.0.0.1', 20000)
  Address of client : ('127.0.0.1', 47170)
  Address of socket : ('127.0.0.1', 20000)
  </code></pre>