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(); } } }
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(); client = listener.AcceptIrDAClient(); stream = client.GetStream(); bytesRead = stream.Read(Buffer, 0, Buffer.Length); str = ">" + System.Text.ASCIIEncoding.ASCII.GetString(Buffer, 0, bytesRead); } catch (SocketException ex) { } catch (Exception e) { StatusBar1.BeginInvoke(new myDelegate(UpdateStatus), new object[] { e.ToString() }); } finally { if ((!(stream == null))) { stream.Close(); } if ((!(client == null))) { client.Close(); } listener.Stop(); } return(str); }