两个客户端无法在套接字客户端 - 服务器连接中相互通信

时间:2016-04-10 15:15:53

标签: python multithreading sockets client-server output

当一个客户端发送输入时,在第二个客户端中它不打印该消息(即未收到消息)。 当第二个客户端发送输入时,在第一个客户端中它不打印该消息(即未收到消息)。

我在服务器脚本中了解到client.send(message),它会将消息发送给连接到服务器的所有客户端,因此您无需指定发送者是谁。

客户端

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

服务器

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)

两个客户端都有以下输出:

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

当两个客户端发送相同的消息时就是这种情况(因此两个客户端的输出都会有Enter command:Hello

如果客户端1发送" Hello"并且客户端2发送"嗨": 客户端1将具有输出Hello,客户端2将具有输出Hi,而不是接收和在输出HiHello中打印。

注意:如果你告诉我我的错误,而不是向我展示新版本,将会更加感激。

1 个答案:

答案 0 :(得分:3)

您的错误是,您只将文本从客户端发送回同一客户端:

self.client.send(response)

即。客户c1和客户c2是独立的,彼此之间并不了解。