static async Task Main(string[] args) { var buffer = new byte[1024]; try { using (var client = MailSlot.CreateClient("test1")) { for (int indx = 0; indx < 50; indx++) { var msg = $"Writing line #{indx}."; // var count = Encoding.UTF8.GetBytes(msg, 0, msg.Length, buffer, 0); var count = Encoding.UTF8.GetBytes(msg, buffer); buffer[count++] = MessageSeparator; await Task.Delay(100); // small delay between messages await client.WriteAsync(buffer, 0, count); // This leads to "Incorrect Function" error on full framework: // client.Flush(true) or await client.FlushAsync(); client.Flush(false); } } } catch (EndOfStreamException) { Console.WriteLine("Server closed mail slot."); Console.ReadLine(); } }
static void Main(string[] args) { Console.WriteLine(@"Environment.MachineName --> {0}", Environment.MachineName); Console.WriteLine(@"Dns.Hostname --> {0}", Dns.GetHostName()); Console.WriteLine(@"IPAdress --> {0}", Network.FindIPAddress(true).ToString()); Console.WriteLine(); Console.WriteLine(@"Enter Scope of search (domain name, computer name or '*' for primary domain : "); string scope = Console.ReadLine(); Console.WriteLine(@"Enter Mailslot name to search (production, dev, test etc) : "); string mailslotname = Console.ReadLine(); MailSlot listening_ms; try { // Create local Listening mailslot to get answer from server listening_ms = new MailSlot(@"listener"); Console.WriteLine(@"Creating listeningMailSlot: {0}", listening_ms.Filename); //Creating Remote mailslot MailSlot ms = new MailSlot(mailslotname, scope); Console.WriteLine(@"Creating Remote MailSlot {0}", ms.Filename); Console.WriteLine(@"Creating Stream"); StreamWriter sw = new StreamWriter(ms.FStream); String message = String.Format(@"{0}:{1}", Environment.MachineName, @"listener"); sw.WriteLine(message); sw.Flush(); Console.WriteLine(@"Wrote to stream: {0}", message); sw.Dispose(); ms.Dispose(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.StackTrace); return; } StreamReader sr = new StreamReader(listening_ms.FStream); while (true) { try { while (listening_ms.IsMessageInSlot > 0) { Console.WriteLine(@"Messages {0}, {1}", listening_ms.IsMessageInSlot, sr.ReadLine()); } } catch (Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); } Thread.Sleep(100); } //Did not implement a way of exiting while loop so the following is unreachable. //sr.Dispose(); }
static void Main(string[] args) { Console.WriteLine(@"Environment.MachineName --> {0}", Environment.MachineName); Console.WriteLine(@"Dns.Hostname --> {0}", Dns.GetHostName()); Console.WriteLine(@"IPAdress --> {0}", Network.FindIPAddress(true).ToString()); Console.WriteLine(@"Enter Mailslot name to Host (production, dev, test etc) : "); string mailslotname = Console.ReadLine(); MailSlot ms, remote_ms; StreamReader sr; try { Console.WriteLine(@"Creating Listening MailSlot"); ms = new MailSlot(mailslotname); Console.WriteLine(@"Creating Stream"); sr = new StreamReader(ms.FStream); } catch (Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); return; } while (true) { try { if (ms.IsMessageInSlot > 0) { Console.WriteLine(@"Messages waiting --> {0}", ms.IsMessageInSlot); string message = sr.ReadLine(); Console.WriteLine(); Console.WriteLine(@"Message --> {0}", (message != null)?message:@"NULL"); string remote_env = message.Substring(message.IndexOf(':') + 1); Console.WriteLine(@"Remote Mailslot Name --> {0}", remote_env); string scope = message.Substring(0, message.IndexOf(':')); Console.WriteLine(@"Remote Adresse --> {0}", scope); //Create remote mailslot to answer remote_ms = new MailSlot(remote_env, scope); StreamWriter sw = new StreamWriter(remote_ms.FStream); sw.WriteLine(@"Server Name --> {0}", Environment.MachineName); sw.Flush(); sw.WriteLine(@"Server Name (DNS) --> {0}", Dns.GetHostName()); sw.Flush(); sw.WriteLine(@"Server IP Adress --> {0}", Network.FindIPAddress(true).ToString()); sw.Flush(); sw.WriteLine(@"This message was sent using windows Mailslots..."); sw.Flush(); sw.Dispose(); remote_ms.Close(); Console.WriteLine(@"Message sent..."); Console.WriteLine(@"********************************************************************"); } } catch (Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); } Thread.Sleep(100); } //Did not implement a way of exiting while loop so the following is unreachable. //sr.Dispose(); //ms.Dispose(); }