client.py要求用户输入用户名,该用户名显示在终端行中。当用户发送消息时,他们会在终端中看到其用户名,但由于用户名不会发送到服务器,因此服务器将其视为主机和端口,也将其视为其他端口。我想将用户名发送到服务器,服务器将使用其用户名对其进行调用。 server.py是这样的:
# server.py
import sys, socket, select
HOST = ''
SOCKET_LIST = []
RECV_BUFFER = 2048
PORT = 1969
def chat_server():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT))
server_socket.listen(10)
# add server socket object to the list of readable connections
SOCKET_LIST.append(server_socket)
print "Chat server started on port " + str(PORT)
while 1:
# get the list sockets which are ready to be read through select
# 4th arg, time_out = 0 : poll and never block
ready_to_read,ready_to_write,in_error = select.select(SOCKET_LIST,[],[],0)
for sock in ready_to_read:
# a new connection request recieved
if sock == server_socket:
sockfd, addr = server_socket.accept()
SOCKET_LIST.append(sockfd)
print "Client (%s, %s) connected" % addr
broadcast(server_socket, sockfd, "[%s:%s] entered the room\n" % addr)
# a message from a client, not a new connection
else:
# process data recieved from client,
try:
# receiving data from the socket.
data = sock.recv(RECV_BUFFER)
if data:
# there is something in the socket
broadcast(server_socket, sock, "\r" + '[' + str(sock.getpeername()) + '] ' + data)
else:
# remove the socket that's broken
if sock in SOCKET_LIST:
SOCKET_LIST.remove(sock)
# at this stage, no data means probably the connection has been broken
broadcast(server_socket, sock, "Client (%s, %s) is offline\n" % addr)
# exception
except:
broadcast(server_socket, sock, "Client (%s, %s) is offline\n" % addr)
continue
server_socket.close()
# broadcast chat messages to all connected clients
def broadcast (server_socket, sock, message):
for socket in SOCKET_LIST:
# send the message only to peer
if socket != server_socket and socket != sock :
try :
socket.send(message)
except :
# broken socket connection
socket.close()
# broken socket, remove it
if socket in SOCKET_LIST:
SOCKET_LIST.remove(socket)
if __name__ == "__main__":
sys.exit(chat_server())
这是client.py:
# client.py
import sys, socket, select
username = raw_input("Enter a username: ") # type: str
def chat_client():
if(len(sys.argv) < 3) :
print 'Usage : python client.py hostname port'
sys.exit()
host = sys.argv[1]
port = int(sys.argv[2])
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
# connect to remote host
try :
s.connect((host, port))
except :
print 'Can not connect to server.'
sys.exit()
print 'Connected to chat server. You can now send messages'
sys.stdout.write('[' + username + '] '); sys.stdout.flush()
while 1:
socket_list = [sys.stdin, s]
# Get the list sockets which are readable
read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])
for sock in read_sockets:
if sock == s:
# incoming message from remote server, s
data = sock.recv(4096)
if not data :
print '\nDisconnected from chat server'
sys.exit()
else :
#print data
sys.stdout.write(data)
sys.stdout.write('[' + username + '] '); sys.stdout.flush()
else :
# user entered a message
msg = sys.stdin.readline()
s.send(msg)
sys.stdout.write('[' + username + '] '); sys.stdout.flush()
if __name__ == "__main__":
sys.exit(chat_client())
答案 0 :(得分:0)
您可以做一些小改动以使其正常工作:
添加一个新的数据结构,并希望收到一个用户名和一个新的连接
HOST = ''
SOCKET_LIST = []
USERNAMES = {} #new data structure
RECV_BUFFER = 2048
PORT = 1969
def chat_server():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT))
server_socket.listen(10)
# add server socket object to the list of readable connections
SOCKET_LIST.append(server_socket)
print "Chat server started on port " + str(PORT)
while 1:
# get the list sockets which are ready to be read through select
# 4th arg, time_out = 0 : poll and never block
ready_to_read,ready_to_write,in_error = select.select(SOCKET_LIST,[],[],0)
for sock in ready_to_read:
# a new connection request received
if sock == server_socket:
sockfd, addr = server_socket.accept()
SOCKET_LIST.append(sockfd)
#wait to receive the username, you may want to set a timer and handle the case in which no username is sent for whichever reason
username = sockfd.recv(1024)
print "Client (%s, %s) connected" % addr
# add a key - value pair to be able to map a connection to a username
USERNAMES[sockfd.getpeername()] = username
broadcast(server_socket, sockfd, "%s entered the room\n" % username)
# a message from a client, not a new connection
else:
# process data received from client,
try:
# receiving data from the socket.
data = sock.recv(RECV_BUFFER)
if data:
# there is something in the socket
broadcast(server_socket, sock, "\r" + '[' + str(USERNAMES[sock.getpeername()]) + '] ' + data) #use the data structure previously defined to send the correct usernames
else:
# remove the socket that's broken
if sock in SOCKET_LIST:
SOCKET_LIST.remove(sock)
# at this stage, no data means probably the connection has been broken
broadcast(server_socket, sock, "Client (%s, %s) is offline\n" % addr)
# exception
except:
broadcast(server_socket, sock, "Client (%s, %s) is offline\n" % addr)
continue
server_socket.close()
[...]
在建立连接后发送用户名
username = raw_input("Enter a username: ") # type: str
def chat_client():
[...]
# connect to remote host
try :
s.connect((host, port))
except :
print 'Can not connect to server.'
sys.exit()
print 'Connected to chat server. You can now send messages'
sys.stdout.write('[' + username + '] '); sys.stdout.flush()
# immediately send the username
s.send(username);
while 1:
[...]
当然,您可能需要实现一些安全性,例如在接收用户名时在服务器上设置超时,但这应该可以按需工作