Created
May 7, 2020 10:10
-
-
Save xsoheilalizadeh/13c5868ba3e17fb8b24cdfef67feb743 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| class Program | |
| { | |
| private static IPEndPoint IPEndPoint = new IPEndPoint(IPAddress.Loopback, 8801); | |
| private static TcpClient Client = new TcpClient(IPEndPoint); | |
| static async Task Main(string[] args) | |
| { | |
| Console.WriteLine($"Started on {IPEndPoint}"); | |
| await Client.ConnectAsync(IPAddress.Loopback, 8800); | |
| var message = "Hello, TCP!"; | |
| var messageAsBytes = Encoding.UTF8.GetBytes(message); | |
| var stream = Client.GetStream(); | |
| await stream.WriteAsync(messageAsBytes); | |
| Console.WriteLine($"Sent: {message}"); | |
| Console.ReadKey(); | |
| } | |
| } |
This file contains hidden or 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
| class Program | |
| { | |
| public static TcpListener Server = new TcpListener(IPAddress.Loopback, 8800); | |
| static async Task Main(string[] args) | |
| { | |
| Server.Start(); | |
| var buffer = new byte[100]; | |
| while (true) | |
| { | |
| Console.WriteLine($"Listening on {Server.LocalEndpoint}"); | |
| var client = await Server.AcceptTcpClientAsync(); | |
| Console.WriteLine($"Connected to {client.Client.RemoteEndPoint}"); | |
| var stream = client.GetStream(); | |
| while (await stream.ReadAsync(buffer, 0, buffer.Length) != 0) | |
| { | |
| var data = Encoding.UTF8.GetString(buffer); | |
| Console.WriteLine($"Received: {data}"); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment