Skip to content

Instantly share code, notes, and snippets.

@tomasherceg
Created March 29, 2017 16:17
Show Gist options
  • Save tomasherceg/4ef15cfa7f4a8708503bebc7fd2b22b0 to your computer and use it in GitHub Desktop.
Save tomasherceg/4ef15cfa7f4a8708503bebc7fd2b22b0 to your computer and use it in GitHub Desktop.
MFF
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace EchoServer
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Echo server is running...");
Console.WriteLine("Press Ctrl-C to exit.");
Console.WriteLine();
var listener = new TcpListener(IPAddress.Any, 50001);
listener.Start();
while (true)
{
var client = listener.AcceptTcpClientAsync()?.Result;
if (client == null)
{
return;
}
Console.WriteLine($"Client {client.Client.RemoteEndPoint} connected...");
Task.Factory.StartNew(() =>
{
var stream = client.GetStream();
while (true)
{
var data = stream.ReadByte();
if (data < 0)
{
break;
}
stream.WriteByte((byte)data);
}
Console.WriteLine($"Client {client.Client.RemoteEndPoint} disconnected...");
});
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment