private void ReceiveCallback(IAsyncResult AR) { try { // Socket exception will raise here when client closes, as this sample does not // demonstrate graceful disconnects for the sake of simplicity. int received = clientSocket.EndReceive(AR); if (received == 0) { return; } // The received data is deserialized in the PersonPackage ctor. Package = new QuestionPackage(buffer); GetPackage(Package); ToggleControlState(true); // Start receiving data again. clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveCallback, null); } // Avoid catching all exceptions handling in cases like these. catch (SocketException ex) { ShowErrorDialog(ex.Message); } catch (ObjectDisposedException ex) { ShowErrorDialog(ex.Message); } }
///------------------------------------------------------------------------------------------------- /// <summary> Async callback, called on completion of receive callback. </summary> /// /// <remarks> Jaege, 17/09/2018. </remarks> /// /// <param name="AR"> The result of the asynchronous operation. </param> ///------------------------------------------------------------------------------------------------- private void ReceiveCallback(IAsyncResult AR) { try { int received = clientSocket.EndReceive(AR); if (received == 0) { return; } // The received data is deserialized in the PersonPackage ctor. Package = new QuestionPackage(buffer); Package.Value = 1; MessageBox.Show(Package.Value.ToString()); GetPackage(Package); // Start receiving data again. clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveCallback, null); } // Avoid catching all exceptions handling in cases like these. catch (SocketException ex) { ShowErrorDialog(ex.Message); } catch (ObjectDisposedException ex) { ShowErrorDialog(ex.Message); } }
private void GetPackage(QuestionPackage package) { question = package.QuestionNo1.ToString() + " " + package.QuestionOperator.ToString() + " " + package.QuestionNo2.ToString() + " " + "="; instructorQuestion1 = Convert.ToInt32(package.QuestionNo1); instructorOperator = package.QuestionOperator.ToString(); instructorQuestion2 = Convert.ToInt32(package.QuestionNo2); instructorAnswer = Convert.ToInt32(package.QuestionAnswer); }
///------------------------------------------------------------------------------------------------- /// <summary> Gets a package. </summary> /// /// <remarks> Jaege, 17/09/2018. </remarks> /// /// <param name="package"> The package. </param> ///------------------------------------------------------------------------------------------------- private void GetPackage(QuestionPackage package) { receivedInstructorFirstNumber = Convert.ToInt32(package.QuestionNo1); receivedaOperator = package.QuestionOperator.ToString(); receivedInstructorSecondNumber = Convert.ToInt32(package.QuestionNo2); receivedInstructorAnswer = Convert.ToInt32(package.QuestionAnswer); question = package.QuestionNo1.ToString() + " " + package.QuestionOperator.ToString() + " " + package.QuestionNo2.ToString() + " " + "="; Value = package.Value; if (receivedaOperator != null) { //ToggleControlState(true); } }
public void SendQuestion() { try { // Serialize the textBoxes text before sending. QuestionPackage package = new QuestionPackage(instructorQuestion1, instructorOperator, instructorQuestion2, instructorAnswer, Value); byte[] buffer = package.ToByteArray(); clientSocket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, SendCallback, null); ToggleControlState(false); } catch (SocketException ex) { ShowErrorDialog(ex.Message); ToggleControlState(false); } catch (ObjectDisposedException ex) { ShowErrorDialog(ex.Message); ToggleControlState(false); } }