- Asynchronous servers and clients that are very easy to use.
- PacketTcpServer that ensures reception without truncating the size of the data sent.
- HTTP server that is very simple to use.
RawTcpServer tcpServer = new RawTcpServer();
tcpServer.Start(5000);
RawTcpServer tcpServer = new RawTcpServer();
tcpServer.ClientConnected += ClientConnected;
tcpServer.ClientDisconnected += ClientDisconnected;
tcpServer.DataReceived += DataReceived;
tcpServer.Clients
tcpServer.Clients[0].Write(buffer, 0, buffer.Length);
tcpServer.WriteToAllClients(buffer, 0, buffer.Length);
RawTcpClient tcpClient = new RawTcpClient();
tcpClient.Connect("127.0.0.1", 5000);
RawTcpClient tcpClient = new RawTcpClient();
tcpClient.Connected += Connected;
tcpClient.Disconnected += Disconnected;
tcpClient.DataReceived += DataReceived;
tcpClient.Write(buffer, 0, buffer.Length);
See the example project for details.
HttpServer httpServer = new HttpServer();
httpServer.HttpRequest += (sender, e) =>
{
return new HttpResponse(HttpStatusCode.OK);
};
httpServer.Start(); // default start port is 80
Echo Server / Client
static void Main(string[] args)
{
using (RawTcpServer tcpServer = new RawTcpServer())
{
tcpServer.ClientConnected += (sender, e) =>
Console.WriteLine($"[{e}]: Connected"); // new client connected
tcpServer.ClientDisconnected += (sender, e) =>
Console.WriteLine($"[{e}]: Disconnected"); // client disconnected
tcpServer.DataReceived += (sender, e) =>
{
byte[] readBytes = e.Client.ReadExisting(); // read all data
string dataString = readBytes.Aggregate( // data to hex string
new StringBuilder(32),
(stringBuilder, data) => stringBuilder.Append($" 0x{data.ToString("X2")}")
).ToString().Trim();
Console.WriteLine($"[{e.Client}]: {dataString}");
e.Client.Write(readBytes, 0, readBytes.Length); // return same data
};
try
{
tcpServer.Start(5000);
Console.WriteLine("Listening for connections...");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.ReadLine();
}
}
static void Main(string[] args)
{
using (RawTcpClient tcpClient = new RawTcpClient())
{
tcpClient.Connected += (sender, e) =>
Console.WriteLine($"Connect to [{e.RemoteEndPoint}]");
tcpClient.Disconnected += (sender, e) =>
Console.WriteLine($"{Environment.NewLine}Disconnected from [{e.RemoteEndPoint}]");
tcpClient.DataReceived += (sender, e) =>
{
if (sender is RawTcpClient rawTcpClient)
{
byte[] readBytes = rawTcpClient.ReadExisting(); // read all data
Console.WriteLine($"DataReceived: {Encoding.ASCII.GetString(readBytes)}");
}
};
try
{
tcpClient.Connect("127.0.0.1", 5000);
while (true)
{
string line = Console.ReadLine();
byte[] buffer = Encoding.ASCII.GetBytes(line);
tcpClient.Write(buffer, 0, buffer.Length);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.ReadLine();
}
}
}
Packet Echo Server / Client
The size of the received data is received without being cut off.
static void Main(string[] args)
{
using (PacketTcpServer tcpServer = new PacketTcpServer())
{
tcpServer.ClientConnected += (sender, e) =>
Console.WriteLine($"[{e}]: Connected"); // new client connected
tcpServer.ClientDisconnected += (sender, e) =>
Console.WriteLine($"[{e}]: Disconnected"); // client disconnected
tcpServer.PacketReceived += (sender, e) =>
{
if (sender is PacketTcpServer packetTcpServer)
{
Console.WriteLine($"[{e.Packet.IPEndPoint}]: PacketReceived (PacketLength: {e.Packet.PacketData.Length})");
packetTcpServer.WritePacket(e.Packet.TcpClient, e.Packet.PacketData); // return same packet
}
};
try
{
tcpServer.Start(5000);
Console.WriteLine("Listening for connections...");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.ReadLine();
}
}
static void Main(string[] args)
{
using (PacketTcpClient tcpClient = new PacketTcpClient())
{
tcpClient.Connected += (sender, e) =>
Console.WriteLine($"Connect to [{e.RemoteEndPoint}]");
tcpClient.Disconnected += (sender, e) =>
Console.WriteLine($"{Environment.NewLine}Disconnected from [{e.RemoteEndPoint}]");
tcpClient.PacketReceived += (sender, e) =>
{
Console.WriteLine($"PacketReceived: (PacketLength: {e.PacketData.Length})");
};
try
{
tcpClient.Connect("127.0.0.1", 5000);
tcpClient.WritePacket(new byte[1024]); // send 1024 bytes
tcpClient.WritePacket(new byte[1024 * 1024]); // send 1024 * 1024 bytes
tcpClient.WritePacket(new byte[1024 * 1024 * 10]); // send 1024 * 1024 * 10 bytes
Console.ReadLine();
tcpClient.Disconnect();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.ReadLine();
}
}
Http Server
static void Main(string[] args)
{
using (var httpServer = new SimpleTcp.Server.Http.HttpServer())
{
httpServer.HttpRequest += (sender, e) =>
{
Console.WriteLine($"[{e.Request.IPEndPoint}] -> [{e.Request.Url}]");
switch(e.Request.Url)
{
case "/":
return new HttpResponse(HttpStatusCode.OK)
{
Content = Encoding.UTF8.GetBytes(
"<!DOCTYPE html>" +
"<html>" +
"<head>" +
"<meta charset=\"UTF-8\">" +
"<title>SimpleTcp HttpServer Example</title>" +
"</head>" +
"<body>" +
"Hello, World !!<br/><a href=\"https://github.com/akon47/SimpleTcp\">Github</a>" +
"</body>" +
"</html>")
};
default:
return new HttpResponse(HttpStatusCode.NotFound);
}
};
httpServer.Start();
Console.ReadLine();
}
}
If you find a bug, please report to us posting issues on GitHub.