void CreateServer() { // Create the server var server = new SimplSocketServer(); // Start listening for client connections on loopback end poiny server.Listen(new IPEndPoint(IPAddress.Loopback, 5000)); // Wait until a new client has connected var connectedClient = server.WaitForNewClient(); // Create a byte array to send var arrayToSend1 = new byte[1000]; // Send it Console.WriteLine($"Server is going to send message of {arrayToSend1.Length} bytes"); server.Send(arrayToSend1, connectedClient); // Get a byte array from the memory pool var arrayToSend2 = PooledMessage.Rent(1000); Console.WriteLine($"Server is going to send a pooled message of {arrayToSend1.Length} bytes"); server.Send(arrayToSend2, connectedClient); // Return message to pool arrayToSend2.ReturnAfterSend(); }
static async Task RunViaSockets() { using (var server = new SimplSocketServer( () => new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) { NoDelay = true })) { server.MessageReceived += (s, e) => { var blob = e.ReceivedMessage.Message; ReverseServer.Reverse(blob); server.Reply(blob, e.ReceivedMessage); }; server.Listen(new IPEndPoint(IPAddress.Loopback, 5000)); await Console.Out.WriteLineAsync( "Server running; type 'q' to exit, anything else to broadcast"); string line; while ((line = await Console.In.ReadLineAsync()) != null) { if (line == "q") { break; } var blob = Encoding.UTF8.GetBytes(line); server.Broadcast(blob); await Console.Out.WriteLineAsync( $"Broadcast {blob.Length} bytes to {server.CurrentlyConnectedClientCount} clients"); } } }
public void Setup() { var socketServer = new SimplSocketServer(CreateSocket); socketServer.Listen(s1); _socketServer = socketServer; var pipeServer = SimplPipelineSocketServer.For <ReverseServer>(); pipeServer.Listen(s2); _pipeServer = pipeServer; _data = new byte[1024]; }
void CreateServer() { // Create the server var server = new SimplSocketServer(); // Create a callback for received array server.MessageReceived += (s, e) => { // Get the message var receivedMessage = e.ReceivedMessage; Console.WriteLine($"Server received message of {receivedMessage.Length} bytes"); // Return the message to the pool receivedMessage.Return(); }; // Start listening for client connections on loopback endpoint server.Listen(new IPEndPoint(IPAddress.Loopback, 5000)); }
void CreateServer() { // Create the server var server = new SimplSocketServer(); // Create a callback for received array server.MessageReceived += (s, e) => { // Get the message var receivedMessage = e.ReceivedMessage; Console.WriteLine($"Server received message of {receivedMessage.Length} bytes"); // Reply to the message with the same message (echo) server.Reply(receivedMessage, receivedMessage); Console.WriteLine($"Server replied to message"); // We cannot not dispose the received message directly, since it may still need to be send // Instead we use the ReturnAfterSend, which will return the message to the pool after sending receivedMessage.ReturnAfterSend(); }; // Start listening for client connections on loopback endpoint server.Listen(new IPEndPoint(IPAddress.Loopback, 5000)); }
public void Listen(IPEndPoint ipEndPoint, bool discoverable = true, string name = "SimplMessageServer", string description = null) { _simplSocketServer.Listen(ipEndPoint, discoverable, name, description); }