. NET Compact framework BASICS (6)-socket communication

Source: Internet
Author: User

Many people have asked questions about the use of socket in WM. Let's make a demo. Some problems encountered during this demo are shared with you.

 

The server in socket communication uses tcplistener

When tcplistner is instantiated, the IP address and port of the listener have been specified.

 

1 Listener =   New Tcplistener (IPaddress. Any, 4564 );

 

Then the socket object is obtained through the acceptsocket method.

1 Socket = Listener. acceptsocket ();
2 If (Socket ! =   Null )
3 {
4 If (Socket. Connected)
5 {
6 Int Length = Socket. Receive (bcontent );
7 This . Lstcontent. Add (encoding. Default. getstring (bcontent, 0 , Length ));
8 Socket. Send (encoding. Default. getbytes ( " Receive OK! " ));
9 Socket. Disconnect ( False );
10 }
11 }

 

Create a tcpclient object in the PC test.

After the tcpclient object is instantiated, the socket object is obtained through the client attribute.

1 Tcpclient Client =   New Tcpclient ();
2 Client. Connect ( New IPaddress ( New   Byte [] { 192 , 168 , 0 , 157 }), 4564 );
3 Byte [] Bcontent =   New   Byte [ 1024 ];
4 If (Client. Connected)
5 {
6 Socket socket = Client. client;
7 Socket. Send (encoding. Default. getbytes ( " Client send OK! " ));
8 Int Length = Socket. Receive (bcontent );
9 System. Console. writeline (encoding. Default. getstring (bcontent, 0 , Length ));
10
11 Socket. Disconnect ( False );
12 Socket. Close ();
13 Client. Close ();
14 }

 

After the test is passed, perform the test on the mobile device.

Directly create a socket object in Mobile.

Here, you can directly create a socket object on communication 2 to connect to the client. Of course, you can also create tcplistener and tcpclient objects. The results are the same.

1 Byte [] Bcontent =   New   Byte [ 1024 ];
2 String Tmpstring =   "" ;
3
4 Socket socket =   New Socket (addressfamily. InterNetwork, sockettype. Stream, protocoltype. TCP );
5 Ipendpoint localep =   New Ipendpoint ( New IPaddress ( New   Byte [] { 192 , 168 , 0 , 157 }), 4564 );
6 Socket. Connect (localep );
7 If (Socket. Connected)
8 {
9 Socket. Send (encoding. Default. getbytes ( " Client send OK! " ));
10 Int Length = Socket. Receive (bcontent );
11 Tmpstring = Encoding. Default. getstring (bcontent, 0 , Length );
12
13 Socket. Close ();
14 }
15 MessageBox. Show (tmpstring );

 

The effect is as follows:

Server:

 

Mobile:

 

Note the following:

When testing on a simulator, when both the server and the client are on the same machine, the server's listening IP address must be set to IPaddress. Any. Otherwise, if an IP address is specified and a socket communication occurs on the mobile device, an error will be reported that the connection is forced to be closed.

In fact, both the simulator and the actual device access network resources through DMA. In this way, the access speed will be much faster. Of course, you can also set up a simulator or the actual device to connect through TCP/IP. However, this approach is not recommended.

 

How to achieve connection through TCP/IP:

Choose tools> Options> device tools> devices> select a device> Properties> change transport to TCP connect transport.

When deploying to an actual device, copy the DLL and exe files in the programs files \ common files \ microsoft shared \ corecon \ 1.0 \ target \ wce400 \ <CPU> folder to the Windows directory of the device.

Then run commanclient2.exe on the device.

If the security function on the device is enabled, you need to run the cmaccept.exe

 

No information is found on how the simulator is implemented.

 

CodeDownload: mobilesocket.rar

 

Author: appleseeker
Date: 2008-08-05

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.