Server has connection only with one client, Python Socket
<p>First of all, I hope I'm not writing too much. I'm new and I want that no one has a doubt, that everything will be clear for the readers. I hope someone can help me.</p>

<p>I have been working in a socket server and client for some weeks. As time passes I add more features. At the beginning, it was just about having an <em>echo server</em>. Afterwards, I did a server that returns time, random number or other specific things the client asked. And for last thing, I added to the scripts so the server can accept 2 clients so they can talk between them. However, the client couldn't write the messages he wanted because he needed always to wait until the second client answers. When I got stuck with this problem, I learned about <strong>Threads</strong>. </p>

<p>So the next feature I wanted to add and it's where I'm stuck for about two or three weeks is the part where two clients can send to each other messages without stop and without the need of waiting like before they would need.</p>

<p>I have a script of the server:</p>

<p><div class="snippet" data-lang="js" data-hide="true">
<div class="snippet-code snippet-currently-hidden">
<pre class="snippet-code-html lang-html prettyprint-override"><code>import socket
import threading
from datetime import datetime
from random import randint

global num
num = 0

class serverThread(threading.Thread):
    def __init__(self):
        global num
        num = num + 1
        self.id = num
        threading.Thread.__init__(self)
        client, address = server.accept()
        self.client = client
        self.address = address
        print "serverThread init finished-" + str(self.id)

    def run(self):
        print "r1 num-" + str(self.id)
        size = 1024
        while True:
            #try:
                print "r2*************-" + str(self.id)
                data = self.client.recv(size)
                print "r3..... " + data
                print "r4-" + str(self.id)
                if data:
                    print "r5-" + str(self.id)
                    response = data

                    self.client.send(response)
                    print "r6-" + str(self.id)
                else:
                    print "r7-" + str(self.id)
                    raise Exception('Client disconnected-' + str(self.id) )
            #except:
               # print "Except"
               # self.client.close()
               # return

def create(ipHost, port):
    server = socket.socket()
    server.bind((ipHost, port))
    print "The server was created successfully."
    return server

def listen(server):
    server.listen(5)
    c1 = serverThread()
    c1.start()
    c2 = serverThread()
    c2.start()
    print "finished both threads created"

    while c1.isAlive() and c2.isAlive():
        continue

server = create("0.0.0.0", 1729)
listen(server)</code></pre>
</div>
</div>
</p>

<p>As you can see I'm not using <code>try</code> and <code>except</code> because I don't know good how to use them.</p>

<p>My second script, the client:
<div class="snippet" data-lang="js" data-hide="true">
<div class="snippet-code snippet-currently-hidden">
<pre class="snippet-code-html lang-html prettyprint-override"><code>import socket
import threading

class sendThread(threading.Thread):
    def __init__(self, ip, port):
        threading.Thread.__init__(self)
        self.client = socket.socket()
        self.port = port
        self.ip = ip
        self.client.connect((self.ip, self.port))
        print "[+] New send thread started for "+ip+":"+str(port) + "...Everything went successful!"

    def run(self):
        while True:
            data = raw_input("Enter command:")
            self.client.send("Client sent: " + data)

class receiveThread(threading.Thread):
    def __init__(self, ip, port):
        threading.Thread.__init__(self)
        self.client = socket.socket()
        self.ip = ip
        self.port = port
        self.client.connect((str(self.ip), self.port))
        print "[+] New receive thread started for "+ip+":"+str(port) + "...Everything went successful!"

    def run(self):
        print "Entered run method"
        size = 1024
        while True:
            data = self.client.recv(size)
            if data != "" or data:
                print "A client sent " + data
def client():
    port = 1729
    ip = '127.0.0.1'
    print "Connection from : "+ip+":"+str(port)

    receive = receiveThread(ip, port)
    print "b1"
    receive.start()
    print "b2"
    send = sendThread(ip, port)
    print "b3"
    send.start()
    while send.isAlive() and receive.isAlive():
        continue
    print "-----END of While TRUE------"
    print "Client disconnected..."

client()</code></pre>
</div>
</div>
</p>

<p>I thought it would be a good idea to describe my scripts, go step by step in my code, maybe it helps so it will be more readable.</p>

<h2>The Server script</h2>

<p>I create a socket server, and call the <code>bind</code> method. I call for the <code>listen</code> method and begin to receive the clients. I create a <strong>thread</strong> for each client that I will accept (<code>accept()</code>) and receive (<code>recv</code>) data from. After I create each client thread I print a message that they were created successfully. When I start the clients threads they wait for receiving a message sent (<code>recv</code>) and send it. If I'm not wrong, I just need the <code>send</code> method and not to tell to <em>who</em> send it.</p>

<h2>The Client script</h2>

<p>The client will have two threads. One for sending messages (as much as you want) and one for always waiting for messages that another client sent.</p>

<h2>The problem</h2>

<p>When I want to run the server before running the two clients it prints</p>

<pre><code>The server was created successfully.
</code></pre>

<p>I run 2 clients and both print:</p>

<pre><code>Connection from : 127.0.0.1:1720
[+] New receive thread started for 127.0.0.1:1720...Everything went successful!
b1
Entered run method
b2
[+] New send thread started for 127.0.0.1:1720...Everything went successful!
b3
Enter command:
</code></pre>

<p>However, there is a <strong>problem</strong> in the connection between the <strong>second</strong> client created and the server. I did that when the server receives a message sent by a client, it will print the message as output in the server. However, when the first client sends a message it prints it. But not when the second client sends.</p>

<p>I even tried to copy the client script and put it in a new file, so the two clients are from two different files and maybe find a problem. However, it didn't help. I tried to run the first file and then the second file, and vice versa. Always the second client had a problem with the server. (By the way, I would also want to know why the client file doesn't print the message he himself sends (he will still receive it from the server) but that's a secondary problem).</p>

<p>I hope I didn't do it too long or too far, hope someone can help find the problem in my code.</p>

<p>I would be even happier if you tell what is the problem in the code, instead of giving me one that you created or find!</p>