private void SendMessage(int NumRetries, string str)
        {
            IrDAClient client       = null;
            int        CurrentTries = 0;

            do
            {
                try
                {
                    client = new IrDAClient(ServiceName);
                }
                catch (Exception se)
                {
                    if ((CurrentTries >= NumRetries))
                    {
                        throw se;
                    }
                }
                CurrentTries = CurrentTries + 1;
            } while (client == null & CurrentTries < NumRetries);

            if ((client == null))
            {
                StatusBar1.BeginInvoke(new myDelegate(UpdateStatus), new object[] { "Error establishing contact" });

                return;
            }

            System.IO.Stream stream = null;
            try
            {
                stream = client.GetStream();
                stream.Write(System.Text.ASCIIEncoding.ASCII.GetBytes(str), 0, str.Length);
                StatusBar1.BeginInvoke(new myDelegate(UpdateStatus), new object[] { "Message sent!" });
                txtMessagesArchive.Text = str + "\r\n" + txtMessagesArchive.Text;
            }
            catch (Exception e)
            {
                StatusBar1.BeginInvoke(new myDelegate(UpdateStatus), new object[] { "Error sending message." });
            }
            finally
            {
                if ((!(stream == null)))
                {
                    stream.Close();
                }
                if ((!(client == null)))
                {
                    client.Close();
                }
            }
        }
예제 #2
0
파일: Infrarrojos.cs 프로젝트: zapic0/pfc
 /// <summary>
 /// Detiene la conexión.
 /// </summary>
 public override void Parar()
 {
     if (clienteIR != null)
     {
         clienteIR.Close();
         clienteIR = null;
         if (escuchaIR != null)
         {
             escuchaIR.Stop();
         }
         Cursor.Current = Cursors.Default;
     }
 }
        private string ReceiveMessage()
        {
            int          bytesRead = 0;
            IrDAListener listener  = new IrDAListener(ServiceName);
            IrDAClient   client    = null;

            System.IO.Stream stream = null;
            byte[]           Buffer = new byte[MAX_MESSAGE_SIZE - 1];
            string           str    = string.Empty;

            try
            {
                listener.Start();

                //---blocking call---
                client    = listener.AcceptIrDAClient();
                stream    = client.GetStream();
                bytesRead = stream.Read(Buffer, 0, Buffer.Length);

                //---format the received message---
                str = ">" + System.Text.ASCIIEncoding.ASCII.GetString(Buffer, 0, bytesRead);
            }
            catch (SocketException ex)
            {
                //---ignore error---
            }
            catch (Exception e)
            {
                txtMessagesArchive.BeginInvoke(new myDelegate(UpdateStatus),
                                               new object[] { e.ToString() });
            }
            finally
            {
                if ((!(stream == null)))
                {
                    stream.Close();
                }
                if ((!(client == null)))
                {
                    client.Close();
                }
                listener.Stop();
            }
            return(str);
        }
        private void SendMessage(int NumRetries, string str)
        {
            IrDAClient client       = null;
            int        CurrentTries = 0;

            //---try to establish a connection---
            do
            {
                try
                {
                    client = new IrDAClient(ServiceName);
                }
                catch (Exception se)
                {
                    if ((CurrentTries >= NumRetries))
                    {
                        throw se;
                    }
                }
                CurrentTries = CurrentTries + 1;
            } while (client == null & CurrentTries < NumRetries);

            //---timeout occurred---
            if ((client == null))
            {
                txtMessagesArchive.BeginInvoke(new myDelegate(UpdateStatus),
                                               new object[] { "Error establishing contact" });
                return;
            }

            //---send the message over a stream object---
            System.IO.Stream stream = null;
            try
            {
                stream = client.GetStream();
                stream.Write(System.Text.ASCIIEncoding.ASCII.GetBytes(str), 0, str.Length);

                //---update the status bar---
                txtMessagesArchive.BeginInvoke(new myDelegate(UpdateStatus),
                                               new object[] { "Message sent!" });

                //---display the message that was sent---
                txtMessagesArchive.Text = str + Environment.NewLine + txtMessagesArchive.Text;
            }
            catch (Exception e)
            {
                txtMessagesArchive.BeginInvoke(new myDelegate(UpdateStatus),
                                               new object[] { "Error sending message." });
            }
            finally
            {
                if ((!(stream == null)))
                {
                    stream.Close();
                }
                if ((!(client == null)))
                {
                    client.Close();
                }
            }
        }
예제 #5
0
 public override async Task Close()
 {
     await Task.Run(() => _client.Close());
 }