How to Establish a GPRS connection in Windows Mobile so that the socket can communicate normally

Source: Internet
Author: User

How to Establish a GPRS connection in Windows Mobile so that the socket can communicate normally

 
Content Abstract: I use socket for data transmission. It passes debugging through data cable + ActiveSync. data transmission is normal. When I prepare to submit the software to the Quality Control Department, when the real GPRS is used for communication testing, the problem arises and the connection cannot be established, but the Internet explorer of the mobile phone can open the webpage normally, what's strange is that as long as the web page is successfully accessed using IE, my socket can communicate normally. It seems that the legendary GPRS connection is often misunderstood by me.

Download the sample source code or material in this article

Recently, a medical project program needs to be compiled using Windows Mobile for communication. Data on the mobile phone end needs to 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 (maid, 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;
} Run the following code to enable the connection bool cconnectmanager with the specified number: establishconnection (DWORD dwindex)
{
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;
} To ensure that the connection is actually available, check the connection status. If the connection is not "established successfully" within the specified time, the connection is considered to fail to be enabled properly, you may need to configure the Connection Manager interface bool cconnectmanager: waitforconnected (INT ntimeoutsec, DWORD * pdwstatus/* = NULL */) of the mobile phone */)
{
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. The following is an example of testing whether the Socket network connection can be established normally: setwaitcursor ();
Csocket sock;
Sock. Create ();
If (sock. Connect (_ T ("www.baidu.com"), 80 ))
{
Restorecursor ();
Afxmessagebox (_ T ("connect to www.baidu.com successfully"), mb_iconinformation );
}
Else
{
Restorecursor ();
Afxmessagebox (_ T ("connect to www.baidu.com failed "));
}

 

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.