Created
March 29, 2017 16:17
-
-
Save tomasherceg/4ef15cfa7f4a8708503bebc7fd2b22b0 to your computer and use it in GitHub Desktop.
MFF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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