/// <summary> /// Run example /// </summary> public static void RunExample() { SendReceiveOptions nullCompressionSRO = new SendReceiveOptions(DPSManager.GetDataSerializer <ProtobufSerializer>(), null, null); NetworkComms.DefaultSendReceiveOptions = nullCompressionSRO; //We need to define what happens when packets are received. //To do this we add an incoming packet handler for a 'Message' packet type. // //We will define what we want the handler to do inline by using a lambda expression //http://msdn.microsoft.com/en-us/library/bb397687.aspx. //We could also just point the AppendGlobalIncomingPacketHandler method //to a standard method (See AdvancedSend example) // //This handler will convert the incoming raw bytes into a string (this is what //the <string> bit means) and then write that string to the local console window. NetworkComms.AppendGlobalIncomingPacketHandler <PingRequestReturnDC>("Message", (packetHeader, connection, incomingString) => { Console.WriteLine("\n ... Incoming message from " + connection.ToString() + " saying '" + incomingString.ClientID + "'-'" + incomingString.PingID + "'."); }); //Start listening for incoming 'TCP' connections. //We want to select a random port on all available adaptors so provide //an IPEndPoint using IPAddress.Any and port 0. //See also UDPConnection.StartListening() Connection.StartListening(ConnectionType.TCP, new IPEndPoint(IPAddress.Any, 0)); //Print the IP addresses and ports we are listening on to make sure everything //worked as expected. Console.WriteLine("Listening for messages on:"); foreach (System.Net.IPEndPoint localEndPoint in Connection.ExistingLocalListenEndPoints(ConnectionType.TCP)) { Console.WriteLine("{0}:{1}", localEndPoint.Address, localEndPoint.Port); } //We loop here to allow any number of test messages to be sent and received while (true) { //Request a message to send somewhere Console.WriteLine("\nPlease enter your message and press enter (Type 'exit' to quit):"); string stringToSend = Console.ReadLine(); //If the user has typed exit then we leave our loop and end the example if (stringToSend == "exit") { break; } else { //Once we have a message we need to know where to send it //We have created a small wrapper class to help keep things clean here ConnectionInfo targetServerConnectionInfo; ExampleHelper.GetServerDetails(out targetServerConnectionInfo); PingRequestReturnDC test = new PingRequestReturnDC(0, 0); //There are loads of ways of sending data (see AdvancedSend example for more) //but the most simple, which we use here, just uses an IP address (string) and port (integer) //We pull these values out of the ConnectionInfo object we got above and voila! NetworkComms.SendObject("Message", ((System.Net.IPEndPoint)targetServerConnectionInfo.RemoteEndPoint).Address.ToString(), ((System.Net.IPEndPoint)targetServerConnectionInfo.RemoteEndPoint).Port, test); } } //We should always call shutdown on comms if we have used it NetworkComms.Shutdown(); }
/// <summary> /// Run example /// </summary> public static void RunExample() { SendReceiveOptions nullCompressionSRO = new SendReceiveOptions(DPSManager.GetDataSerializer<ProtobufSerializer>(), null, null); NetworkComms.DefaultSendReceiveOptions = nullCompressionSRO; //We need to define what happens when packets are received. //To do this we add an incoming packet handler for a 'Message' packet type. // //We will define what we want the handler to do inline by using a lambda expression //http://msdn.microsoft.com/en-us/library/bb397687.aspx. //We could also just point the AppendGlobalIncomingPacketHandler method //to a standard method (See AdvancedSend example) // //This handler will convert the incoming raw bytes into a string (this is what //the <string> bit means) and then write that string to the local console window. NetworkComms.AppendGlobalIncomingPacketHandler<PingRequestReturnDC>("Message", (packetHeader, connection, incomingString) => { Console.WriteLine("\n ... Incoming message from " + connection.ToString() + " saying '" + incomingString.ClientID + "'-'" + incomingString .PingID+ "'."); }); //Start listening for incoming 'TCP' connections. //We want to select a random port on all available adaptors so provide //an IPEndPoint using IPAddress.Any and port 0. //See also UDPConnection.StartListening() Connection.StartListening(ConnectionType.TCP, new IPEndPoint(IPAddress.Any, 0)); //Print the IP addresses and ports we are listening on to make sure everything //worked as expected. Console.WriteLine("Listening for messages on:"); foreach (System.Net.IPEndPoint localEndPoint in Connection.ExistingLocalListenEndPoints(ConnectionType.TCP)) Console.WriteLine("{0}:{1}", localEndPoint.Address, localEndPoint.Port); //We loop here to allow any number of test messages to be sent and received while (true) { //Request a message to send somewhere Console.WriteLine("\nPlease enter your message and press enter (Type 'exit' to quit):"); string stringToSend = Console.ReadLine(); //If the user has typed exit then we leave our loop and end the example if (stringToSend == "exit") break; else { //Once we have a message we need to know where to send it //We have created a small wrapper class to help keep things clean here ConnectionInfo targetServerConnectionInfo; ExampleHelper.GetServerDetails(out targetServerConnectionInfo); PingRequestReturnDC test = new PingRequestReturnDC(0,0); //There are loads of ways of sending data (see AdvancedSend example for more) //but the most simple, which we use here, just uses an IP address (string) and port (integer) //We pull these values out of the ConnectionInfo object we got above and voila! NetworkComms.SendObject("Message", ((System.Net.IPEndPoint)targetServerConnectionInfo.RemoteEndPoint).Address.ToString(), ((System.Net.IPEndPoint)targetServerConnectionInfo.RemoteEndPoint).Port, test); } } //We should always call shutdown on comms if we have used it NetworkComms.Shutdown(); }