0

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 occurs the following error when I try to connect to the server

Cannot access a disposed object. Object name: 'System.Net.Sockets.TcpClient'.

So, to work again, I close the client app and open again, and then works normally.

What I Want I have a web application that has to work just on FireFox. I have a printer that I need to access him through DLL, so I made a client app that conects with this Web Application via Socket. I pass some parameter via stream and recover these parameter in client app and then print.

In my Web Application Code

public void btnImprimeBematech_Venda()
        {            
            Utilidade.QuebraToken tk = new Utilidade.QuebraToken();
            int Credenciada = Convert.ToInt32(tk.CarregaToken(1, HttpContext.Current.Request.Cookies["token"].Value));
            TcpListener serverSocket = new TcpListener(8852);            
            int requestCount = 0;
            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"].ToString() + ";" + Credenciada.ToString() + ".";
                        Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);
                        networkStream.Write(sendBytes, 0, sendBytes.Length);
                        networkStream.Flush();
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }

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

I pass via stream a string, and recover in the client app.

Client app Code

`System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();`

private void Form1_Load(object sender, EventArgs e)
    {
        clientSocket.Connect("server_ip", 8852);
        this.imprimir();
    }

private void button3_click(object sender, EventArgs e)
{
    if (clientSocket.Connected == false)
        clientSocket.Connect("server_ip", 8852);            

    this.imprimir();
}

protected void imprimir()
{            
    NetworkStream serverStream = clientSocket.GetStream();
    byte[] inStream = new byte[10025];
    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();
}

I need that everytime that my user click on a specific button on my Web Application, call my btnImprimeBematech_Venda() and then talk to my Client App to print. Nowadays I need to close the Client App and open again everytime that my user click on my button to print.

I don't know so much about Thread but maybe I need to use that. I don't know.

Someone can help me ?

4

1 に答える 1

0

あなたの問題は、Close()クライアントアプリの最後の呼び出しだと思います。Close は、破棄例外を説明するソケットに関連付けられているすべてのリソースを解放します。ソケットを再利用できるようにするために、Shutdown続いて使用してみてください。ここDisconnect(true)のmsdn ドキュメントを参照してください。

または、閉じたままにして、接続するたびに新しいソケットを作成することもできます。

于 2012-10-25T21:17:41.857 に答える