The socket type of Windows Phone 7

Source: Internet
Author: User

The socket type of Windows Phone 7 is changed from the socket type in Silverlight. In general, compared to the socket type in the complete. Net desktop, it has a large degree of castrated.

First, all operations can only be asynchronous, and different from the conventional socket-type APM asynchronous methods (such as beginconnect and beginsend), it uses an EAP-like mode, typical asynchronous calls in EAP mode include the downloadstringasync method of WebClient and its downloadstringcompleted event. However, the socket type in Windows Phone 7 does not define any events, but defines events in a socketasynceventargs type.

The workflow of this socket type is as follows. Initialize a socketasynceventargs type and set the corresponding attributes. For example, if a connection is performed, set the socketasynceventargs. remoteendpoint attribute. To send data, call the setbuffer method of socketasynceventargs type to set the internal buffer.

Next, register the completed event of the socketasynceventargs type. Because all operations of the socket type are asynchronous, you must register this event to obtain the result of the asynchronous operation. The delegate type of the completed event is eventhandler <socketasynceventargs>, that is, it uses itself as eventargs.

Finally, in the completed event, you can use some attributes of the socketasynceventargs object to obtain the data you need (it feels awkward to use an eventargs type too much ). For example, the socketasynceventargs. socketerror attribute returns the operation result. If you call the socket. receiveasync method, you need the socketasynceventargs. Buffer attribute. Note that if socketerror is success, the operation is successful. (What? Socketerror. Success? Is it an error? Haha, I don't know who defined it, so it's another joke)

 

Here is the Demo code. For example, if we need to connect to the remote asynchronously, we need to call the socket. connectasync method, as shown below:

// Create socketasynceventargs

VaR socketargs = new socketasynceventargs ();

// Set the connected IP address and port number

Socketargs. remoteendpoint = new ipendpoint (IPaddress. parse ("123.123.123.123"), 1033 );

// Register the completed event

Socketargs. Completed + = socketargs_completed;

// Start the operation

// The socket variable is a socket object

Socket. connectasync (socketargs );

 

Then the completed event method:

Void socketargs_completed (Object sender, socketasynceventargs E)

{

// Determine whether the request is successful

If (E. socketerror = socketerror. Success)

{

// Obtain the remote connection IP address and port number (ipendpoint object)

VaR endpoint = E. remoteendpoint;

}

Else

{

// Throw socketexception

Throw new socketexception (INT) E. socketerror );

}

}

As you can see, in the completed event, the above Code first judges the socketasynceventargs. socketerror attribute. If it fails, a socketexception is thrown.

 

There are also some very fatal problems. The socket in Windows Phone 7 does not have any TCP listening method. No listen, no accept method. At the same time, the socketasynceventargs type only has the remoteendpoint attribute, but does not have the localendpoint attribute. So helpless. However, the good news is that socket operations can be improved in Windows Phone 8 and Windows 8.

 

Update:

I wrote another article about Windows Phone 7 socket:

Run the networkstream type in Windows Phone 7.

 

Another point was found: socket. connectasync seems to always succeed, even if the corresponding remoteendpoint does not have a TCP listener at all, and the remoteendpoint and connected of the socket will be the result of successful connection, but in fact there is no connection at all. After sendasync is called, socketerror. notconnected is returned.

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.