How to get python tcp server/client to allow multiple clients on ant the same time
<p>I have started to make my own TCP server and client. I was able to get the server and the client to connect over my LAN network. But when I try to have another client connect to make a three way connection, it does not work. What will happen is only when the first connected client has terminated the connection between, the server and the client, can the other client connect and start the chat session. I do not understand why this happens. I have tried threading, loops, and everything else I can think of. I would appreciate any advice. I feel like there is just one small thing i am missing and I can not figure out what it is.</p>

<h2>Here is my server:</h2>

<pre><code>import socket
from threading import Thread 

def whatBeip():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(('8.8.8.8', 0))  
    local_ip_address = s.getsockname()[0]
    print('Current Local ip: ' + str(local_ip_address))

def clietConnect():
    conn, addr = s.accept()
    print 'Connection address:', addr
    i = True
    while i == True:
        data = conn.recv(BUFFER_SIZE)
        if not data:
            break 
        print('IM Recieved: ' + data)
        conn.sendall(data)  # echo


whatBeip()    

TCP_IP = ''
TCP_PORT = 5005
BUFFER_SIZE = 1024
peopleIn = 4

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(peopleIn)

for client in range(peopleIn):
    Thread(target=clietConnect()).start() 


conn.close()
</code></pre>

<h2>Here is my client</h2>

<pre><code>import socket


TCP_IP = '10.255.255.3'
TCP_PORT = 5005
BUFFER_SIZE = 1024
MESSAGE = "Hello, World!"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))

i = True
while i == True:
    s.sendall(raw_input('Type IM: '))
    data = s.recv(BUFFER_SIZE)

s.close()
</code></pre>

<p>This is your main problem: <code>Thread(target=clietConnect()).start()</code> executes the function <code>clientConnect</code> and uses it's return value as the Thread function (which is None, so the Thread does nothing)</p>

<p>Also have a look at:</p>

<p>1) You should wait for all connections to close instead of <code>conn.close()</code> in the end of the server:</p>

<pre><code>threads = list()
for client in range(peopleIn):
    t = Thread(target=clietConnect)
    t.start() 
    threads.append(t)
for t in threads: t.join()
</code></pre>

<p>and to close the connection when no data is received:</p>

<pre><code>    if not data:
        conn.close()
        return
</code></pre>

<p>2) You probably want to use SO_REUSEADDR [ <a href="http://stackoverflow.com/questions/14388706/">Socket options SO_REUSEADDR and SO_REUSEPORT, how do they differ? Do they mean the same across all major operating systems?</a> , <a href="http://stackoverflow.com/questions/6380057/">Python: Binding Socket: &quot;Address already in use&quot;</a> ]</p>

<p>3) And have a look at asyncio for python</p>

python tcp