Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.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_Multithreading_Sockets_Client_Python Multithreading - Fatal编程技术网

套接字通信Python与多线程&;多客户端到多服务器

套接字通信Python与多线程&;多客户端到多服务器,python,multithreading,sockets,client,python-multithreading,Python,Multithreading,Sockets,Client,Python Multithreading,我是Python套接字通信新手。我有一个关于多客户端和多线程的问题。我正在为服务器使用SocketTest3应用程序。我打开4个“SocketTest3”程序。它们都在本地主机上工作,但端口条目不同。后来,我创建的线程打开了我的客户机。到目前为止没有问题。但是我的客户端线程是按顺序打开的。问题就从这里开始。我希望这些线程同时启动。因为如果队列顶部的客户端无法连接到服务器,我的代码就会在其他客户端被允许连接之前爆炸。关于那个话题你能帮我吗?我的客户代码是 import socket import

我是Python套接字通信新手。我有一个关于多客户端和多线程的问题。我正在为服务器使用SocketTest3应用程序。我打开4个“SocketTest3”程序。它们都在本地主机上工作,但端口条目不同。后来,我创建的线程打开了我的客户机。到目前为止没有问题。但是我的客户端线程是按顺序打开的。问题就从这里开始。我希望这些线程同时启动。因为如果队列顶部的客户端无法连接到服务器,我的代码就会在其他客户端被允许连接之前爆炸。关于那个话题你能帮我吗?我的客户代码是

import socket
import threading
from threading import Thread
import time
import os

class ClientSockets(threading.Thread):
    def __init__(self, ip_1, port_1, ip_2, port_2, ip_3, port_3, ip_4, port_4):

        self.ip_1 = ip_1
        self.port_1 = port_1

        self.ip_2 = ip_2
        self.port_2 = port_2

        self.ip_3 = ip_3
        self.port_3 = port_3

        self.ip_4 = ip_4
        self.port_4 = port_4

        self.start1Condition = True
        self.start2Condition = True
        self.start3Condition = True
        self.start4Condition = True

        try:
            #Creating Socket-Clients
            self.socketClient1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            
            self.socketClient2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

            self.socketClient3 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

            self.socketClient4 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            
            #Connection Validation
            self.socketClient1.connect((self.ip_1, self.port_1))
            print("1.Client connected..")

            self.socketClient2.connect((self.ip_2, self.port_2))
            print("2.Client connected..")
            
            self.socketClient3.connect((self.ip_3, self.port_3))
            print("3.Client connected..")

            self.socketClient4.connect((self.ip_4, self.port_4))
            print("4.Client connected..")

        except Exception as err:
            print(err)

        #Control to Threads
        self.clientThread_1 = Thread(target=self.start_1)
        print("Thread-1 created..")
        self.clientThread_2 = Thread(target=self.start_2)
        print("Thread-2 created..")
        self.clientThread_3 = Thread(target=self.start_3)
        print("Thread-3 created..")
        self.clientThread_4 = Thread(target=self.start_4)
        print("Thread-4 created..")



    def start_1(self):

        
        try:
            while self.start1Condition:

                self.yanit1 = self.socketClient1.recv(4096)
                print(self.port_1,":",self.yanit1.decode())
                
    
            self.clientThread_1.close()

        except Exception as err:
            print(err)

    def send1(self, msg):
        self.socketClient1.sendall(msg.encode())   

    def start_2(self):

        
        try:
            while self.start2Condition:

                self.yanit2 = self.socketClient2.recv(4096)
                print(self.port_2,":",self.yanit2.decode())
                
            self.clientThread_2.close()

        except Exception as err:
            print(err)
    
    def send2(self, msg):
        self.socketClient2.sendall(msg.encode())

    def start_3(self):

        try:
            while self.start3Condition:

                self.yanit3 = self.socketClient3.recv(4096)
                print(self.port_3,":",self.yanit3.decode())
                
            self.clientThread_3.close()

        except Exception as err:
            print(err)
        
    def send3(self, msg):
        self.socketClient3.sendall(msg.encode())    

    def start_4(self):

        try:
            while self.start4Condition:

                self.yanit4 = self.socketClient4.recv(4096)
                print(self.port_4,":",self.yanit4.decode())
                
            self.clientThread_4.close()
        except Exception as err:
            print(err)

    def send4(self, msg):
        self.socketClient4.sendall(msg.encode())


    def run(self):
        self.clientThread_1.start()
        self.clientThread_2.start()
        self.clientThread_3.start()
        self.clientThread_4.start()

if __name__ == "__main__":
    runz = ClientSockets("localhost",8787,"localhost",8788,"localhost",8789,"localhost",8790)
    runz.run()
当我没有启动“8787”时,这是从Sockettest3程序连接的第一个端口,我的代码在启动之前停止工作


致以最诚挚的问候。

您是否尝试过将每个客户端连接包装到自己的try-except中?因为它们都是相同的,所以如果1失败,自然就不能得到2、3或4。@user3832673我将客户机连接从init移动到每个客户机方法中,问题就解决了。现在,即使第一个客户端的端口不活动,我也可以连接其他客户端。谢谢你的建议。