Click here to Skip to main content
15,895,084 members
Articles / Web Development / ASP.NET

Your First Step to the Silverlight Voice/Video Chatting Client/Server

Rate me:
Please Sign up or sign in to vote.
4.85/5 (44 votes)
15 Dec 2013CPOL5 min read 343.5K   22.6K   188  
What you should to do to create your own Silverlight Voice/Video chatting system
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 System.Net.Sockets;
using System.IO;
using System.Windows.Media.Imaging;

namespace SocketCoder_WebCamReceiver
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }


        #region Global Declarations
        private Socket client_socket;
        private delegate void mydelegate(byte[] buffer);
        private delegate void ShowMessagedelegate(string MSG);
        #endregion Global Declarations
        #region Socket Methods
        void Conncet(string IP_Address)
        {

            client_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs()
            {
                RemoteEndPoint = new IPEndPoint(IPAddress.Parse(IP_Address), 4532)
            };
            socketEventArg.Completed += OnConncetCompleted;
            client_socket.ConnectAsync(socketEventArg);
        }
        void StartReceiving()
        {
            byte[] response = new byte[65536];
            SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
            socketEventArg.Completed += OnReceiveCompleted;
            socketEventArg.SetBuffer(response, 0, response.Length);
            client_socket.ReceiveAsync(socketEventArg);
        }
        void OnConncetCompleted(object sender, SocketAsyncEventArgs e)
        {
            if (e.SocketError == SocketError.Success)
            {
                this.Dispatcher.BeginInvoke(new ShowMessagedelegate(ShowMessageBox), "Connceted Successfully!");
                StartReceiving();
            }
            else
            {
                this.Dispatcher.BeginInvoke(new ShowMessagedelegate(ShowMessageBox), e.SocketError.ToString());
            }

        }
        private void ShowMessageBox(string MSG)
        {
            MessageBox.Show(MSG);
        }
        void OnReceiveCompleted(object sender, SocketAsyncEventArgs e)
        {
            this.Dispatcher.BeginInvoke(new mydelegate(ViewReceivedImage), e.Buffer);
        }
         #endregion Socket Methods
        #region Decoding Methods
        private void ViewReceivedImage(byte[] buffer)
        {
            try
            {
                MemoryStream ms = new MemoryStream(buffer);
                BitmapImage bi = new BitmapImage();
                bi.SetSource(ms);
                MyImage.Source = bi;
                ms.Close();
            }
            catch (Exception) { }
            finally
            {
                StartReceiving();
            }
        }
        #endregion Decoding Methods

        private void ConnectBTN_Click(object sender, RoutedEventArgs e)
        {
            Conncet(IPAddress_TXT.Text);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems Engineer SocketCoder.Com
Jordan Jordan
Fadi Abdelqader
WebSite: http://www.SocketCoder.com
Facebook Group: http://www.facebook.com/groups/socketcoder/
Facebook Page: http://www.facebook.com/socketcoders/

Last Published Book:
-Professional Network, Distributed Systems & TCP/IP Programming In .NET Framework 1.1 & 2.0 (2006 Release)

Last Published eBook:
- The SocketCoder e-Reference For Network, Distributed Systems And TCP/IP Programming In .NET, Arabic (SocketCoder.Com 2010)

Last Projects:
- Silverlight Based Projects For Web Video Conferencing (WCS,WMS,WCS Plus..)
-(SocketCoder) RTP Multicasting VOIP Library
-(SocketCoder) Remote Controlling Systems

Last SocketCoder RSS: http://www.socketcoder.com/feed/

http://www.socketcoder.com

Comments and Discussions