无法在python中启动套接字客户端

时间:2019-06-09 05:46:55

标签: c# python-2.7

我在Windows 7中使用python 2.7。

我使用python编写套接字客户端。当我启动套接字服务器时,它停留在接受状态,然后运行客户端,但是它始终停留在接受状态。

我不知道为什么客户没有工作。 服务器和客户端在同一台PC上。

    shortcut = QtGui.QShortcut(QtGui.QKeySequence(self.tr("A+D")), self)
    shortcut.activated.connect(lambda: self.ui.start.isEnabled() and 
    self.when_debug())



@QtCore.Slot()
def when_debug(self):
    client = Meter_Client("8021")
    time.sleep(2)
    client.connect_to_server_debug()
    client.start_client()











class Meter_Client(object):
    def __init__(self, net_port):
     self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     self.socket_msg_queue = queue.Queue()
     self.socket_thread = None
     self.net_port = int(net_port)
     self.is_exit = False
     self.cur_meter_inst = None
     self.vol_meter_inst = None

    def connect_to_server_debug(self):
     SERVER_ADDR = ('127.0.0.1', self.net_port)
     self.socket.connect(SERVER_ADDR)
     self.socket.send("debug")
     print 'debug sent' 






    def start_client(self):
     self.socket_thread = socket_thread(self.socket_msg_queue, 
         self.socket)
     self.socket_thread.start()

服务器程序是用C#编写的:

   public void SocketServie()
    {

        string host = "127.0.0.1";
        int port = 8021;
        socket.SetSocketOption(SocketOptionLevel.Socket, 
         SocketOptionName.ReuseAddress, true);   
        socket.Bind(new IPEndPoint(IPAddress.Parse(host), port));
        socket.Listen(100);  
        Thread SocketThread = new Thread(this.ListenClientConnect);  
        SocketThread.IsBackground = true;
        SocketThread.Start();


        string host_2 = "127.0.0.1";
        int port_2 = 8024
        socket_2.SetSocketOption(SocketOptionLevel.Socket, 
        SocketOptionName.ReuseAddress, true); 
        socket_2.Bind(new IPEndPoint(IPAddress.Parse(host_2), port_2));
        socket_2.Listen(100);  
        Thread SocketThread_2 = new Thread(this.ListenClientConnect);
        SocketThread_2.IsBackground = true;
        SocketThread_2.Start();
    }


    private void ListenClientConnect()
    {

        while (true)
        {


            this.clientSocket = socket.Accept();


            Thread receiveThread = new Thread(new 
            ParameterizedThreadStart(ReceiveMessage));

            receiveThread.IsBackground = true;

            receiveThread.Start(this);
            Thread.Sleep(1000);
        }
    }

0 个答案:

没有答案
相关问题