Skip to content

Instantly share code, notes, and snippets.

@xsoheilalizadeh
Created May 7, 2020 10:10
Show Gist options
  • Select an option

  • Save xsoheilalizadeh/13c5868ba3e17fb8b24cdfef67feb743 to your computer and use it in GitHub Desktop.

Select an option

Save xsoheilalizadeh/13c5868ba3e17fb8b24cdfef67feb743 to your computer and use it in GitHub Desktop.
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();
}
}
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