Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
套接字多线程聊天(Python)_Python_Sockets_Server_Client_Chat - Fatal编程技术网

套接字多线程聊天(Python)

套接字多线程聊天(Python),python,sockets,server,client,chat,Python,Sockets,Server,Client,Chat,我正在尝试使用Socket做一个TCP/UDP多线程聊天服务器。我尝试在服务器和客户机之间进行套接字多线程聊天,但我无法连接多个客户机,我只是在服务器和一个客户机之间进行了聊天 服务器代码: import time, socket, sys new_socket = socket.socket() host_name = socket.gethostname() s_ip = socket.gethostbyname(host_name) port = 8080 new_socket.bi

我正在尝试使用Socket做一个TCP/UDP多线程聊天服务器。我尝试在服务器和客户机之间进行套接字多线程聊天,但我无法连接多个客户机,我只是在服务器和一个客户机之间进行了聊天

服务器代码:

import time, socket, sys

new_socket = socket.socket()
host_name = socket.gethostname()
s_ip = socket.gethostbyname(host_name)

port = 8080

new_socket.bind((host_name, port))
print("Binding successful!")
print("This is your IP: ", s_ip)

name = input('Enter name: ')

new_socket.listen(1)

conn, add = new_socket.accept()
conn, add = new_socket.accept()

print("Received connection from ", add[0])
print('Connection Established. Connected From: ', add[0])


client = (conn.recv(1024)).decode()
print(client + ' has connected.')

conn.send(name.encode())
while True:
    message = input('Me : ')
    conn.send(message.encode())
    message = conn.recv(1024)
    message = message.decode()
    print(client, ':', message)
客户端代码:

import time, socket, sys

socket_server = socket.socket()
server_host = socket.gethostname()
ip = socket.gethostbyname(server_host)
sport = 8080

print('This is your IP address: ', ip)
server_host = input('Enter friend\'s IP address:')
name = input('Enter Friend\'s name: ')

socket_server.connect((server_host, sport))

socket_server.send(name.encode())
server_name = socket_server.recv(1024)
server_name = server_name.decode()

print(server_name, ' has joined...')
while True:
    message = (socket_server.recv(1024)).decode()
    print(server_name, ":", message)
    message = input("Me : ")
    socket_server.send(message.encode())

为什么你说你在尝试多线程而实际上你没有尝试?为什么你重复了这行
conn,add=new\u socket.accept()
?问题是什么?