使用多线程的python客户端服务器

时间:2017-11-29 21:51:38

标签: python multithreading sockets client-server

我使用多线程在python中创建客户端 - 服务器程序,但是当2个客户端将数据发送到服务器时。它只能回复第二个客户端,有人可以帮助解决它,我的项目要求在多线程中执行此操作

代码:

import socket
import sys
from _thread import *

host =''

port=888
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

print("socket created ")

try:
    s.bind((host,port))
except socket.error:
    print("BINDING FAILED")
    sys.exit()
print("socket hai bounded ")


s.listen(10)#/upto 10 people can be connet

print("socket is ready")

def clientthread(conn):
    welcomemessage="WELCOME TO SERVER TYPE SOMETHING AND HIT ENTER "
    conn.send(welcomemessage.encode())
    while True:
        data=conn.recv(1024)
        reply="OK." +data.decode()
        if not data:
            break;
        print(reply)

        reply1=input("your message :")
        conn.sendall(reply1.encode())
    conn.close()


while 1:
    conn, addr = s.accept()
    print("conneted with " + addr[0] + ":" + str(addr[1]))
    start_new_thread(clientthread,(conn,))
s.close()

0 个答案:

没有答案