Python中的套接字模块管道错误和多客户端

时间:2017-08-31 10:43:48

标签: python sockets tcp

目标是让多个客户端连接到服务器,并在它们仍然连接时发送数据包。 One-One通信工作没有任何问题,并且没有损坏的管道问题,当我们有多个客户端连接到服务器时会出现问题。

    import socket
    import os
    import time
    bind_ip      = '192.168.4.4'
    bind_port    =  12345
    payload      =  []
    devices      =  []
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
    server.bind((bind_ip, bind_port))   
    server.listen(5)  # max backlog of connections
    print 'Listening on {}:{}'.format(bind_ip, bind_port)

    def send(counts, payload):
        x = counts     #No of iterations.
        y = 0

        start_time = time.time()
        while(x != 0):     
            try:
                client_socket, address = server.accept()
                devices.append(client_socket)
            except socket.error:
                break

            print client_socket
            print 'Accepted connection from {}:{}'.format(address[0], address[1])
            Post.IP.append(address[0])

            for z in devices:

                request = z.recv(1024)
                print 'Received: {}'.format(request)
                z.sendall(payload)

            x = x-1
            print "Count: " ,x+1
            print ""

        elapsed_time = time.time() - start_time
        print ""
        print "Elapsed Time for " + str(10 * len(data)) + " packets is " + str(elapsed_time)
        print ""
        print "Connections"
        print Post.IP[0]
        client_socket.close()

send(2, bytearray(112, 100, 100, 100, 1))

所以我尝试的是制作一个可用客户列表,将它们附加到列表中并分别与它们中的每一个进行对话。一旦计数超过3,就会出现破裂的管道错误。

我怀疑服务器在重新开始之前关闭连接,对此有何帮助?我应该使用线程在后台扫描吗?

0 个答案:

没有答案