Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8371133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:05:20+00:00 2026-06-09T14:05:20+00:00

I have TCP Socket server and would like to make asynchronous Silverlight client for

  • 0

I have TCP Socket server and would like to make asynchronous Silverlight client for it, but I cannot find any useful information on that. To be exact, I need three steps: connect, send and receive.

No need for it to be very detailed, just the idea how to build it to connect to the server at least.

By the way, I use Silverlight 5.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:05:21+00:00Added an answer on June 9, 2026 at 2:05 pm

    Thank you very much, Karel Frajtak for the link to video about sockets in Silverlight!
    Everything is shown and explained in that video tutorial, so I recommend everyone who needs sockets in Silverlight to watch it!
    Here’s the solution that I needed, with explanations what does what and my comments:

    I assume that all the connection actions will be handled by separate class SocketConnection:

    public class MessageEventArgs : EventArgs
    {
        public string Message { get; set; }
    }
    public class SocketConnection
    {
        /// <summary>
        /// Event handler shot when message is received
        /// </summary>
        public event EventHandler<MessageEventArgs> MessageReceived;
    
        /// <summary>
        /// Socket used for connection to the server, sending and receiving data
        /// </summary>
        Socket socket;
    
        /// <summary>
        /// Default buffer size that should be used with the same value in both server and client
        /// </summary>
        int bufferSize = 128;
    
        /// <summary>
        /// Buffer used to store bytes received
        /// </summary>
        byte[] buffer;
    
        /// <summary>
        /// Bytes received in current receiving operation
        /// </summary>
        int bytesReceived;
    
        public SocketConnection(string host = "localhost", int port = 4502)
        {
            // Initializing buffer for receiving data
            buffer = new byte[bufferSize];
            // Initializing socket to connect to the server with default parameters
            // *** If you need IPv6, set the AddressFamily.InterNetworkV6 ***
            // *** Silverlight supports only Stream or Unknown socket types (Silverlight 5) ***
            // *** The only defined protocol supported by Silverlight is TCP.
            //     Others can be only Unknown or Unspecified (Silverlight 5)***
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    
            // Socket args that are needed for connection
            SocketAsyncEventArgs args = new SocketAsyncEventArgs()
            {
                // Server IP and port
                RemoteEndPoint = new DnsEndPoint(host, port),
                // If policy server is hosted as TCP socket, this has to be set to SocketClientAccessPolicyProtocol.Tcp
                // If policy is stored in HTTP server, this has to be set SocketClientAccessPolicyProtocol.Http
                SocketClientAccessPolicyProtocol = SocketClientAccessPolicyProtocol.Tcp
            };
            // Set the event handler for completed connection (nomatter if it is successful or not)
            args.Completed += OnConnected;
            // Start connecting to the server asynchronously
            socket.ConnectAsync(args);
        }
    
        /// <summary>
        /// Even handler shot when socket connection is completed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OnConnected(object sender, SocketAsyncEventArgs e)
        {
            // If the connection is not successful, set socket object to null
            if (e.SocketError != SocketError.Success)
            {
                if (e.SocketError == SocketError.AccessDenied)
                {
                    // Policy server is not running or cannot be reached
                    throw new SocketException((int)SocketError.AccessDenied);
                }
                socket = null;
            }
            // Begin receiving data otherwise
            else
            {
                BeginRead();
            }
        }
    
        /// <summary>
        /// Method for receiving data from the server
        /// </summary>
        private void BeginRead()
        {
            // Receive data only if socket is connected
            if (socket != null && socket.Connected)
            {
                SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                // Store the received buffer in a class variable
                args.SetBuffer(buffer, bytesReceived, buffer.Length - bytesReceived);
                // Set the event handler for received data
                args.Completed += OnReceived;
    
                // Start receiving data asynchronously
                socket.ReceiveAsync(args);
            }
        }
    
        /// <summary>
        /// Event handler shot when data is received from the server
        /// </summary>
        void OnReceived(object sender, SocketAsyncEventArgs e)
        {
            // Make sure that receiving was successful
            if (e.SocketError == SocketError.Success)
            {
                // Increase the count of received bytes in the current receiving operation
                bytesReceived += e.BytesTransferred;
            }
            // If the receiving was unsuccessful, throw an exception
            else
            {
                throw new SocketException();
            }
            // Check if the buffer is already full
            if (bytesReceived == buffer.Length)
            {
                // If the buffer is full, decode the string received from bytes
                // *** This should be your object deserialization, if you use anything but string ***
                string text = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
    
                // If the event was set from somewhere, shoot it
                // *** In most cases event is set from UI thread to handle the
                //     received string or object and show the result to user ***
                if (MessageReceived != null)
                {
                    // Shoot the event, if it's set
                    MessageReceived(this, new MessageEventArgs()
                    {
                        Message = text
                    });
                }
    
                // Set the bytes received count to 0, for other data receiving event to fill the buffer from begining
                bytesReceived = 0;
            }
            // Begin the data receiving again
            BeginRead();
        }
    
        /// <summary>
        /// Sample method to send data to the server
        /// </summary>
        /// <param name="text">Text you would like the server to receive</param>
        public void SendText(string text)
        {
            // Check if the socket is connected to the server
            if (socket != null && socket.Connected)
            {
                // Encode the string to be sent to bytes
                // *** This is where your object serialization should be done ***
                byte[] buffer = Encoding.UTF8.GetBytes(text);
                // Check if the buffer is not empty
                if (buffer.Length != 0)
                {
                    SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                    // Set the buffer to be sent
                    args.SetBuffer(buffer, 0, buffer.Length);
    
                    // Start sending buffer to the server asynchronously
                    socket.SendAsync(args);
                }
            }
        }
    }
    

    For socket connection in Silverlight to be successful, there HAS to be

    • cross-domain policy socket server, hosted on port 943

    OR

    • cross-domain policy file hosted on HTTP server (port 80).

    If you choose to use socket server, here’s a working sample source code:

    Sample taken from here and it says there that

    the policy server <…> must be run on port 943

    and says nothing about possibility to host it on HTTP server, just because the article is too old.

    using System;
    using System.IO;
    using System.Net;
    using System.Net.Sockets;
    
    namespace PolicyServer
    {
        // Encapsulate and manage state for a single connection from a client
        class PolicyConnection
        {
            private Socket _connection;
            private byte[] _buffer; // buffer to receive the request from the client       
            private int _received;
            private byte[] _policy; // the policy to return to the client
    
            // the request that we're expecting from the client
            private static string _policyRequestString = "<policy-file-request/>";
    
            public PolicyConnection(Socket client, byte[] policy)
            {
                _connection = client;
                _policy = policy;
    
                _buffer = new byte[_policyRequestString.Length];
                _received = 0;
    
                try
                {
                    // receive the request from the client
                    _connection.BeginReceive(_buffer, 0, _policyRequestString.Length, SocketFlags.None,
                        new AsyncCallback(OnReceive), null);
                }
                catch (SocketException)
                {
                    _connection.Close();
                }
            }
    
            // Called when we receive data from the client
            private void OnReceive(IAsyncResult res)
            {
                try
                {
                    _received += _connection.EndReceive(res);
    
                    // if we haven't gotten enough for a full request yet, receive again
                    if (_received < _policyRequestString.Length)
                    {
                        _connection.BeginReceive(_buffer, _received, _policyRequestString.Length - _received,
                            SocketFlags.None, new AsyncCallback(OnReceive), null);
                        return;
                    }
    
                    // make sure the request is valid
                    string request = System.Text.Encoding.UTF8.GetString(_buffer, 0, _received);
                    if (StringComparer.InvariantCultureIgnoreCase.Compare(request, _policyRequestString) != 0)
                    {
                        _connection.Close();
                        return;
                    }
    
                    // send the policy
                    Console.Write("Sending policy...\n");
                    _connection.BeginSend(_policy, 0, _policy.Length, SocketFlags.None,
                        new AsyncCallback(OnSend), null);
                }
                catch (SocketException)
                {
                    _connection.Close();
                }
            }
    
            // called after sending the policy to the client; close the connection.
            public void OnSend(IAsyncResult res)
            {
                try
                {
                    _connection.EndSend(res);
                }
                finally
                {
                    _connection.Close();
                }
            }
        }
    
        // Listens for connections on port 943 and dispatches requests to a PolicyConnection
        class PolicyServer
        {
            private Socket _listener;
            private byte[] _policy;
    
            // pass in the path of an XML file containing the socket policy
            public PolicyServer(string policyFilePath)
            {
    
                // Load the policy file
                FileStream policyStream = new FileStream(policyFilePath, FileMode.Open);
    
                _policy = new byte[policyStream.Length];
                policyStream.Read(_policy, 0, _policy.Length);
                policyStream.Close();
    
    
                // Create the Listening Socket
                _listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
                    ProtocolType.Tcp);
    
                _listener.SetSocketOption(SocketOptionLevel.Tcp, (SocketOptionName)
                    SocketOptionName.NoDelay, 0);
    
                _listener.Bind(new IPEndPoint(IPAddress.Any, 943));
                _listener.Listen(10);
    
                _listener.BeginAccept(new AsyncCallback(OnConnection), null);
            }
    
            // Called when we receive a connection from a client
            public void OnConnection(IAsyncResult res)
            {
                Socket client = null;
    
                try
                {
                    client = _listener.EndAccept(res);
                }
                catch (SocketException)
                {
                    return;
                }
    
                // handle this policy request with a PolicyConnection
                PolicyConnection pc = new PolicyConnection(client, _policy);
    
                // look for more connections
                _listener.BeginAccept(new AsyncCallback(OnConnection), null);
            }
    
            public void Close()
            {
                _listener.Close();
            }
        }
    
        public class Program
        {
            static void Main()
            {
                Console.Write("Starting...\n");
                PolicyServer ps =
                    new PolicyServer(@".\clientaccesspolicy.xml");
                System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
            }
        }
    }
    

    That’s about it. Run the cross-domain policy server first and your client only then, to make sure the connection will succeed.

    If you need an asynchronous WPF socket server sample, just ask and give me link to the question! I have one built with async-await keywords, to be as simple as possible.

    Good luck in everything!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a TCP socket client receiving messages (data) from a server. messages are
I have a NodeJS TCP socket server running and I'd like to add support
I have written a TCP server using the Socket class's asynchronous/IOCP methods, BeginSend()/BeginRead()/etc. I
I currently have an asynchronous TCP/IP socket C# WinForms application that is the Server
I have problem with receiving an image over TCP socket [.net 4.0] Server: Socket
I have developed a Java server using Eclipse that accepts TCP socket connection from
I have a server-client application [TCP sockets, .NET 4.0].. the application about : do
I have a tcp socket sending three lines like this out2.println(message1\n); out2.println(message2\n); out2.println(message3\n); and
Assume that i have created a client TCP Socket in Activity A. I want
My client application uses a boost::asio::ip::tcp::socket to connect to a remote server. If the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.