【问题标题】:What is the problem with the following python socket server code?以下 python 套接字服务器代码有什么问题?
【发布时间】:2020-02-19 15:18:32
【问题描述】:

为什么即使在第一个连接关闭后,以下服务器代码似乎也无法接受与客户端的新连接?

def bindPort(port):
    global return_response
    serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        serverSocket.bind(('', port))
    except:
        print("Cannot bind to port. Error: " + str(sys.exc_info))

    serverSocket.listen(2)
    print("The server is ready to receive")
    count = 0
    while True:
        connectionSocket, addr = serverSocket.accept()
        count += 1
        print("Accepted {} connections so far.".format(count))
        print('Connection established from {}'.format(addr))
        while True:
            sentence = connectionSocket.recv(1024)
            if not sentence:
                print("Empty")
                break
            print(sentence)
            #parseIncomingRequest(sentence.decode())
            print(return_response)
            connectionSocket.send(return_response.encode())
            return_response = ''
        print("Closing loop")
        connectionSocket.close() 

【问题讨论】:

    标签: python sockets server serversocket


    【解决方案1】:

    对于没有正确阅读问题,我深表歉意

    1)在使用前声明返回响应变量

    2) 如果您想接受另一个连接,请跳出第二个 while 循环。在问题中,您说在套接字关闭后没有接受任何连接,但要关闭套接字,您必须跳出第二个while循环。或者在我看来你应该删除第二个循环。

    3)如果您想继续监听套接字并且仍然能够接收其他连接,请使用线程模块

    def bindPort(port):
      global return_response
      serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      try:
        serverSocket.bind(('', port))
      except:
        print("Cannot bind to port. Error: " + str(sys.exc_info))
    
      serverSocket.listen(2)
      print("The server is ready to receive")
      count = 0
      while True:
        connectionSocket, addr = serverSocket.accept()
        count += 1
        print("Accepted {} connections so far.".format(count))
        print('Connection established from {}'.format(addr))
        sentence = connectionSocket.recv(1024)    
        if not sentence:
              print("Empty")
              break
        print(sentence)
         #parseIncomingRequest(sentence.decode())
        return_response = ''
        print(return_response)
        connectionSocket.send(return_response.encode())
        connectionSocket.shutdown(socket.SHUT_RDWR)
        connectionSocket.close() 
    

    4) 或者如果您想保留循环以进行进一步修改

    import socket
    
    def bindPort(port):
      global return_response
      serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      try:
        serverSocket.bind(('', port))
      except:
        print("Cannot bind to port. Error: " + str(sys.exc_info))
    
      serverSocket.listen(2)
      print("The server is ready to receive")
      count = 0
      while True:
        connectionSocket, addr = serverSocket.accept()
        count += 1
        print("Accepted {} connections so far.".format(count))
        print('Connection established from {}'.format(addr))
        while True:
    
            sentence = connectionSocket.recv(1024)
            if not sentence:
                print("Empty")
                break
            print(sentence)
            #parseIncomingRequest(sentence.decode())
            return_response = 'a'
            print(return_response)
            connectionSocket.send(return_response.encode())
            break
    
        print("Closing loop")
        connectionSocket.shutdown(socket.SHUT_RDWR)
        connectionSocket.close()
    

    【讨论】:

      猜你喜欢
      • 2021-04-30
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多