using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Threading;
using System.Net.Sockets;
using System.Text;
using Microsoft.Phone.Net.NetworkInformation;
namespace SocketExtensions
{
    public partial class MainPage : PhoneApplicationPage
    {
        ManualResetEvent waitOnSend = new ManualResetEvent(false);
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //TCP Socket erstellen
            Socket theSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //Event Args erstellen
            SocketAsyncEventArgs args = new SocketAsyncEventArgs();
            //Server IP
            args.RemoteEndPoint = new DnsEndPoint("192.168.2.113", 17);
            //Completed Behandlung
            args.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object socketsender, SocketAsyncEventArgs ea)
            {
                waitOnSend.Set();
            });
            //Mit dem Server verbinden
            theSocket.ConnectAsync(args);
            //Auf die Verbidnung warten
            waitOnSend.WaitOne();
            //Verbunden
            waitOnSend.Reset();
            if (args.SocketError == SocketError.Success && theSocket.Connected)
            {
                //Den Empfangsbuffer setzen
                args.SetBuffer(new byte[5000], 0, 5000);
                //Empfang anstossen
                theSocket.ReceiveAsync(args);
                //Auf Empfang warten
                waitOnSend.WaitOne();
                //Empfangen
                waitOnSend.Reset();
                if (args.SocketError == SocketError.Success)
                {
                    //Buffer auswerten
                    textBlock1.Text = Encoding.UTF8.GetString(args.Buffer, 0, args.Buffer.Length); ;
                    string txt = textBlock1.Text;
                    NetworkInterfaceInfo info = theSocket.GetCurrentNetworkInterface();
                    txt += "\r\n";
                    txt += "\r\n";
                    txt += info.ToString();
                    txt += ":\r\n";
                    txt += info.InterfaceName;
                    txt += "\r\n";
                    txt += info.Description;
                    txt += "\r\n";
                    txt += info.Bandwidth.ToString();
                    txt += "\r\n";
                    txt += info.Characteristics.ToString();
                    txt += "\r\n";
                    txt += info.InterfaceType.ToString();
                    txt += "\r\n";
                    txt += info.InterfaceSubtype.ToString();
                    txt += "\r\n";
                    txt += info.InterfaceState.ToString();
                    txt += "\r\n";

                    textBlock1.Text = txt;
                }
                else
                {
                    textBlock1.Text = args.LastOperation.ToString() + ": " + args.SocketError.ToString();
                }
            }
            else
            {
                textBlock1.Text = args.LastOperation.ToString() + ": " + args.SocketError.ToString();
            }
            //Trennen
            if (theSocket.Connected)
                theSocket.Shutdown(SocketShutdown.Both);
            theSocket.Close();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            //TCP Socket erstellen
            Socket theSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //Präferenz auf Mobilfunk/GSM
            theSocket.SetNetworkPreference(NetworkSelectionCharacteristics.Cellular);
            //Event Args erstellen
            SocketAsyncEventArgs args = new SocketAsyncEventArgs();
            //Server IP
            args.RemoteEndPoint = new DnsEndPoint("192.168.2.113", 17);
            //Completed Behandlung
            args.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object socketsender, SocketAsyncEventArgs ea)
            {
                waitOnSend.Set();
            });
            //Mit dem Server verbinden
            theSocket.ConnectAsync(args);
            //Auf die Verbidnung warten
            waitOnSend.WaitOne();
            //Verbunden
            waitOnSend.Reset();
            if (args.SocketError == SocketError.Success && theSocket.Connected)
            {
                //Den Empfangsbuffer setzen
                args.SetBuffer(new byte[5000], 0, 5000);
                //Empfang anstossen
                theSocket.ReceiveAsync(args);
                //Auf Empfang warten
                waitOnSend.WaitOne();
                //Empfangen
                waitOnSend.Reset();
                if (args.SocketError == SocketError.Success)
                {
                    //Buffer auswerten
                    textBlock1.Text = Encoding.UTF8.GetString(args.Buffer, 0, args.Buffer.Length);
                }
                else
                {
                    textBlock1.Text = args.LastOperation.ToString() + ": " + args.SocketError.ToString();
                }
            }
            else
            {
                textBlock1.Text = args.LastOperation.ToString() + ": " + args.SocketError.ToString();
            }
            //Trennen
            if (theSocket.Connected)
                theSocket.Shutdown(SocketShutdown.Both);
            theSocket.Close();
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            //TCP Socket erstellen
            Socket theSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //Zwingen per Mobilfunk/GSM
            theSocket.SetNetworkRequirement(NetworkSelectionCharacteristics.Cellular);
            //Event Args erstellen
            SocketAsyncEventArgs args = new SocketAsyncEventArgs();
            //Server IP
            args.RemoteEndPoint = new DnsEndPoint("192.168.2.113", 17);
            //Completed Behandlung
            args.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object socketsender, SocketAsyncEventArgs ea)
            {
                waitOnSend.Set();
            });
            //Mit dem Server verbinden
            theSocket.ConnectAsync(args);
            //Auf die Verbidnung warten
            waitOnSend.WaitOne();
            //Verbunden
            waitOnSend.Reset();
            if (args.SocketError == SocketError.Success && theSocket.Connected)
            {
                //Den Empfangsbuffer setzen
                args.SetBuffer(new byte[5000], 0, 5000);
                //Empfang anstossen
                theSocket.ReceiveAsync(args);
                //Auf Empfang warten
                waitOnSend.WaitOne();
                //Empfangen
                waitOnSend.Reset();
                if (args.SocketError == SocketError.Success)
                {
                    //Buffer auswerten
                    textBlock1.Text = Encoding.UTF8.GetString(args.Buffer, 0, args.Buffer.Length);
                }
                else
                {
                    textBlock1.Text = args.LastOperation.ToString() + ": " + args.SocketError.ToString();
                }
            }
            else
            {
                textBlock1.Text = args.LastOperation.ToString() + ": " + args.SocketError.ToString();
            }
            //Trennen
            if (theSocket.Connected)
                theSocket.Shutdown(SocketShutdown.Both);
            theSocket.Close();
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            //TCP Socket erstellen
            Socket theSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //An Desktop Paththrouh Interface binden
            NetworkInterfaceList list = new NetworkInterfaceList();
            foreach (NetworkInterfaceInfo info in list)
            {
                if(info.InterfaceType == NetworkInterfaceType.Ethernet && info.InterfaceSubtype == NetworkInterfaceSubType.Desktop_PassThru)
                //if (info.InterfaceType == NetworkInterfaceType.MobileBroadbandGsm || info.InterfaceType == NetworkInterfaceType.MobileBroadbandCdma)
                {
                    theSocket.AssociateToNetworkInterface(info);
                    break;
                }
            }
            //Event Args erstellen
            SocketAsyncEventArgs args = new SocketAsyncEventArgs();
            //Server IP
            args.RemoteEndPoint = new DnsEndPoint("192.168.2.113", 17);
            //Completed Behandlung
            args.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object socketsender, SocketAsyncEventArgs ea)
            {
                waitOnSend.Set();
            });
            //Mit dem Server verbinden
            theSocket.ConnectAsync(args);
            //Auf die Verbidnung warten
            waitOnSend.WaitOne();
            //Verbunden
            waitOnSend.Reset();
            if (args.SocketError == SocketError.Success && theSocket.Connected)
            {
                //Den Empfangsbuffer setzen
                args.SetBuffer(new byte[5000], 0, 5000);
                //Empfang anstossen
                theSocket.ReceiveAsync(args);
                //Auf Empfang warten
                waitOnSend.WaitOne();
                //Empfangen
                waitOnSend.Reset();
                if (args.SocketError == SocketError.Success)
                {
                    //Buffer auswerten
                    textBlock1.Text = Encoding.UTF8.GetString(args.Buffer, 0, args.Buffer.Length);
                }
                else
                {
                    textBlock1.Text = args.LastOperation.ToString() + ": " + args.SocketError.ToString();
                }
            }
            else
            {
                textBlock1.Text = args.LastOperation.ToString() + ": " + args.SocketError.ToString();
            }
            //Trennen
            if (theSocket.Connected)
                theSocket.Shutdown(SocketShutdown.Both);
            theSocket.Close();
        }
    }
}