在python中的多线程客户端服务器套接字程序中运行时发送输入时出错

时间:2017-11-17 16:49:38

标签: multithreading python-3.x sockets tcp client-server

我已经制作了这个全双工多线程TCP服务器(python 3.6),它可以一次监听并向多个客户端发送消息(客户端使用python 3的输入函数从客户端向运行时发送消息) )。一切顺利,除非客户端发送除“hello”或“goodbye”之外的消息(我已经预定了对这两个消息的响应)。当客户发送随机消息,如'asdadsk'或'asdasd'或除了hello或goodbye之外的任何其他消息;该服务器被编程为从控制台获取输入并将其发送到客户端。所以,当我给它输入时,它就停在那里。按ENTER无效。没有任何反应。什么都没有达到客户。由于这个客户端也一直在等待响应。

以下是客户端与此服务器之间的交互:

Connected to : 127.0.0.1 : 51811
<socket.socket fd=196, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 28985), raddr=('127.0.0.1', 51811)>
Client Sent: hello
Client Sent: hello
Client Sent: hello
Client Sent: asdasd
>>>>> :asdkkad
asdjajd

asdasda

ad
a
sda
sd

多线程server.py

    import socket
    import _thread


    def threaded_client(sock):

      sock.send(str.encode("Yokoso O-genki-desuka:)"))

      while True:
        data = sock.recv(4096).decode()

        # print(data)
        if data.lower() == 'hello':
            reply = 'Server Output: Dono yō ni watashi wa anata o tasukeru kamo shirenai- How may i help you? :D'
            print('Client Sent: ' + data.lower())
            sock.send(reply.encode())

        elif data.lower() == 'goodbye':
            reply = 'Server Output: Sayōnara :)'
            print('Client Sent: ' + data.lower())
            sock.send(reply.encode())
            break

        elif not data:
            print('Awaiting data...')

        else:
            print('Client Sent: ' + data.lower())
            reply = input(">>>>> :")
            sock.send(reply.encode())

    sock.close()
    return


def main_func():
    TCP_IP = ''
    TCP_PORT = 28990

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    try:
        s.bind((TCP_IP, TCP_PORT))
    except Exception:
        print("Binding Error. Already bound." + str(TCP_IP) + str(TCP_PORT))

    s.listen(5)


    while True:
        conn, addr = s.accept()
        print('Connected to : ' + addr[0] + ' : ' + str(addr[1]))
        _thread.start_new_thread(threaded_client, (conn,))
        print(conn)


if __name__ == '__main__':
    main_func()

多线程client.py

import socket 

host = 'localhost' 
port = 28990
BUFFER_SIZE = 4096



tcpClientA = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
tcpClientA.connect((host, port))

while True:
    data = tcpClientA.recv(BUFFER_SIZE).decode()
    print (" Client received data:" + data)
    MESSAGE = input("Client- Enter message :")
    tcpClientA.send(MESSAGE.encode())

客户方的回应:

 Client received data:Yokoso O-genki-desuka:)
Client- Enter message :hello
 Client received data:Server Output: Dono yō ni watashi wa anata o tasukeru kamo shirenai- How may i help you? :D
Client- Enter message :hello
 Client received data:Server Output: Dono yō ni watashi wa anata o tasukeru kamo shirenai- How may i help you? :D
Client- Enter message :hello
 Client received data:Server Output: Dono yō ni watashi wa anata o tasukeru kamo shirenai- How may i help you? :D
Client- Enter message :asdasd



sdfsdf

我找不到任何解决这个问题的方法。有人可以帮忙吗?

0 个答案:

没有答案