0

我在计算机和服务器之间的 Socket 通信上有问题。发生的情况是,仅当我单击特定按钮时,我才通过套接字建立通信。在第一次沟通中,一切都发生得很完美。如果我再次单击,则不会执行代码。我放了一个断点来查看发生了什么,并看到在给定的行上,他只是在尝试执行该行,但是执行了很长时间。(我总是传递相同的参数)。

长时间处理的线路

serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);

客户代码

    private void Form1_Load(object sender, EventArgs e)
    {
        msg("Client Started");
        clientSocket.Connect("server_ip", 8855);
        //clientSocket.Connect("127.0.0.1", 8873);
        label1.Text = "Client Socket Program - Server Connected ...";
        this.imprimir();
    }

    protected void imprimir()
    {
        NetworkStream serverStream = clientSocket.GetStream();
        byte[] inStream = new byte[10025];

        //// LINE THAT PROCESS FOR A LONG TIME IN THE SECOND TIME.
        serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);

        string returndata = System.Text.Encoding.ASCII.GetString(inStream);
        int ponto = returndata.IndexOf('.');
        returndata = returndata.Substring(0, ponto);
        string[] quebraretorno = returndata.Split(';');

        ServiceReference1.bematechSoapClient bema = new ServiceReference1.bematechSoapClient();
        string r = bema.InformacoesImovelBematech(quebraretorno[0], quebraretorno[1]);
        int retorno = -1;

        retorno = IniciaPorta("COM7");

        if (retorno == 1)
        {
            ConfiguraModeloImpressora(7);
            BematechTX(r);
            AcionaGuilhotina(1);
            FechaPorta();
        }

        clientSocket.Close();
    }

服务器代码

 public void btnImprimeBematech_Locacao()
        {
            Utilidade.QuebraToken tk = new Utilidade.QuebraToken();
            int Credenciada = Convert.ToInt32(tk.CarregaToken(1, HttpContext.Current.Request.Cookies["token"].Value));
            TcpListener serverSocket = new TcpListener(8855);
            int requestCount = 0;
            TcpClient clientSocket = default(TcpClient);
            serverSocket.Start();
            clientSocket = serverSocket.AcceptTcpClient();

            requestCount = 0;

            while ((true))
            {
                try
                {
                    requestCount = requestCount + 1;
                    NetworkStream networkStream = clientSocket.GetStream();
                    string serverResponse = Request.QueryString["id"] + ";" + Credenciada.ToString() + ".";
                    Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);                          
                    networkStream.Write(sendBytes, 0, sendBytes.Length);
                    networkStream.Flush();                    
                }
                catch (Exception ex)
                {                    
                }
            }

            clientSocket.Close();
            serverSocket.Stop();            
        }

谁能帮我 ?

4

1 回答 1

2

似乎在第一次通信时,服务器接受了一个新的套接字并进入了 while 循环。但是在第二次通信期间,它不会返回到 serverSocket.AcceptTCPClient() 即不会退出 while 循环并且不接受任何新套接字。

您需要编辑服务器端代码,以便在当前连接的套接字断开连接时返回到 AcceptTCPClient()(只需添加另一个循环)。并且您需要为已有的 While 循环提供退出条件。

     TcpClient clientSocket = default(TcpClient);
            serverSocket.Start();

            while(True)
           {
                clientSocket = serverSocket.AcceptTcpClient()
                requestCount = 0;
                while ((clientSocket.Connected == true))
                {
                  try
                  {
                    requestCount = requestCount + 1;
                    NetworkStream networkStream = clientSocket.GetStream();
                    string serverResponse = Request.QueryString["id"] + ";" + Credenciada.ToString() + ".";
                    Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);                          
                    networkStream.Write(sendBytes, 0, sendBytes.Length);
                    networkStream.Flush();                    
                  }
                  catch (Exception ex) { }
                }
           }
于 2012-10-25T11:55:38.350 回答