惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 聂微东
MyScale Blog
MyScale Blog
The GitHub Blog
The GitHub Blog
C
Check Point Blog
M
MIT News - Artificial intelligence
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
H
Help Net Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
DataBreaches.Net
大猫的无限游戏
大猫的无限游戏
D
Docker
Last Week in AI
Last Week in AI
IT之家
IT之家
F
Fortinet All Blogs
A
About on SuperTechFans
P
Proofpoint News Feed
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
B
Blog RSS Feed
博客园_首页
月光博客
月光博客
博客园 - 司徒正美
Y
Y Combinator Blog

博客园 - Blaze

eVC++就是eVC++啊 - Blaze - 博客园 VC++常用数据类型及其操作详解[徐兆元] From VB.NET to C# and Back Again [By Darren Neimke and Scott Mitchell ] 请不要做浮躁的人(老文了,还是转一下,共勉) 地球上最慢的网路不在巴布里亚新几内亚和尼泊尔之间,而在中国网通和电信之间! 1年零2个月零25天 我回来了 .NET的Pascal--Chrome来了! Gmail疯了?50个邀请! 检举个博客园的Bug! INI配置文件的替代品-XML配置文件的操作 INI的替代品--XML配置文件读取与保存 Wallop下蛋送邀请。 从WebService的SessionID说起 Gmail下蛋了 udp的奇怪问题 webservice对"小"规模数据传输的效率问题 运动会痛苦经历 n个VB.Net C#代码转换工具 关于时间的加减计算
[翻译]用TcpClient建立GPRS连接
Blaze · 2006-06-09 · via 博客园 - Blaze

NETCF的HttpWebRequest在有线或者wifi网络不能使用时会自动建立GPRS连接。因此,当你请求一个http连接或者webservice连接时, 你不用专门为GPRS连接写代码。 但这对于低等的socket类(例如TcpClient 和 UdpClient并不可行。对于这些类,你就得用 Connection Manager APIs 来建立/断开连接了。为了方便大家更方便地操作GPRS 连接, 我写了一个托管的类。

    public class GPRSConnection
    
{
        
const int S_OK = 0;
        
const uint CONNMGR_PARAM_GUIDDESTNET = 0x1;
        
const uint CONNMGR_FLAG_PROXY_HTTP = 0x1;
        
const uint CONNMGR_PRIORITY_USERINTERACTIVE = 0x08000;
        
const uint INFINITE = 0xffffffff;
        
const uint CONNMGR_STATUS_CONNECTED = 0x10;
        
static Hashtable ht = new Hashtable();

        
static GPRSConnection()
        
{
            ManualResetEvent mre 
= new ManualResetEvent(false);
            mre.Handle 
= ConnMgrApiReadyEvent();
            mre.WaitOne();
            CloseHandle(mre.Handle);
        }


        
~GPRSConnection()
        
{
            ReleaseAll();
        }


        
public static bool Setup(Uri url)
        
{
            
return Setup(url.ToString());
        }


        
public static bool Setup(string urlStr)
        
{
            ConnectionInfo ci 
= new ConnectionInfo();
            IntPtr phConnection 
= IntPtr.Zero;
            
uint status = 0;

            
if (ht[urlStr] != null)
                
return true;

            
if (ConnMgrMapURL(urlStr, ref ci.guidDestNet, IntPtr.Zero) != S_OK)
                
return false;
            
            ci.cbSize 
= (uint) Marshal.SizeOf(ci);
            ci.dwParams 
= CONNMGR_PARAM_GUIDDESTNET;
            ci.dwFlags 
= CONNMGR_FLAG_PROXY_HTTP;
            ci.dwPriority 
= CONNMGR_PRIORITY_USERINTERACTIVE;
            ci.bExclusive 
= 0;
            ci.bDisabled 
= 0;
            ci.hWnd 
= IntPtr.Zero;
            ci.uMsg 
= 0;
            ci.lParam 
= 0;

            
if (ConnMgrEstablishConnectionSync(ref ci, ref phConnection, INFINITE, ref status) != S_OK &&
                status 
!= CONNMGR_STATUS_CONNECTED)
                
return false;

            ht[urlStr] 
= phConnection;
            
return true;
        }


        
public static bool Release(Uri url)
        
{
            
return Release(url.ToString());
        }


        
public static bool Release(string urlStr)
        
{
            
return Release(urlStr, true);
        }


        
private static bool Release(string urlStr, bool removeNode)
        
{
            
bool res = true;
            IntPtr ph 
= IntPtr.Zero;
            
if (ht[urlStr] == null)
                
return true;
            ph 
= (IntPtr)ht[urlStr];
            
if (ConnMgrReleaseConnection(ph, 1!= S_OK)
                res 
= false;
            CloseHandle(ph);
            
if (removeNode)
                ht.Remove(urlStr);
            
return res;
        }


        
public static void ReleaseAll()
        
{
           
foreach(DictionaryEntry de in ht)
           
{
               Release((
string)de.Key, false);
           }

           ht.Clear();
        }


        [StructLayout(LayoutKind.Sequential)]
        
public struct ConnectionInfo
        
{
            
public uint cbSize;
            
public uint dwParams;
            
public uint dwFlags;
            
public uint dwPriority;
            
public int bExclusive;
            
public int bDisabled;
            
public Guid guidDestNet;
            
public IntPtr hWnd;
            
public uint uMsg;
            
public uint lParam;
            
public uint ulMaxCost;
            
public uint ulMinRcvBw;
            
public uint ulMaxConnLatency;
        }


        [DllImport(
"cellcore.dll")]
        
private static extern int ConnMgrMapURL(string pwszURL, ref Guid pguid, IntPtr pdwIndex);

        [DllImport(
"cellcore.dll")]
        
private static extern int ConnMgrEstablishConnectionSync(ref ConnectionInfo ci, ref IntPtr phConnection, uint dwTimeout, ref uint pdwStatus);

        [DllImport(
"cellcore.dll")]
        
private static extern IntPtr ConnMgrApiReadyEvent();

        [DllImport(
"cellcore.dll")]
        
private static extern int ConnMgrReleaseConnection(IntPtr hConnection, int bCache);

        [DllImport(
"coredll.dll")]
        
private static extern int CloseHandle(IntPtr hObject);
    }

 使用GRPS托管类, 调用Setup方法创建连接。下面是个例子。

        public void DoTcpConnection()
        
{
            
string url = "www.msn.com";
            
bool res = GPRSConnection.Setup("http://" + url + "/");
            
if (res)
            
{
                TcpClient tc 
= new TcpClient(url, 80);
                NetworkStream ns 
= tc.GetStream();
                
byte[] buf = new byte[100];
                ns.Write(buf, 
0100);
                tc.Client.Shutdown(SocketShutdown.Both);
                ns.Close();
                tc.Close();
                MessageBox.Show(
"Wrote 100 bytes");
            }

            
else
            
{
                MessageBox.Show(
"Connection establishment failed");
            }

        }

Enjoy,

Anthony Wong [MSFT]

This posting is provided "AS IS" with no warranties, and confers no rights.