Lucas_Santos
Lucas_Santos

Reputation: 4740

Socket communication take a long long time

I have a problem on the Socket communication between a computer and the server. What happens is that I establish communication via socket only if I click on a particular button. In the first communication, everything happens perfectly. If I click again, the code is not executed. I put a breakpoint to see what is happening, and saw that on a given line, he is simply trying to execute the line, but for a long long time. (I always pass the same parameter).

Line that Process for a Long Long time

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

Client Code

    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();
    }

Server Code

 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();            
        }

Can anyone help me ?

Upvotes: 0

Views: 1524

Answers (1)

yohannist
yohannist

Reputation: 4194

It seems on the first communication, the server accepts a new socket and goes in to the while loop. But during the second communication, it doesn't go back to serverSocket.AcceptTCPClient() i.e. Doesn't get out of the while loop and is not accepting any new socket.

you need to edit the server side code so that it goes back to AcceptTCPClient() when the currently connected socket disconnects(just add another loop). And you need to provide an exit condition to the While loop you already have.

     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) { }
                }
           }

Upvotes: 2

Related Questions