[ Two clients can't talk to each other in a socket client-server connection ]

When one client sends an input, in the second client it's not printed the message (i.e. message not received). When second client sends an input, in the first client it's not printed the message (i.e. message not received).

I learned that client.send(message) in the server script, it sends the message to all the clients connected to the server so you don't need to specify to who to send it.

Client

import socket
import threading

class sendThread(threading.Thread):
    def __init__(self, ip, port, client):
        threading.Thread.__init__(self)
        self.port = port
        self.ip = ip
        self.client = client
        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(data)

class receiveThread(threading.Thread):
    def __init__(self, ip, port, client):
        threading.Thread.__init__(self)
        self.ip = ip
        self.port = port
        self.client = client
        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 data


def client():
    port = 1724
    ip = '127.0.0.1'
    print "Connection from : "+ip+":"+str(port)
    client = socket.socket()
    client.connect((ip, port))
    receive = receiveThread(ip, port, client)
    print "b1"
    receive.start()
    print "b2"
    send = sendThread(ip, port, client)
    print "b3"
    send.start()
    while send.isAlive() and receive.isAlive():
        continue
    print "-----END of While TRUE------"
    print "Client disconnected..."

client()

Server

import socket
import threading

global num
num = 0

class serverThread(threading.Thread):
    def __init__(self, client, address):
        global num
        num = num + 1
        self.id = num
        threading.Thread.__init__(self)
        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:
                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) )

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)
    client, address = server.accept()
    c1 = serverThread(client, address)
    c1.start()
    client, address = server.accept()
    c2 = serverThread(client, address)
    c2.start()
    print "finished both threads created"

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

server = create("0.0.0.0", 1724)
listen(server)

Both clients have the following output:

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:Hello
Enter command:Hello

This is the case when both clients send the same message (Thus both will have Enter command:Hello in their output.

If client 1 sends "Hello" and client 2 sends "Hi": Client 1 will have as output Hello and client 2 will have as output Hi, instead of both receiving and printing in the output Hi and Hello.

Note: Will be much more grateful if you show me my mistake, instead of showing me a new version.

Answer 1


Your mistake is, that you only send the text from the client to the same client back:

self.client.send(response)

I.e. Client c1 and client c2 are independent and don't know of each other.