public void Listen(int port, IQuoteSource quoteSource) { _quoteSource = quoteSource; _server = new UdpClient(port); while (true) { var remoteEP = new IPEndPoint(IPAddress.Any, port); _server.Receive(ref remoteEP); Log.Information("Request from {RemoteEndPoint}", remoteEP); Quote quote = quoteSource.GetQuote(); byte[] quoteBytes = Encoding.ASCII.GetBytes(quote + "\n"); _server.Send(quoteBytes, quoteBytes.Length, remoteEP); Log.Information("Sent quote {Quote} to {RemoteEndPoint}", quote, remoteEP); } }
public void Listen(int port, IQuoteSource quoteSource) { _quoteSource = quoteSource; _server = new TcpListener(IPAddress.Any, port); _server.Start(); while (true) { var client = _server.AcceptTcpClient(); Log.Information("Request from {RemoteEndPoint}", client.Client.RemoteEndPoint); Quote quote = quoteSource.GetQuote(); byte[] quoteBytes = Encoding.ASCII.GetBytes(quote + "\n"); client.GetStream().Write(quoteBytes, 0, quoteBytes.Length); Log.Information("Sent quote {Quote} to {RemoteEndPoint}", quote, client.Client.RemoteEndPoint); client.Close(); } }