/** * Function to handle informing the server of a client disconnecting, closing the socket and quitting the application. */ private void ExitBtn_Click(object sender, EventArgs e) { AppFunctions.SendText("/exit", mClientSocket, mSendData); mClientSocket.Shutdown(SocketShutdown.Both); mClientSocket.Close(); Application.Exit(); }
/** * Function for detecting when the enter key is pressed whilst focused on the sendTxt textbox and sending the data. */ private void SendTxt_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode.Equals(Keys.Enter)) { AppFunctions.SendText(sendTxt.Text, mClientSocket, mSendData); sendTxt.Clear(); e.SuppressKeyPress = true; } }
/** * Function to handle sending data to the server when the send button is clicked. */ private void SendBtn_Click(object sender, EventArgs e) { try { AppFunctions.SendText(sendTxt.Text, mClientSocket, mSendData); sendTxt.Clear(); } catch (Exception) { UpdateControlStates(false); } }
/** * Function for receiving data from the server and displaying the data received in textbox. */ private void ReceiveCallback(IAsyncResult AR) { try { int received = mClientSocket.EndReceive(AR); if (received == 0) { return; } var message = Encoding.ASCII.GetString(mReceivedData).TrimEnd('\0'); AppFunctions.AppendTextBox(message, receivedTxt); mClientSocket.BeginReceive(mReceivedData, 0, mReceivedData.Length, SocketFlags.None, ReceiveCallback, null); Array.Clear(mReceivedData, 0, mReceivedData.Length); } catch (SocketException) { } catch (ObjectDisposedException) { } }