Advertisement
Vadorequest

MainPage.cs

Sep 30th, 2013
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Navigation;
  8. using Microsoft.Phone.Controls;
  9. using Microsoft.Phone.Shell;
  10. using SocketsTest.Resources;
  11. using System.Net.Sockets;
  12. using System.Threading;
  13. using System.Diagnostics;
  14. using System.Text;
  15.  
  16. namespace SocketsTest
  17. {
  18.     public partial class MainPage : PhoneApplicationPage
  19.     {
  20.         // Constants
  21.         const int ECHO_PORT = 1337;
  22.         const int QOTD_PORT = 1337;
  23.  
  24.         // Constructor
  25.         public MainPage()
  26.         {
  27.             Debug.WriteLine("Application proccessing...\n");
  28.             InitializeComponent();
  29.             Debug.WriteLine("Application started\n");
  30.         }
  31.         /// <summary>
  32.         /// Handle the btnEcho_Click event by sending text to the echo server
  33.         /// and outputting the response
  34.         /// </summary>
  35.         private void btnEcho_Click(object sender, RoutedEventArgs e)
  36.         {
  37.             // Clear the log
  38.             ClearLog();
  39.  
  40.             // Make sure we can perform this action with valid data
  41.             if (ValidateRemoteHost() && ValidateInput())
  42.             {
  43.                 // Instantiate the SocketClient
  44.                 SocketClient client = new SocketClient();
  45.  
  46.                 // Attempt to connect to the echo server
  47.                 Log(String.Format("Connecting to server '{0}' over port {1} (echo) ...", txtRemoteHost.Text, ECHO_PORT), true);
  48.                 string result = client.Connect(txtRemoteHost.Text, ECHO_PORT);
  49.                 Log(result, false);
  50.  
  51.                 // Attempt to send our message to be echoed to the echo server
  52.                 Log(String.Format("Sending '{0}' to server ...", txtInput.Text), true);
  53.                 result = client.Send(txtInput.Text);
  54.                 Log(result, false);
  55.  
  56.                 // Receive a response from the echo server
  57.                 Log("Requesting Receive ...", true);
  58.                 result = client.Receive();
  59.                 Log(result, false);
  60.  
  61.                 // Close the socket connection explicitly
  62.                 client.Close();
  63.             }
  64.  
  65.         }
  66.  
  67.         /// <summary>
  68.         /// Handle the btnEcho_Click event by receiving text from the Quote of
  69.         /// the Day (QOTD) server and outputting the response
  70.         /// </summary>
  71.         private void btnGetQuote_Click(object sender, RoutedEventArgs e)
  72.         {
  73.             // Clear the log
  74.             ClearLog();
  75.  
  76.             // Make sure we can perform this action with valid data
  77.             if (ValidateRemoteHost())
  78.             {
  79.                 // Instantiate the SocketClient object
  80.                 SocketClient client = new SocketClient();
  81.  
  82.                 // Attempt connection to the Quote of the Day (QOTD) server
  83.                 Log(String.Format("Connecting to server '{0}' over port {1} (Quote of the Day) ...", txtRemoteHost.Text, QOTD_PORT), true);
  84.                 string result = client.Connect(txtRemoteHost.Text, QOTD_PORT);
  85.                 Log(result, false);
  86.  
  87.                 // Note: The QOTD protocol is not expecting data to be sent to it.
  88.                 // So we omit a send call in this example.
  89.  
  90.                 // Receive response from the QOTD server
  91.                 Log("Requesting Receive ...", true);
  92.                 result = client.Receive();
  93.                 Log(result, false);
  94.  
  95.                 // Close the socket conenction explicitly
  96.                 client.Close();
  97.             }
  98.         }
  99.  
  100.         #region UI Validation
  101.         /// <summary>
  102.         /// Validates the txtInput TextBox
  103.         /// </summary>
  104.         /// <returns>True if the txtInput TextBox contains valid data, otherwise
  105.         /// False.
  106.         ///</returns>
  107.         private bool ValidateInput()
  108.         {
  109.             // txtInput must contain some text
  110.             if (String.IsNullOrWhiteSpace(txtInput.Text))
  111.             {
  112.                 MessageBox.Show("Please enter some text to echo");
  113.                 return false;
  114.             }
  115.  
  116.             return true;
  117.         }
  118.  
  119.         /// <summary>
  120.         /// Validates the txtRemoteHost TextBox
  121.         /// </summary>
  122.         /// <returns>True if the txtRemoteHost contains valid data,
  123.         /// otherwise False
  124.         /// </returns>
  125.         private bool ValidateRemoteHost()
  126.         {
  127.             // The txtRemoteHost must contain some text
  128.             if (String.IsNullOrWhiteSpace(txtRemoteHost.Text))
  129.             {
  130.                 MessageBox.Show("Please enter a host name");
  131.                 return false;
  132.             }
  133.  
  134.             return true;
  135.         }
  136.         #endregion
  137.  
  138.         #region Logging
  139.         /// <summary>
  140.         /// Log text to the txtOutput TextBox
  141.         /// </summary>
  142.         /// <param name="message">The message to write to the txtOutput TextBox</param>
  143.         /// <param name="isOutgoing">True if the message is an outgoing (client to server)
  144.         /// message, False otherwise.
  145.         /// </param>
  146.         /// <remarks>We differentiate between a message from the client and server
  147.         /// by prepending each line  with ">>" and "<<" respectively.</remarks>
  148.         private void Log(string message, bool isOutgoing)
  149.         {
  150.             string direction = (isOutgoing) ? ">> " : "<< ";
  151.             txtOutput.Text += Environment.NewLine + direction + message;
  152.         }
  153.  
  154.         /// <summary>
  155.         /// Clears the txtOutput TextBox
  156.         /// </summary>
  157.         private void ClearLog()
  158.         {
  159.             txtOutput.Text = String.Empty;
  160.         }
  161.         #endregion
  162.  
  163.         private void txtRemoteHost_TextChanged(object sender, TextChangedEventArgs e)
  164.         {
  165.  
  166.         }
  167.  
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement