Implementing Sockets in Silverlight Applications : Sockets « Communication « Silverlight






Implementing Sockets in Silverlight Applications

Implementing Sockets in Silverlight Applications
 

<UserControl x:Class='SilverlightApplication3.MainPage'
    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' 
    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
    xmlns:d='http://schemas.microsoft.com/expression/blend/2008' 
    xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006' 
    mc:Ignorable='d' 
    d:DesignWidth='640' 
    d:DesignHeight='480'>
    
    <Grid x:Name="LayoutRoot" Background="White">
           <Button x:Name="timeBtn" Content="Server Time"/>
           <Button x:Name="dateBtn" Content="Server Date"/>
           <Button x:Name="fullBtn" Content="Server Full"/>
           <TextBlock Text="Server Date / Time"/>
           <TextBlock x:Name="serverText"/>
    </Grid>
</UserControl>
//File:Page.xaml.cs


using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;

using System.Net;
using System.Net.Sockets;
using System.Text;

namespace SilverlightApplication3
{
    public partial class MainPage : UserControl
    {
        string timeDateFormat;
        public MainPage()
        {
            InitializeComponent();
            timeDateFormat = "Full";

             timeBtn.Click += new RoutedEventHandler(doGetTime);
             dateBtn.Click += new RoutedEventHandler(doGetDate);
             fullBtn.Click += new RoutedEventHandler(doGetFull);
         }
         void doGetTime(object sender, RoutedEventArgs e)
         {
             timeDateFormat = "Time";
             doGetData();
         }
         void doGetDate(object sender, RoutedEventArgs e)
         {
             timeDateFormat = "Date";
             doGetData();
         }
         void doGetFull(object sender, RoutedEventArgs e)
         {
             timeDateFormat = "Full";
             doGetData();
         }
         void doGetData()
         {
            DnsEndPoint ePoint = new DnsEndPoint(Application.Current.Host.Source.DnsSafeHost,4510);
            Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);

            SocketAsyncEventArgs sArgs = new SocketAsyncEventArgs();
            sArgs.UserToken = socket;
            sArgs.RemoteEndPoint = ePoint;
            sArgs.Completed += new EventHandler<SocketAsyncEventArgs>(OnConnectCompleted);
            socket.ConnectAsync(sArgs);
         }

        private void OnConnectCompleted(object sender,SocketAsyncEventArgs e)
         {
            Byte[] bytes = Encoding.UTF8.GetBytes(timeDateFormat);
            e.SetBuffer(bytes, 0, bytes.Length);
            e.Completed -= new EventHandler<SocketAsyncEventArgs>(OnConnectCompleted);
            e.Completed += new EventHandler<SocketAsyncEventArgs>(OnSocketSendCompleted);
            Socket socket = (Socket)e.UserToken;
            socket.SendAsync(e);
         }
        private void OnSocketSendCompleted(object sender, SocketAsyncEventArgs e)
         {
            byte[] response = new byte[1024];
            e.SetBuffer(response, 0, response.Length);
            e.Completed -= new EventHandler<SocketAsyncEventArgs>(OnSocketSendCompleted);
            e.Completed += new EventHandler<SocketAsyncEventArgs>(OnSocketReceive);
            Socket socket = (Socket)e.UserToken;
            socket.ReceiveAsync(e);
         }
         private void OnSocketReceive(object sender, SocketAsyncEventArgs e)
         {
            string data = Encoding.UTF8.GetString(e.Buffer, e.Offset,e.BytesTransferred);
            this.Dispatcher.BeginInvoke(delegate {serverText.Text = data;});
            Socket socket = (Socket)e.UserToken;
            socket.ReceiveAsync(e);
         }
    }
}

   
  








Related examples in the same category