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.Net.Sockets;
using System.Threading;
using System.Text;

namespace SimpleTCPSvc
{
    public partial class MainPage : PhoneApplicationPage
    {
        ManualResetEvent waitOnSend = new ManualResetEvent(false);

        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void start_Click(object sender, System.EventArgs e)
        {
            int iPivot = myPivot.SelectedIndex;
            switch (iPivot)
            {
                case 0:
                    DoQOTD();
                    break;
                case 1:
                    DoDaytime();
                    break;
                case 2:
                    DoChargen();
                    break;
                case 3:
                    DoEcho();
                    break;
            }
        }

        private void DoQOTD()
        {
            //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 sender, SocketAsyncEventArgs e)
                {
                    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
                    txtQOTD.Text = Encoding.UTF8.GetString(args.Buffer, 0, args.Buffer.Length);
                }
                else
                {
                    txtQOTD.Text = args.LastOperation.ToString() + ": " + args.SocketError.ToString();
                }
            }
            else
            {
                txtQOTD.Text = args.LastOperation.ToString() + ": " + args.SocketError.ToString();
            }
            //Trennen
            if(theSocket.Connected)
                theSocket.Shutdown(SocketShutdown.Both);
            theSocket.Close();
        }

        private void DoDaytime()
        {
            //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", 13);
            //Completed Behandlung
            args.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object sender, SocketAsyncEventArgs e)
            {
                waitOnSend.Set();
            });
            //Mit dem Server verbinden
            theSocket.ConnectAsync(args);
            //Auf die Verbidnung warten
            waitOnSend.WaitOne();
            //Verbunden
            waitOnSend.Reset();
            if (args.SocketError == SocketError.Success)
            {
                //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
                    txtDaytime.Text = Encoding.UTF8.GetString(args.Buffer, 0, args.Buffer.Length);
                }
                else
                {
                    txtDaytime.Text = args.LastOperation.ToString() + ": " + args.SocketError.ToString();
                }
            }
            else
            {
                txtDaytime.Text = args.LastOperation.ToString() + ": " + args.SocketError.ToString();
            }
            //Trennen
            if (theSocket.Connected)
                theSocket.Shutdown(SocketShutdown.Both);
            theSocket.Close();
        }

        private void DoChargen()
        {
            //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", 19);
            //Completed Behandlung
            args.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object sender, SocketAsyncEventArgs e)
            {
                waitOnSend.Set();
            });
            //Mit dem Server verbinden
            theSocket.ConnectAsync(args);
            //Auf die Verbidnung warten
            waitOnSend.WaitOne();
            //Verbunden
            waitOnSend.Reset();
            if (args.SocketError == SocketError.Success)
            {
                //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
                    txtChargen.Text = Encoding.UTF8.GetString(args.Buffer, 0, args.Buffer.Length);
                }
                else
                {
                    txtChargen.Text = args.LastOperation.ToString() + ": " + args.SocketError.ToString();
                }
            }
            else
            {
                txtChargen.Text = args.LastOperation.ToString() + ": " + args.SocketError.ToString();
            }
            //Trennen
            if (theSocket.Connected)
                theSocket.Shutdown(SocketShutdown.Both);
            theSocket.Close();
        }

        private void DoEcho()
        {
            //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", 7);
            //Completed Behandlung
            args.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object sender, SocketAsyncEventArgs e)
            {
                waitOnSend.Set();
            });
            //Mit dem Server verbinden
            theSocket.ConnectAsync(args);
            //Auf die Verbidnung warten
            waitOnSend.WaitOne();
            //Verbunden
            waitOnSend.Reset();
            //Sendebuffer erstellen
            byte[] buffer = Encoding.UTF8.GetBytes(strEcho.Text);
            //Buffer kopieren
            args.SetBuffer(buffer,0,buffer.Length);
            //Versenden
            theSocket.SendAsync(args);
            //Auf Versand warten
            waitOnSend.WaitOne();
            //Versendet
            waitOnSend.Reset();
            //Den Empfangsbuffer setzen
            args.SetBuffer(new byte[5000], 0, 5000);
            //Empfang anstossen
            theSocket.ReceiveAsync(args);
            //Auf Empfang warten
            waitOnSend.WaitOne();
            //Empfangen
            waitOnSend.Reset();
            //Buffer auswerten
            txtEcho.Text = Encoding.UTF8.GetString(args.Buffer, 0, args.Buffer.Length);
            //Trennen
            if (theSocket.Connected)
                theSocket.Shutdown(SocketShutdown.Both);
            theSocket.Close();
        }
    }
}