Advertisement
Vadorequest

MainPage.xaml.cs

Sep 30th, 2013
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.18 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.  
  15. namespace SocketsTest
  16. {
  17.     public partial class MainPage : PhoneApplicationPage
  18.     {
  19.         Socket _socket = null;
  20.         static ManualResetEvent _clientDone = new ManualResetEvent(false);
  21.         public Boolean _isConnected = false;
  22.  
  23.         // Constructor
  24.         public MainPage()
  25.         {
  26.             Debug.WriteLine("Application proccessing...\n");
  27.             InitializeComponent();
  28.             Debug.WriteLine("Application started\n");
  29.             // Sample code to localize the ApplicationBar
  30.             //BuildLocalizedApplicationBar();
  31.         }
  32.  
  33.         private void Button_Click_2(object sender, RoutedEventArgs e)
  34.         {
  35.             Console.WriteLine("Connection proccessing...\n");
  36.             string result = string.Empty;
  37.             string hostName = "137.135.176.144";
  38.             int portNumber = 1337;
  39.  
  40.             DnsEndPoint hostEntry = new DnsEndPoint(hostName, portNumber);
  41.             _socket = new Socket(AddressFamily.InterNetwork,
  42.            SocketType.Stream,
  43.            ProtocolType.Tcp);
  44.  
  45.             SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
  46.             socketEventArg.RemoteEndPoint = hostEntry;
  47.             socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs h)
  48.             {
  49.                 result = h.SocketError.ToString();
  50.                 _clientDone.Set();
  51.             });
  52.             _clientDone.Reset();
  53.             _isConnected = _socket.ConnectAsync(socketEventArg); // Return "true"
  54.             _clientDone.WaitOne(2000);
  55.  
  56.             if (_socket != null && _socket.Connected == true)
  57.             {
  58.                 Debug.WriteLine("Successful connection =D\n");
  59.             }
  60.             else
  61.             {
  62.                 Debug.WriteLine("Connection failed!\n");
  63.             }
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement