Ejemplo n.º 1
0
        private void btnOK1_Click(object sender, EventArgs e)
        {
            if (this.conectado == true)
            {
                try
                {
                    Protocolo msgToSend = new Protocolo();
                    msgToSend.strElemento = this.cbJanelas.Text;
                    string acao1 = this.cbAcao1.Text;
                    if (acao1 == "Abrir")
                        msgToSend.cmdAcao = Acao.Abrir;

                    if (acao1 == "Fechar")
                        msgToSend.cmdAcao = Acao.Fechar;

                    if (acao1 == "Travar")
                        msgToSend.cmdAcao = Acao.Travar;

                    byte[] byteData = msgToSend.ToByte();

                    this.clientSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), null);

                }
                catch (Exception)
                {
                    MessageBox.Show("Não foi possível efetuar o comando!", "ClienteA", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
Ejemplo n.º 2
0
        private void OnReceive(IAsyncResult ar)
        {
            try
            {
                this.clientSocket.EndReceive(ar);

                Protocolo msgReceived = new Protocolo(byteData);

                string acao = msgReceived.cmdAcao.ToString();

                if (acao == "Desconectar")
                {
                    MessageBox.Show("O servidor desconectou...", "ClienteA");
                    this.Disconnect();
                    if (this.conectado == false)
                    {
                        this.btnDesconectar.Visible = false;
                        this.btnConectar.Visible = true;
                        this.btnOK1.Enabled = false;
                        this.btnOK2.Enabled = false;
                    }
                }
                else
                {
                    MessageBox.Show(msgReceived.strMensagem, "ClienteA OnReceive", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);
                }

            }
            catch (ObjectDisposedException)
            { }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ClienteA OnReceive", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Ejemplo n.º 3
0
 private void Disconnect()
 {
     try
     {
         if (this.conectado == true)
         {
             try
             {
                 Protocolo msgToSend = new Protocolo();
                 msgToSend.strMensagem = null;
                 msgToSend.strElemento = null;
                 msgToSend.cmdAcao = Acao.Desconectar;
                 byte[] b = msgToSend.ToByte();
                 this.clientSocket.Send(b, 0, b.Length, SocketFlags.None);
                 this.clientSocket.Disconnect(false);
                 this.clientSocket.Close();
             }
             catch (ObjectDisposedException)
             { }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message, "ClienteA FormClosing", MessageBoxButtons.OK, MessageBoxIcon.Error);
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "ClienteA Disconnect", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
     finally
     {
         this.conectado = false;
     }
 }