basic understanding of simple socket server
<p>I am new to socket programming and am trying to understand how sockets work. Presently I am trying to play with python socket library to see how it works. </p>

<p>Now there are a few things that I am not able to wrap my head around. 
Let's take an example as shown <a href="http://www.bogotobogo.com/python/python_network_programming_server_client.php" rel="nofollow">here</a> of a simple echo client and server written in python using the Python socket library. </p>

<p>The <strong>echo server</strong> looks like the below
    # echo_server.py
    import socket</p>

<pre><code>host = ''        # Symbolic name meaning all available interfaces
port = 12345     # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(1)
conn, addr = s.accept()
print('Connected by', addr)
while True:
    data = conn.recv(1024)
    if not data: break
    conn.sendall(data)
conn.close()
</code></pre>

<p>The <strong>echo client</strong> looks like the below</p>

<pre><code># echo_client.py
import socket

host = socket.gethostname()    
port = 12345                   # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
s.sendall(b'Hello, world')
data = s.recv(1024)
s.close()
print('Received', repr(data))
</code></pre>

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

<ol>
<li>In echo_server.py when I say conn, addr = s.accept(), accept() does two things. 1. it returns the connecting client's address (ip and port), which is stored in addr and 2. It creates a new socket (called client socket) that can be referenced here using conn and is used to interact with the connected client. 

<blockquote>
  <p>Now what happens when another client also connects to this server? Does the conn and addr variables get overwritten with the new values? Or how is it handled?</p>
</blockquote></li>
<li>Again in echo_server.py, the s.listen(1) suggests there is a maximum of 1 backlog allowed, which I seem to understand only theoretically that it is the maximum number of clients that the server wants to allow to keep waiting in the queue while the server is busy dealing with the already connected client. 

<blockquote>
  <p>Is there some simple practical example to demo this ?</p>
</blockquote></li>
</ol>

<blockquote>
  <blockquote>
    <p>Now what happens when another client also connects to this server? Does the conn and addr variables get overwritten with the new values? Or how is it handled?</p>
  </blockquote>
</blockquote>

<p>As pointed out by Rob in another answer, your code will accept only one client. The <code>accept</code> have to be called for any connect request from client. And code accordingly. Now coming to your question, yes, the (conn,addr) pair will be overwritten. The code should take care of passing them on to some connection thread before calling <code>accept</code> again for new connection.</p>

<blockquote>
  <blockquote>
    <p>Is there some simple practical example to demo this ?s there some simple practical example to demo this ?</p>
  </blockquote>
</blockquote>

<p>In the updated code for accepting multiple clients try not calling <code>accept</code> after few connections and notice the TCP connection status table (with <code>netstat</code> for example) to see the behavior for varying values of backlog.</p>

python sockets networking tcp-ip