using System;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace Server_Dashboard_Socket {
///
/// Basic echo server to test the socket connection
///
public class EchoServer {
///
/// Start the socket on 127.0.0.1:9000
///
/// A port that is not already in use
public void Start(int port = 9000) {
//Creates a new endpoint on 127.0.0.1 and the port 9000
IPEndPoint endPoint = new IPEndPoint(IPAddress.Loopback, port);
//Creates a new Socket on the given endpoint
Socket socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
//Bind the endpoint to the socket
socket.Bind(endPoint);
//Define how many Clients you want to have connected max
socket.Listen(128);
//Just run the Task forever
_ = Task.Run(() => DoEcho(socket));
}
///
/// Listens for messages and sends them back asap
///
/// The socket to work with
/// poop
private async Task DoEcho(Socket socket) {
while (true) {
//Listen for incomming connection requests and accept them
Socket clientSocket = await Task.Factory.FromAsync(
new Func(socket.BeginAccept),
new Func(socket.EndAccept),
null).ConfigureAwait(false);
//Create a network stream and make it the owner of the socket
//This will close the socket if the stream is closed and vice verca
using(NetworkStream stream = new NetworkStream(clientSocket, true)) {
//New buffer for the message in bytes
byte[] buffer = new byte[1024];
while (true) {
//Read everything that somes in
int bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
//If its 0 just leave since its a obscolete connection
if (bytesRead == 0)
break;
//Send it back to the sender
await stream.WriteAsync(buffer, 0, bytesRead).ConfigureAwait(false);
}
}
}
}
}
}