Skip to content

Instantly share code, notes, and snippets.

@jamesmanning
Created May 6, 2012 12:32
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save jamesmanning/2622054 to your computer and use it in GitHub Desktop.
Save jamesmanning/2622054 to your computer and use it in GitHub Desktop.
simple TCP client and server
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
StartListener();
ConnectAsTcpClient();
Console.ReadLine();
}
private static async void ConnectAsTcpClient()
{
using (var tcpClient = new TcpClient())
{
Console.WriteLine("[Client] Connecting to server");
await tcpClient.ConnectAsync("127.0.0.1", 1234);
Console.WriteLine("[Client] Connected to server");
using (var networkStream = tcpClient.GetStream())
{
Console.WriteLine("[Client] Writing request {0}", ClientRequestString);
await networkStream.WriteAsync(ClientRequestBytes, 0, ClientRequestBytes.Length);
var buffer = new byte[4096];
var byteCount = await networkStream.ReadAsync(buffer, 0, buffer.Length);
var response = Encoding.UTF8.GetString(buffer, 0, byteCount);
Console.WriteLine("[Client] Server response was {0}", response);
}
}
}
private static readonly string ClientRequestString = "Some HTTP request here";
private static readonly byte[] ClientRequestBytes = Encoding.UTF8.GetBytes(ClientRequestString);
private static readonly string ServerResponseString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><document><userkey>key</userkey> <machinemode>1</machinemode><serial>0000</serial><unitname>Device</unitname><version>1</version></document>\n";
private static readonly byte[] ServerResponseBytes = Encoding.UTF8.GetBytes(ServerResponseString);
private static async void StartListener()
{
var tcpListener = TcpListener.Create(1234);
tcpListener.Start();
var tcpClient = await tcpListener.AcceptTcpClientAsync();
Console.WriteLine("[Server] Client has connected");
using (var networkStream = tcpClient.GetStream())
{
var buffer = new byte[4096];
Console.WriteLine("[Server] Reading from client");
var byteCount = await networkStream.ReadAsync(buffer, 0, buffer.Length);
var request = Encoding.UTF8.GetString(buffer, 0, byteCount);
Console.WriteLine("[Server] Client wrote {0}", request);
await networkStream.WriteAsync(ServerResponseBytes, 0, ServerResponseBytes.Length);
Console.WriteLine("[Server] Response has been written");
}
}
}
}
@cckelly
Copy link

cckelly commented Dec 20, 2020

Is there any way to support multiple clients?

@jamesmanning
Copy link
Author

Is there any way to support multiple clients?

@cckelly sure, just have it fire off each listener client - you can see something that does so at https://stackoverflow.com/a/5339885

@cckelly
Copy link

cckelly commented Dec 21, 2020

Is there any way to support multiple clients?

@cckelly sure, just have it fire off each listener client - you can see something that does so at https://stackoverflow.com/a/5339885

Interesting - is that not synchronous though? I didn't think you could use threads in an async context.

@cckelly
Copy link

cckelly commented Dec 21, 2020

Ah nevermind - looks like ThreadPool is commonly used for async I/O. Thanks for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment