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);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Da comienzo a un servicio de comunicaciones como Servidor.
 /// </summary>
 public override void Servir()
 {
     try
     {
         Cursor.Current = Cursors.WaitCursor;
         clienteIR      = new IrDAClient();
         extremoIR      = new IrDAEndPoint(informacionIR[seleccionado].DeviceID, "Dame una Vida");
         escuchaIR      = new IrDAListener(extremoIR);
         escuchaIR.Start();
         clienteIR         = escuchaIR.AcceptIrDAClient();
         Cursor.Current    = Cursors.Default;
         this.nombreRemoto = clienteIR.RemoteMachineName;
         if (OnAlguienConectado != null)
         {
             OnAlguienConectado(this, new EventArgs());
         }
     }
     catch { }
 }