Socket Application Example in Silverlight 2 (Beta)

Source: Internet
Author: User
Note: Code The example is taken from the 50 new Silverlight 2 beta 1 screencasts video tutorial series.

As a new feature in Silverlight 2, sockets will help us develop a powerful, flexible, and efficient network.Program.
I originally wanted to write an example myself, but I read it online and found that Mike taulty has made a set of tutorials to practice sl2.0.
So I followed the steps in the tutorial and opened the socket in silverlight2.
There is no big difference between sending and writing socket applications. Because we are very busy at ordinary times, I believe we don't have much time to listen to any classes.
So I will list the development steps of this instance here to see"Put an elephant in a refrigerator"A few steps are required :)

Step 1:Create a console server project

Because it is socket development, there must be a sever. Because it is a demonstration, the server listening code is directly used in the example.
The main () function is written. The code snippet is as follows:

Static   Void Main ( String [] ARGs)
{
Console. writeline ( " Demonstration of using Silverlight socket to send information: " );
Console. writeline ( " ========================================================== ============ " );
Console. writeline ( "" );
Socket socket =   New Socket (addressfamily. InterNetwork, sockettype. Stream, protocoltype. TCP );
Socket. BIND ( New Ipendpoint (IPaddress. Any, 4502 ));
Socket. Listen ( 5 );
While ( True )
{
Socket s = Socket. Accept ();
Thread t =   New Thread (() =>
{
Try
{
While ( True )
{
Byte [] Buffer =   New   Byte [ 1024 ];
Int Receive = S. Receive (buffer );
If (Receive ! =   0 )
{
Console. writeline (encoding. Unicode. getchars (buffer, 0 , Receive ));
}
}
}
Finally
{
S. Close ();
}
});

T. Start ();
}
}

This is basically a common class used to develop socket applications. I will not say much about it. I believe that I will be familiar with socket development at a glance.

Step 2:Create a Silverlight Application Project (client)

Put the following XAML code into page. XAML.

< Usercontrol X: Class = "Socketclient. Page"
Xmlns = "Http://schemas.microsoft.com/client/2007"
Xmlns: x = "Http://schemas.microsoft.com/winfx/2006/xaml"
Width = "400" Height = "300" >
< Grid X: Name = "Layoutroot" Background = "White" Showgridlines = "True" >
< Grid. rowdefinitions >
< Rowdefinition />
< Rowdefinition />
</ Grid. rowdefinitions >

< Textbox X: Name = "Txttosend" Grid. Row = "0" />
< Button Grid. Row = "1" Click = "Onsend" Content = "Send" Margin = "20"   />
</ Grid >
</ Usercontrol >



Then, put the following CS code into the page. XAML. CS file:
  Public Partial ClassPage: usercontrol
{

Socket socket;

// Click Event of the send message button
Void Onsend ( Object Sender, eventargs ARGs)
{
Byte [] Bytes = Encoding. Unicode. getbytes (txttosend. Text );
Socket =   New Socket (addressfamily. InterNetwork, sockettype. stream,
Protocoltype. TCP );

Socketasynceventargs socketargs =   New Socketasynceventargs ()
{
Remoteendpoint =   New Dnsendpoint (
Application. Current. Host. Source. dnssafehost, 4502 )
};
Socketargs. Completed + = Onoperationcompleted;
Socketargs. usertoken = Bytes;
Socket. connectasync (socketargs );
}

// Put data into the buffer and send data Asynchronously
Void Onoperationcompleted ( Object Sender, socketasynceventargs E)
{
E. Completed -= Onoperationcompleted;
Byte [] Bytes = ( Byte []) E. usertoken;
Socketasynceventargs sendargs =   New Socketasynceventargs ();
Sendargs. setbuffer (bytes, 0 , Bytes. Length );
Sendargs. Completed + = Onsendcompleted;
Socket. sendasync (sendargs );
}

//Close the current socket link after sending
VoidOnsendcompleted (ObjectSender, socketasynceventargs E)
{
Socket. Close ();
}


}


In this way, the program development is completed. The third step is shown below (for example ):



Well, today's content is here :)

Click here to download the source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.