private void Test_Click(object sender, EventArgs e)
{
bool result = false;
uint dwStatus = 0;
List<ConnectManager.CONNMGR_DESTINATION_INFO> lst = null;
if (connManager.WaitForConnected(10, ref dwStatus))
{
result = true;
}
else
{
if (!connManager.GetConnMgrAvailable())
{
result = false;
}
else
{
lst = connManager.EnumNetIdentifier();
if (lst.Count == 0)
{
result = false;
}
else
{
int nIndex = 0;
for (; nIndex < lst.Count; nIndex++)
{
if (string.Compare(lst[nIndex].szDescription, "Internet 设置", true) == 0)
{
break;
}
}
if (!connManager.EstablishConnection((uint)nIndex))
{
result = false;
}
else
{
dwStatus = 0;
if (!connManager.WaitForConnected(10, ref dwStatus))
{
result = false;
}
else
{
Connect("baidu.com", 80);
result = true;
}
}
}
}
}
}
private static void Connect(string address, int port)
{
try
{
IPHostEntry IpHost = Dns.GetHostEntry(address);
Socket s = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint point = new IPEndPoint(IpHost.AddressList[0], port);
s.Connect(point);
if (s.Connected)
{
MessageBox.Show("Connection OK!");
}
else
{
MessageBox.Show("Connection failed");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}