Dycom-Windows Phone 7.1 Network Communication

Source: Internet
Author: User

After receiving a notification from WP7 mango SDK, we will immediately develop dycom for WP mango. After all, this has been waiting for a long time. The WP7 SDK has never opened socket communication interfaces (including s60 Silverlight ).

After two minutes of work. Dycom for Silverlight 4 is successfully moved to wp7.1. This filling indicates that silverlihgt for wp7.1 and Silverlight 4 have a perfect consistency. In terms of network communication. As a result, is it possible to support wcf.net. TCP and try again later.

Before getting started, I would like to introduce a dycom product. It is also a small advertisement. However, it is not an advertisement for profit. Dycom is a cross-platform network communication suite. Currently, the supported clients include. net, wm5/6, wp7.1, Android, Flash, Java, and xNa. The key point is that dycom decided to adopt the sharing software policy. dycom itself must not be open-source, but it provides a free use policy. You can use dycom to quickly develop your own cross-platform communication applications. If you don't want to pay, you can apply for free authorization at the http://dy2com.com. It is free for life. Billing authorization is the same as free authorization. In other words, whether to pay for the dycom Development Team is based on your own voluntary principle.

Next, I will show you how to quickly develop a wp7.1 instant chat application through dycom. This program is divided into two terminal programs. Server and client programs.

A) on the server side, we use C # To start the server-side business logic and provide services to the client using a console program to connect and broadcast messages to all clients.

B) the client is a wp7.1 application. It is a client program that can connect to the server

C) The functions to be implemented are as follows:

1. Any WP7 mobile phone connected to the server sends a message to the server.

2. The server receives this message and broadcasts it to all clients connected to the server.

With the above two functional requirements, we can complete a simple instant text chat program.

The following is a normal manual drill process:

1. log onto the dycom official website to apply for free authorization, address: http://bbs.dy2com.com/forum.php? MoD = viewthread & tid = 18 & extra =

After applying for a forum account to log on, you can connect to the above address to browse the application box for applying for a free dycom Authorization key:

After entering your username and email address, click "apply for key" and then dycom will send an email containing the dycom key to your mailbox. With this key, you can use dycom for free permanently.

2. Download the demo project file of the dycom server and WP7 client:

A) dycom server example project file: http://www.dy2com.com/a/DYcomxiazai/fuwuqiduanshili/2011/0120/59.html

The above are five dycom examples. We only need to download the "dycom server. net4.0 example.

(B) dycom client download: http://www.dy2com.com/a/DYcomxiazai/kehuduanshili/2011/0121/66.html

On the client, we only need to download WP7:

The download has been completed. Next, debug and run the two examples so that they can communicate with each other:

TIPS: if you have not installed the wp7.1 SDK, click Download and install: Windows Phone 7.1 SDK:Direct download link

A) decompress the two downloaded files and we will get two vs2010 project files. We use two vs2010 programs to open these two project files.

B) start running the server: Debug and run the vs2010of the dycomservervs10sample project. The running status is as follows:

 

C) start running the client: Debug and run the vs2010of the dycomwptestclient project. The running status is as follows:

TIPS: if any vs error occurs during the running, we can "re-compile" the entire client project.

^ After successful running, debugging is complete. Click the input box to enter some text and then send it. Normally, we will receive the text we sent ourselves. In addition, if you have several more clients, you can also receive messages from one client. So far. Our debugging has been successful. Next, let's look at the source code of the project.

3. Let the program become the official version: Remember the dycom product key we just applied. Now we open the dycomserver. CS file of the server project file dycomservervs10sample.

Code modification: String key = "PD key"; // product key. Change it to your own product key.

Change the content of the above key variable to your dycom product key,

For example, string key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // change the product key to your own product key.

In this way, the entire application has become a formal application, and you can deploy it on a formal server. The client does not need to enter the dycom product key.

Through the above drills, we already have a WP7 high-speed network communication source code project. Next, we will try to add the required functions for this application. Through the following drills, you will be able to learn how to use dycom to develop your own network communication applications. All of this will be incredibly simple. In addition, the communication efficiency is quite high. You can also use dycom to develop WP7 online games. ^

4. Add a user connection notification. Requirement: after a new client connects to the server, the server sends a notification to other clients connected to the server.

A) server code writing: we only need to modify the dycomserver. CS on the server,

At the beginning of the file content of dycomserver. CS, we can see the following code:

List <myclient> clients = new list <myclient> ();

In fact, it is a collection of all clients connected to the server. Then we can adjust the set to implement our functions. First, find

// Custom message Operators
Public Enum op
{
Login
}

This Enum is defined. We add an operator. You can create a name or a Chinese name at will. The modification is as follows:

// Custom message Operators
Public Enum op
{
Login,
New User connection
}

Then I add our functional logic to the onconnetevent of the server:

// User connection event
Void server_onconnetevent (dysocket E, bool isconneted)
{
// Client connection prompt
Console. writeline (E. socketargs. acceptsocket. remoteendpoint. tostring () + "connected ");
// Determine whether the client is newly connected (the usertoken of the new user is null)
If (E. socketargs. usertoken = NULL)
{
// Instantiate
VaR MC = new myclient ();
// Set the connection client to the client instance
MC. Sock = E;
// If you want to disconnect a user without any communication within a certain period of time (in seconds)
// MC. setuptimeoutcheck (server, 5 );
// Operation completed. Save the customer instance
E. socketargs. usertoken = MC;

// Send a message to all clients connected to this service
// The operator is op. A new user is connected
// The message content is: "The server notifies you that a new user connection has entered the system"
// Use utf8 Encoding
Server. sendtoall (clients, dywriter. Merge (dywriter. getdybytes (INT) op. New users are connected ),
Dywriter. getdybytes ("the server notifies you that a new user is connected to the system", encoding. utf8 )));

// Add a new client to the client set
Clients. Add (E. socketargs. usertoken as myclient );
}
}

After adding the above Code.

5. The modification has been completed on the server side. Next, let's get the client to receive the message and display it to the user interface:

A) copy the eumu op in the dycomserver. CS file to the client and replace the original eumu op of the client. In other words, change the enum op in mainpage. XAML. Cs in the dycomwptestclient project:

// Custom message Operators
Public Enum op
{
Login,
New User connection
}

B) modify the ondata event of the client, because all messages sent by the server will trigger this event. We only need to explain the message content:

Void ondata (byte [] data)
{
Dyreader READ = new dyreader (data );
Int type;
If (! Read. readint32 (out type ))
{
Return;
}
OP opera = (OP) type;

If (opera = op. login)
{
String date;
If (read. readstring (out date, system. Text. encoding. utf8 ))
{
// Listbox1.items. insert (0, datetime. Now. Subtract (date). totalmilliseconds. tostring ());
Lb_msgshow.items.insert (0, date );
}
}
// Determine whether new users are connected to the operator, which is sent by the server
Else if (opera = op. A new user is connected)
{
String STR _ Server Message content;
// Read the message content to the STR _ Server Message Content Variable
If (read. readstring (Out STR _ Server Message content, system. Text. encoding. utf8 ))
{
// Display the message content in ListBox.
Lb_msgshow.items.insert (0, STR _ Server Message content );
}
}
}

So far, our client reading and display functions have been completed. The following is the final result after running. It is difficult to enable the WP7 simulator multiple times. So I use another common silverlihgt client to connect to the server:

 

Conclusion: after these drills, I believe you have understood dycom as a concept. I hope it will create more surprises and values for you.

Download the source project file for this Walkthrough: http://dl.dbank.com/c0wllqu9w7

Author: Li Donghai

Net Name: inner cool Superman

Related Article

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.