public class Server { #region -- Variables private TcpListener tcpListener; private Thread listenThread; public List<TcpClient> Connections = new List<TcpClient>(); #endregion public Server(int Port) { // - Create the variables. this.tcpListener = new TcpListener(IPAddress.Any, Port); this.listenThread = new Thread(new ThreadStart(SearchClient)); // -- Start the Thread. this.listenThread.Start(); } public void SearchClient() { // -- Start the Listener. this.tcpListener.Start(); // -- Loop. while (true) { // -- Blocks until there is an incoming connection. try { TcpClient Client = this.tcpListener.AcceptTcpClient(); // -- Create a new Thread to handle the connection. Thread ClientThread = new Thread(new ParameterizedThreadStart(HandleClient)); // -- Start the Thread. ClientThread.Start(Client); } catch { break; } } } public void HandleClient(object Client) { // -- Client. TcpClient TcpClient = (TcpClient)Client; // -- Add the client to the list. Connections.Add(TcpClient); // -- Stream. NetworkStream ClientStream = TcpClient.GetStream(); // -- Buffer. byte[] Buffer = new byte[4096]; // -- To-use later. int bytesRead; // -- Loop until asked to stop. while (true) { // -- Initialize the integer. bytesRead = 0; // -- Attempt to read. try { // -- Blocks until the client sends a message. bytesRead = ClientStream.Read(Buffer, 0, Buffer.Length); } catch { break; /* Socket Exception*/ } // -- Did the client disconnect? if (bytesRead == 0) { break; } // -- Handle buffer here. } // -- Close the client. TcpClient.Close(); // -- Remove the client from the list. Connections.Remove(TcpClient); } public void SendBuffer(TcpClient TcpClient, byte[] Buffer) { TcpClient.GetStream().Write(Buffer, 0, Buffer.Length); } public void StopServer() { foreach (TcpClient Client in Connections) { // -- Close the client. Client.Close(); } tcpListener.Stop(); } } public class Client { #region -- Variables private TcpClient tcpClient; private Thread listenThread; public bool GotConnection { get; private set; } public bool AllowedRead { get; set; } private string _IP; private int _Port; #endregion public Client(string IP, int Port) { // -- Set the variables. _IP = IP; _Port = Port; // - Create the variables. this.tcpClient = new TcpClient(); this.listenThread = new Thread(new ThreadStart(HandleServer)); // -- Start the Thread. this.listenThread.Start(); } public void HandleServer() { // -- Stream. NetworkStream ClientStream = tcpClient.GetStream(); // -- Buffer. byte[] Buffer = new byte[4096]; // -- To-use later. int bytesRead; // -- Connection. GotConnection = true; // -- Loop until asked to stop. while (true) { // -- Allowed Read. if (!AllowedRead) break; // -- Initialize the integer. bytesRead = 0; // -- Attempt to read. try { // -- Blocks until the client sends a message. bytesRead = ClientStream.Read(Buffer, 0, Buffer.Length); } catch { break; /* Socket Exception*/ } // -- Did the client disconnect? if (bytesRead == 0) { break; } // -- Handle buffer here. } // -- Connection. GotConnection = false; // -- Close the client. tcpClient.Close(); } public void SendBuffer(byte[] Buffer) { if (GotConnection) tcpClient.GetStream().Write(Buffer, 0, Buffer.Length); } public void StopClient() { if (tcpClient.Connected) tcpClient.GetStream().Close(); tcpClient.Close(); AllowedRead = false; } }
Client.Close
Client.Dispose
tcpClient.Close
tcpClient.Dispose
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)