1.Client CreationGPRSConnectionAPI
The client uses evc4.0 for development. The main class library is the Connection Manager series APIs. The client uses the APIS described below for GPRS dial-up connections.
Connection Manager (I translated as Connection Manager, this object is a collection of Apis) series of APIs are designed to centrally manage the establishment and maintenance of network connections for devices based on the Windows Mobile series. Mobile applicationsProgramUse the Connection Manager API to establish or plan a network connection, while the Connection Manager controls all status information during the connection process. when an application initiates a connection (such as the Internet), it simply notifies the Connection Manager.
When an application initiates a network connection request, the Connection Manager first obtains all possible connection information from the Connection Service Provider (CSPs, then, the connection manager selects an optimal connection based on overhead, latency, bandwidth, and other factors from the connection information. Finally, the Connection Manager queues the requested connection, then use CSPs to establish a connection at a suitable time.
[API function ]:
A) connmgrapireadyevent () function
Function prototype: handle winapi connmgrapireadyevent ();
Using this function, we can return a connection event handle. Remember to release it in time after obtaining the handle.
B) connmgrconnectionstatus () function
Function prototype: hresult winapi connmgrconnectionstatus (handle hconnection, DWORD * pdwstatus );
With the return value of this function pdwstatus, we can get a lot of information about the connection. For example, if the connection is successful, connmgr_status_connected will be returned. If the connection is closed, connmgr_status_disconnected will be returned, there are as many as 14, which can fully meet our application needs;
C) connmgrenumdestinations () function
Function prototype: hresult winapi connmgrenumdestinations (INT nindex, connmgr_destination_info * pdestinfo );
Generally, our PDA has several connections when connecting to GPRS. Using this function, we can enumerate all available connections and then filter the selected connections to obtain an optimal connection.
next we will talk about two very important functions. We will use one of the two functions to initiate a connection. They are:
d) connmgrestablishconnection () and connmgrestablishconnectionsync () functions . One of these two functions is used to initiate an asynchronous connection request and the other for Synchronous requests, the asynchronous connection request can be returned immediately after the connection is initiated. The synchronous request client will be blocked until the function returns to confirm the connection. I will not describe the asynchronous and synchronous requests;
their prototype is:
hresult winapi connmgrestablishconnection (connmgr_connectioninfo * pconninfo, handle * phconnection);
hresult winapi handle (connmgr_connectioninfo * pconninfo, handle * Phconnection, DWORD dwtimeout, DWORD * pdwstatus);
the first parameter of both functions is a connmgr_connectioninfo object, which is a struct, it stores a series of information about the client-side connection request. Therefore, when we call this function, we must construct this object and pass it as a parameter to the connection function. It is necessary to talk about this struct. The prototype of this struct is as follows:
typedef struct _ connmgr_connectioninfo
{< br> DWORD cbsize; DWORD dwparams; DWORD dwflags;
DWORD dwpriority; bool bexclusive; bool bdisabled; guid guiddestnet;
Hwnd; uint umsg; lparam; ulong ulmaxcost; ulong ulminrcvbw;
Ulong ulmaxconnlatency;
} Connmgr_connectioninfo;
The dwflags parameter is used to specify our access points, such as cmner and cmwap, while the guid parameter indicates the globally unique identifier that corresponds to each access point, for more information about how to obtain or view the guid, see connmgr in the "\ Program Files \ Windows CE tools \ wce420 \ Pocket PC 2003 \ include \ armv4" directory. h file, which contains the guid of each access point, for example:
Cmnet(0x436ef144, 0xb4fb, 0x4863, 0xa0, 0x41, 0x8f, 0x90, 0x5a, 0x62, 0xc5, 0x72)
Cmwap0x7022e968, 0x5a97, 0x4051, 0xbc, 0x1c, 0xc5, 0x78, 0xe2, 0xfb, 0xa5, 0xd9
To learn more, you can view the connection information of the Connection Manager on the PDA by checking the Registry. In PPC 2003, the Registry path is:
[HKEY_LOCAL_MACHINE \ comm \ connmgr], as shown in:
The destinations field corresponds to all our available network connections. This is the same effect as the connmgrenumdestinations () method. In the default Internet settings, we will see the guid of cmnet, as follows:
Here there is a destid item, which corresponds to our long-lost guid
E) connmgrreleaseconnection Function
After we establish a connection in the previous step, we will get a connection handle. We need to call this function to start a new connection or disconnect the connection to release the previously created connection.
Its prototype is hresult winapi connmgrreleaseconnection (handle hconnection, bool bcache );
[Procedure for using the connection management API]
First, we use the connmgrapireadyevent () function to check whether there are available connections. If there are available connections, we use the connmgrenumdestinations () function to enumerate all available connections, then traverse all connections and call our synchronous or asynchronous Connection Methods connmgrestablishconnectionsync () and connmgrestablishconnection () to initiate a connection. Once the connection succeeds, we can proceed to our great next step, is to communicate with our server.
[GPRS demo]
[Introduction to GPRS demo]
The GPRS demo mainly uses the preceding several important API functions to obtain the currently available connections, automatically selects an optimal connection path, and then enables the connection, after the connection starts successfully, use a socket to connect to the Internet and communicate with the internet server.
First, check whether there are available connections.
bool cconnectmanager: getconnmgravailable () {handle hconnmgr = connmgrapireadyevent (); bool bavailbale = false; DWORD dwresult =: waitforsingleobject (hconnmgr, 2000 ); If (dwresult = wait_object_0) {bavailbale = true ;}< span style = "color: green "> // close If (hconnmgr) closehandle (hconnmgr); return bavailbale ;}
Then enumerate all available connections:
void cconnectmanager: enumnetidentifier (Out cstringarray & strary) {connmgr_destination_info networkdestinfo = {0 }; // obtain the Network List for (DWORD dwenumindex = 0; dwenumindex ++) {memset (& networkdestinfo, 0, sizeof (connmgr_destination_info); If (connmgrenumdestinations (dwenumindex, & networkdestinfo) = e_fail) { Break ;} strary. add (networkdestinfo. szdescription) ;}}
Next, find the connection "Internet", which can be completed by remote URL ing, so that the system can automatically select the best connection.
Int Cconnectmanager: mapurlandguid (lpctstr lpszurl, out guid & guidnetworkobject, out cstring * pcsdesc /* = NULL */ ){ If (! Lpszurl | lstrlen (lpszurl) <1) Return False; memset (& guidnetworkobject, 0, Sizeof (Guid )); Int Nindex = 0; hresult = connmgrmapurl (lpszurl, & guidnetworkobject, (DWORD *) & nindex ); If (Failed (hresult) {nindex =-1; DWORD dwlasterror = getlasterror (); afxmessagebox (_ T ( "Cocould not map a request to a network identifier" ));}Else { If (Pcsdesc) {connmgr_destination_info destinfo = {0 }; If (Succeeded (connmgrenumdestinations (nindex, & destinfo) {* pcsdesc = destinfo. szdescription ;}}} Return Nindex ;}
Enable the connection with the specified number and check the connection status.
Bool cconnectmanager: establishconnection (DWORD dwindex ){ // Release the previous connection Releaseconnection ();// Obtain the correct connection information Connmgr_destination_info destinfo = {0}; hresult = connmgrenumdestinations (dwindex, & destinfo); bool Bret = false; If (Succeeded (hresult )){ // Initialize the Connection Structure Connmgr_connectioninfo conninfo; zeromemory (& conninfo, Sizeof (Conninfo); conninfo. cbsize = Sizeof (Conninfo); conninfo. dwparams = connmgr_param_guiddestnet; conninfo. dwflags = connmgr_flag_proxy_http | connmgr_flag_proxy_wap | connmgr_flag_proxy_socks4 | connmgr_flag_proxy_socks5; conninfo. dwpriority = connmgr_priority_userinteractive; conninfo. guiddestnet = destinfo. guid; conninfo. bexclusive = false; conninfo. bdisabled = false; DWORD dwstatus = 0; hresult = connmgrestablishconnectionsync (& conninfo, & m_hconnection, 10*1000, & dwstatus ); If (Failed (hresult) {m_hconnection = NULL ;} Else Bret = true ;} Return Bret ;}
Detect connection status
bool cconnectmanager: waitforconnected ( int ntimeoutsec, DWORD * pdwstatus /* = NULL */) {DWORD dwstarttime = gettickcount (); bool Bret = false; while (gettickcount ()-dwstarttime <(DWORD) ntimeoutsec * 1000) { If (m_hconnection) {DWORD dwstatus = 0; hresult hR = connmgrconnectionstatus (m_hconnection, & dwstatus); If (pdwstatus) * pdwstatus = dwstatus; If (succeeded (HR )) { If (dwstatus = connmgr_status_connected) {Bret = true; Break ;}} sleep (100) ;}< span style =" color: Blue "> return Bret ;}
Remember to release the connection
VoidCconnectmanager: releaseconnection (){If(M_hconnection) {connmgrreleaseconnection (m_hconnection, false); m_hconnection = NULL ;}}
2. Socket Communication API between the client and the server
The socket communication-related development API is defined in the winsock2.h. File, because socket communication is not the focus of this article but must be involved.
A) wsastartup Function. When an application performs Windows Sockets communication, you must first call this function to specify the Windows scoket version to be loaded by the application, before the application ends, we should call wsacleanup to release the system resources used.
B) connect function. This function is used to establish a connection.
C) Select Function. After a connection is established, it is used to listen for data transmission.
D) Send Function. Used to send messages to the server
3. socket communication between the server and the client
The server mainly involves the following databases:System. net,System. net. Sockets,System. IO;
System. the. NET namespace provides simple programming interfaces for various protocols used on the current network. net. the sockets namespace provides a managed implementation of the Windows Sockets (Winsock) interface for developers who need to strictly control network access.
The system. Io namespace contains the types that allow reading and writing files and data streams, as well as the types that support basic files and directories.
Technologies that need to be understood include multithreading, events and delegation, and socket communication.
Reference)ArticleAddress
1.Http://msdn.microsoft.com/en-us/library/ms827527.aspx(MsdnMobile Development Center );
2.Http://www.codeguru.com/cpp/w-p/ce/pocketpc/article.php/c7355/(PPCApplicationsInternetConnection );
3.Http://www.vckbase.com/document/viewdoc? Id = 1803 (how to establish a GPRS connection)
4.Http://www.microsoft.com/china/msdn/library/NetFramework/netcompactframework/understandingnetcfFAQ.mspx? MFR = true(FAQs)
5.PPC SDKReference (InstallationPPC 2003 SDK)
6. Download GPRS demo