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>
  <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>

<p>In your example, nothing interesting will happen. The attempt to connect will hang. Each client call to <code>connect()</code> must match a server call to <code>accept()</code>. Since you call <code>accept()</code> exactly once, at most one client will ever connect.</p>

<p>If you want to be able to accept connections from subsequent clients, serially, try this:</p>

<pre><code>import socket

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)
while True:
    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>If you want to be able to accept connections from clients in parallel, ie. from multiple clients simultaneously, then you have more work to do. But that isn't really part of your question today.</p>

python sockets networking tcp-ip