Establish GPRS socket communication on Windows Mobile

Source: Internet
Author: User

For communication processing on Windows Mobile, data on the mobile phone must be transmitted to a server on the Internet through GPRS. I used socket for data transmission. I used data cable + ActiveSync for debugging. The data transmission was normal. When I was preparing to submit the software to the Quality Control Department, I used real GPRS for communication testing, when the problem arises, the connection cannot be established, but the IE browser on the mobile phone can open the webpage normally, And the strange thing is that once the web page is successfully accessed by the IE browser, my socket can communicate normally. It seems that the legendary GPRS connection is often misunderstood by me.
After activating GPRS on the mobile phone, our socket program cannot directly establish a network connection. We need to use the Connection Manager to obtain the currently available connections, and automatically select an optimal connection path, and then enable the connection, after the connection is successfully started, you can use socket for network connection. Generally, the GPRS dialing and connection process is automatically implemented here. The Source Code encapsulates a connection management class and test code, which clearly shows what operations Windows Mobile needs to do before socket programming.

First, you need to enumerate the currently 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 hResult = ConnMgrMapURL ( lpszURL, &guidNetworkObject, (DWORD*)&nIndex );if ( FAILED(hResult) ){nIndex = -1;DWORD dwLastError = GetLastError ();AfxMessageBox ( _T("Could 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;}

Run the following code to enable a connection with a specified number:

Bool cconnectmanager: establishconnection (DWORD dwindex) {releaseconnection (); // get the correct connection information includestinfo = {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 ;}

To ensure the availability of the connection, you need to check the connection status. If the connection is not in the "connection successful" status within the specified time, the connection is considered to fail to be enabled normally, you may need to configure the Connection Manager interface of your mobile phone.

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 );}return bRet;}

So far, our connection activation has been completed, and we can use the socket we are familiar with to write network communication programs.

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.