static void Main(string[] args) { NetConfiguration config = new NetConfiguration("durable"); NetClient client = new NetClient(config); client.SimulatedMinimumLatency = 0.05f; client.SimulatedLatencyVariance = 0.025f; client.SimulatedLoss = 0.03f; // wait half a second to allow server to start up in Visual Studio Thread.Sleep(500); // create a buffer to read data into NetBuffer buffer = client.CreateBuffer(); // connect to localhost client.Connect("127.0.0.1", 14242, Encoding.ASCII.GetBytes("Hail from client")); // enable some library messages client.SetMessageTypeEnabled(NetMessageType.BadMessageReceived, true); //client.SetMessageTypeEnabled(NetMessageType.VerboseDebugMessage, true); client.SetMessageTypeEnabled(NetMessageType.ConnectionRejected, true); FileStream fs = new FileStream("./clientlog.txt", FileMode.Create, FileAccess.Write, FileShare.Read); StreamWriter wrt = new StreamWriter(fs); Output(wrt, "Log started at " + DateTime.Now); wrt.Flush(); // create a stopwatch Stopwatch sw = new Stopwatch(); sw.Start(); int loops = 0; while (!Console.KeyAvailable) { NetMessageType type; if (client.ReadMessage(buffer, out type)) { switch (type) { case NetMessageType.StatusChanged: string statusMessage = buffer.ReadString(); NetConnectionStatus newStatus = (NetConnectionStatus)buffer.ReadByte(); if (client.ServerConnection.RemoteHailData != null) { Output(wrt, "New status: " + newStatus + " (" + statusMessage + ") Remote hail is: " + Encoding.ASCII.GetString(client.ServerConnection.RemoteHailData)); } else { Output(wrt, "New status: " + newStatus + " (" + statusMessage + ") Remote hail hasn't arrived."); } break; case NetMessageType.BadMessageReceived: case NetMessageType.ConnectionRejected: case NetMessageType.DebugMessage: case NetMessageType.VerboseDebugMessage: // // These types of messages all contain a string in the buffer; display it. // Output(wrt, buffer.ReadString()); break; case NetMessageType.Data: default: // // For this application; server doesn't send any data... so Data messages are unhandled // Output(wrt, "Unhandled: " + type + " " + buffer.ToString()); break; } } // send a message every second if (client.Status == NetConnectionStatus.Connected && sw.Elapsed.TotalMilliseconds >= 516) { loops++; //Console.WriteLine("Sending message #" + loops); Console.Title = "Client; Messages sent: " + loops; Output(wrt, "Sending #" + loops + " at " + NetTime.ToMillis(NetTime.Now)); NetBuffer send = client.CreateBuffer(); send.Write("Message #" + loops); client.SendMessage(send, NetChannel.ReliableInOrder14); sw.Reset(); sw.Start(); } Thread.Sleep(1); } // clean shutdown client.Shutdown("Application exiting"); wrt.Close(); }
static void Main(string[] args) { NetConfiguration config = new NetConfiguration("durable"); config.MaxConnections = 128; config.Port = 14242; NetServer server = new MyDurableServer(config); server.SetMessageTypeEnabled(NetMessageType.ConnectionApproval, true); server.SetMessageTypeEnabled(NetMessageType.DebugMessage, true); //server.SetMessageTypeEnabled(NetMessageType.VerboseDebugMessage, true); server.SetMessageTypeEnabled(NetMessageType.StatusChanged, true); server.SimulatedMinimumLatency = 0.05f; server.SimulatedLatencyVariance = 0.025f; server.SimulatedLoss = 0.03f; server.Start(); FileStream fs = new FileStream("./serverlog.txt", FileMode.Create, FileAccess.Write, FileShare.Read); StreamWriter wrt = new StreamWriter(fs); Output(wrt, "Log started at " + DateTime.Now); wrt.Flush(); NetBuffer buffer = server.CreateBuffer(); int expected = 1; Console.WriteLine("Press any key to quit"); while (!Console.KeyAvailable) { NetMessageType type; NetConnection sender; if (server.ReadMessage(buffer, out type, out sender)) { switch (type) { case NetMessageType.StatusChanged: string statusMessage = buffer.ReadString(); NetConnectionStatus newStatus = (NetConnectionStatus)buffer.ReadByte(); if (sender.RemoteHailData != null) { Output(wrt, "New status: " + newStatus + " (" + statusMessage + ") Remote hail is: " + Encoding.ASCII.GetString(sender.RemoteHailData)); } else { Output(wrt, "New status: " + newStatus + " (" + statusMessage + ") Remote hail hasn't arrived."); } break; case NetMessageType.BadMessageReceived: case NetMessageType.ConnectionRejected: case NetMessageType.DebugMessage: // // All these types of messages all contain a single string in the buffer; display it // Output(wrt, buffer.ReadString()); break; case NetMessageType.VerboseDebugMessage: wrt.WriteLine(buffer.ReadString()); // don't output to console break; case NetMessageType.ConnectionApproval: if (sender.RemoteHailData != null && Encoding.ASCII.GetString(sender.RemoteHailData) == "Hail from client") { Output(wrt, "Hail ok!"); sender.Approve(Encoding.ASCII.GetBytes("Hail from server")); } else { sender.Disapprove("Wrong hail!"); } break; case NetMessageType.Data: // verify ProcessReceived has done its work int len = (int)buffer.Tag; if (len != buffer.LengthBytes) { Output(wrt, "OUCH! ProcessReceived hasn't done its job!"); } string str = buffer.ReadString(); // parse it int nr = Int32.Parse(str.Substring(9)); if (nr != expected) { Output(wrt, "Warning! Expected " + expected + "; received " + nr + " str is ---" + str + "---"); } else { expected++; Console.Title = "Server; received " + nr + " messages"; } break; default: Output(wrt, "Unhandled: " + type + " " + buffer.ToString()); break; } } // we're not doing anything but reading; to suspend this thread until there's something to read server.DataReceivedEvent.WaitOne(1000); } // clean shutdown wrt.Close(); server.Shutdown("Application exiting"); System.Threading.Thread.Sleep(500); // give network thread time to exit }