private void deleteClick(object sender, EventArgs e) // remove client from list, this function is triggered from "DELETE" button { if (connected) { connected = false; AsynchronousServer.ServerSetControlPropertyThreadSafe(AsynchronousServer.console, "Text", AsynchronousServer.console.Text + "Client " + index + " (" + key + "): Lost connection" + " (" + index + ")" + "\n"); AsynchronousServer.ServerSetControlPropertyThreadSafe(AsynchronousServer.console, "Text", AsynchronousServer.console.Text + "Client " + index + " (" + key + "): Deleted" + " (" + index + ")" + "\n"); AsynchronousServer.clients.Remove(this.key); client.Shutdown(SocketShutdown.Both); client.Close(); TestFormCotrolHelper.ControlInvike(AsynchronousServer.list, () => AsynchronousServer.list.Controls.Remove(itemPanel)); } else { AsynchronousServer.clients.Remove(this.key); TestFormCotrolHelper.ControlInvike(AsynchronousServer.list, () => AsynchronousServer.list.Controls.Remove(itemPanel)); AsynchronousServer.ServerSetControlPropertyThreadSafe(AsynchronousServer.console, "Text", AsynchronousServer.console.Text + "Client " + index + " (" + key + "): Deleted" + " (" + index + ")" + "\n"); } AsynchronousServer.StoreData(); Console.WriteLine("My Index: " + this.index); foreach (KeyValuePair <string, Client> item in AsynchronousServer.clients) //move all items below this with 25 units upper, for not leaving blank space in list { Console.WriteLine("item: " + item.Value.index); if (item.Value.index > this.index) { item.Value.itemPanel.Location = new System.Drawing.Point(0, item.Value.itemPanel.Location.Y - 25); } } }
public Client(string key, Socket client, int count) { this.key = key; this.client = client; this.index = count; FillList(); if (client != null) //if client is created from server.accept() result, then start listen thread, otherwise means client is created without socket connection, from list of all connected/disconnected clients { AsynchronousServer.ServerSetControlPropertyThreadSafe(AsynchronousServer.console, "Text", AsynchronousServer.console.Text + "New client connected: " + client.RemoteEndPoint.ToString() + " (" + index + ")" + "\n"); instanceClient(); } }
public void instanceClient() { this.connected = true; itemStatus.ImageLocation = "green.bmp"; //set connected image in clients list chatThread = new Thread(() => { Thread.CurrentThread.IsBackground = true; while (connected) { try { int receivedBytes = client.Receive(bytes); //get message from connected client in bytes buffer if (receivedBytes == 0) //if not bytes received means client lost connection { AsynchronousServer.ServerSetControlPropertyThreadSafe(AsynchronousServer.console, "Text", AsynchronousServer.console.Text + "Client " + index + " (" + key + "): Lost connection" + " (" + index + ")" + "\n"); client.Shutdown(SocketShutdown.Both); client.Close(); connected = false; itemStatus.ImageLocation = "red.bmp";//set disconnected image in clients list } else //put buffer bytes in string { response = Encoding.ASCII.GetString(bytes, 0, receivedBytes); AsynchronousServer.ServerSetControlPropertyThreadSafe(AsynchronousServer.console, "Text", AsynchronousServer.console.Text + "Client " + index + " (" + key + "): " + response); Console.WriteLine("Console height: " + AsynchronousServer.console.Size.Height); AsynchronousServer.Send(response, index); } } catch (System.Net.Sockets.SocketException e) { connected = false; connected = false; itemStatus.ImageLocation = "red.bmp";//set disconnected image in clients list } Thread.Sleep(500); } }); chatThread.Start(); }
//clear server console private void button4_Click(object sender, EventArgs e) { AsynchronousServer.ServerSetControlPropertyThreadSafe(this.serverConsole, "Text", ""); }