Created
November 30, 2013 14:20
-
-
Save benjojo/7719676 to your computer and use it in GitHub Desktop.
Why the hell is this so hard? What do you want from me MS? My soul?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net; | |
using System.Windows; | |
using System.Windows.Controls; | |
using System.Windows.Navigation; | |
using Microsoft.Phone.Controls; | |
using Microsoft.Phone.Shell; | |
using WtfUDP.Resources; | |
using System.Net.Sockets; | |
using System.Threading; | |
using System.Text; | |
namespace WtfUDP | |
{ | |
public partial class MainPage : PhoneApplicationPage | |
{ | |
// Constructor | |
public MainPage() | |
{ | |
InitializeComponent(); | |
// Sample code to localize the ApplicationBar | |
//BuildLocalizedApplicationBar(); | |
} | |
private void Button_Click_1(object sender, RoutedEventArgs e) | |
{ | |
Socket _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); | |
// Attempt to send our message to be echoed to the echo server | |
string result = Send("a", 11111, "192.168.0.1",_socket); | |
// Close the socket connection explicitly | |
_socket.Close(); | |
} | |
static ManualResetEvent _clientDone = new ManualResetEvent(false); | |
public string Send(string serverName, int portNumber, string data, Socket _socket) | |
{ | |
string response = "Operation Timeout"; | |
// We are re-using the _socket object that was initialized in the Connect method | |
if (_socket != null) | |
{ | |
// Create SocketAsyncEventArgs context object | |
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs(); | |
// Set properties on context object | |
socketEventArg.RemoteEndPoint = new DnsEndPoint(serverName, portNumber); | |
// Inline event handler for the Completed event. | |
// Note: This event handler was implemented inline in order to make this method self-contained. | |
socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e) | |
{ | |
response = e.SocketError.ToString(); | |
// Unblock the UI thread | |
_clientDone.Set(); | |
}); | |
// Add the data to be sent into the buffer | |
byte[] payload = Encoding.UTF8.GetBytes(data); | |
socketEventArg.SetBuffer(payload, 0, payload.Length); | |
// Sets the state of the event to nonsignaled, causing threads to block | |
_clientDone.Reset(); | |
// Make an asynchronous Send request over the socket | |
_socket.SendToAsync(socketEventArg); | |
// Block the UI thread for a maximum of TIMEOUT_MILLISECONDS milliseconds. | |
// If no response comes back within this time then proceed | |
_clientDone.WaitOne(1000); | |
} | |
else | |
{ | |
response = "Socket is not initialized"; | |
} | |
return response; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment