111,106
社区成员




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.InteropServices;
namespace eXtremeSQLwcfws
{
public class IsConnected
{
/// <summary>
/// 检查远程主机与本机是否存在连接
/// </summary>
/// <param name="host">远程主机的IP</param>
/// <param name="port">远程主机的PORT</param>
/// <returns></returns>
public static int CheckConnected(string host, string port)
{
try
{
MIB_TCPTABLE tcpTableData = new MIB_TCPTABLE();
tcpTableData = GetTcpTableInfo();
for (int i = 0; i < tcpTableData.dwNumEntries; i++)
{
string i_p = GetIpAddress(tcpTableData.table[i].dwRemoteAddr) + ":" + GetTcpPort(tcpTableData.table[i].dwRemotePort).ToString();
if (i_p == host + ":" + port) return tcpTableData.table[i].dwState;
}
}
catch (Exception)
{ }
return 0;
}
[StructLayout(LayoutKind.Sequential)]
public class MIB_TCPROW
{
public int dwState;
public int dwLocalAddr;
public int dwLocalPort;
public int dwRemoteAddr;
public int dwRemotePort;
}
[StructLayout(LayoutKind.Sequential)]
public class MIB_TCPTABLE
{
public int dwNumEntries;
public MIB_TCPROW[] table;
}
protected static string GetIpAddress(long ipAddrs)
{
try
{
System.Net.IPAddress ipAddress = new System.Net.IPAddress(ipAddrs);
return ipAddress.ToString();
}
catch
{ return ipAddrs.ToString(); }
}
[DllImport("Ws2_32.dll")]
static extern ushort ntohs(ushort netshort);
protected static ushort GetTcpPort(int tcpPort)
{
return ntohs((ushort)tcpPort);
}
[DllImport("Iphlpapi.dll")]
static extern int GetTcpTable(IntPtr pTcpTable, ref int pdwSize, bool bOrder);
protected static MIB_TCPTABLE GetTcpTableInfo()
{
//声明一个指针准备接受Tcp连接信息
IntPtr hTcpTableData = IntPtr.Zero;
//声明hTcpTableData指针所指向的内存缓冲区大小
int iBufferSize = 0;
//声明MIB_TCPTABLE对象,作为返回值
MIB_TCPTABLE tcpTable = new MIB_TCPTABLE();
//声明一个List对象来临时存放MIB_TCPROW对象
List<MIB_TCPROW> lstTcpRows = new List<MIB_TCPROW>();
//调用API来获得真正的缓冲区大小,iBufferSize默认为0,
//这时调用API GetTcpTable会触发一个异常ERROR_INSUFFICIENT_BUFFER
//通过这个异常系统会把真正的缓冲长度返回
GetTcpTable(hTcpTableData, ref iBufferSize, false);
//为托管指针在堆上分配内存
hTcpTableData = Marshal.AllocHGlobal(iBufferSize);
//求得MIB_TCPROW对象的内存字节数
int iTcpRowLen = Marshal.SizeOf(typeof(MIB_TCPROW));
//根据上面得到的缓冲区大小来推算MIB_TCPTABLE里的MIB_TCPROW数组长度
//下面用缓冲长度-sizeof(int)也就是去掉MIB_TCPTABLE里的成员dwNumEntries所占用的内存字节数
int aryTcpRowLength = (int)Math.Ceiling((double)(iBufferSize - sizeof(int)) / iTcpRowLen);
//重新取得TcpTable的数据
GetTcpTable(hTcpTableData, ref iBufferSize, false);
//下面是关键,由于MIB_TCPTABLE里的成员有一个是数组,而这个数组长度起初我们是不能确定的
//所以这里我们只能根据分配的指针来进行一些运算来推算出我们所要的数据
for (int i = 0; i < aryTcpRowLength; i++)
{
//hTcpTableData是指向MIB_TCPTABLE缓冲区的内存起始区域,由于其成员数据在内存中是顺序排列
//所以我们可以推断hTcpTableData+4(也就是sizeof(dwNumEntries)的长度)后就是MIB_TCPROW数组的第一个元素
IntPtr hTempTableRow = new IntPtr(hTcpTableData.ToInt32() + 4 + i * iTcpRowLen);
MIB_TCPROW tcpRow = new MIB_TCPROW();
tcpRow.dwLocalAddr = 0;
tcpRow.dwLocalPort = 0;
tcpRow.dwRemoteAddr = 0;
tcpRow.dwRemotePort = 0;
tcpRow.dwState = 0;
//把指针数据拷贝到我们的结构对象里。
Marshal.PtrToStructure(hTempTableRow, tcpRow);
lstTcpRows.Add(tcpRow);
}
tcpTable.dwNumEntries = lstTcpRows.Count;
tcpTable.table = new MIB_TCPROW[lstTcpRows.Count];
lstTcpRows.CopyTo(tcpTable.table);
return tcpTable;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.InteropServices;
namespace eXtremeSQLwcfws
{
public class IsConnected
{
/// <summary>
/// 检查远程主机与本机是否存在连接
/// </summary>
/// <param name="host">远程主机的IP</param>
/// <param name="port">远程主机的PORT</param>
/// <returns></returns>
public static int CheckConnected(string host, string port)
{
List<MIB_TCPROW> lstTcpRows = new List<MIB_TCPROW>();
int dwNumEntries = 0;
GetTcpTableInfo(ref dwNumEntries, ref lstTcpRows);
for (int i = 0; i < dwNumEntries; i++)
{
string st = GetIpAddress(lstTcpRows[i].dwRemoteAddr) + ":" + GetTcpPort(lstTcpRows[i].dwRemotePort).ToString();
if (st == host + ":" + port) return lstTcpRows[i].dwState;
}
return 0;
}
[StructLayout(LayoutKind.Sequential)]
public class MIB_TCPROW
{
public int dwState;
public int dwLocalAddr;
public int dwLocalPort;
public int dwRemoteAddr;
public int dwRemotePort;
}
protected static string GetIpAddress(long ipAddrs)
{
try
{
System.Net.IPAddress ipAddress = new System.Net.IPAddress(ipAddrs);
return ipAddress.ToString();
}
catch
{ return ipAddrs.ToString(); }
}
[DllImport("Ws2_32.dll")]
static extern ushort ntohs(ushort netshort);
protected static ushort GetTcpPort(int tcpPort)
{
return ntohs((ushort)tcpPort);
}
[DllImport("Iphlpapi.dll")]
static extern int GetTcpTable(IntPtr pTcpTable, ref int pdwSize, bool bOrder);
protected static void GetTcpTableInfo(ref int dwNumEntries, ref List<MIB_TCPROW> lstTcpRows)
{
//声明一个指针准备接受Tcp连接信息
IntPtr hTcpTableData = IntPtr.Zero;
//声明hTcpTableData指针所指向的内存缓冲区大小
int iBufferSize = 0;
//调用API来获得真正的缓冲区大小,iBufferSize默认为0,
//这时调用API GetTcpTable会触发一个异常ERROR_INSUFFICIENT_BUFFER
//通过这个异常系统会把真正的缓冲长度返回
GetTcpTable(hTcpTableData, ref iBufferSize, false);
//为托管指针在堆上分配内存
hTcpTableData = Marshal.AllocHGlobal(iBufferSize);
//求得MIB_TCPROW对象的内存字节数
int iTcpRowLen = Marshal.SizeOf(typeof(MIB_TCPROW));
//根据上面得到的缓冲区大小来推算MIB_TCPTABLE里的MIB_TCPROW数组长度
int aryTcpRowLength = (int)Math.Ceiling((double)(iBufferSize - sizeof(int)) / iTcpRowLen);
//重新取得TcpTable的数据
GetTcpTable(hTcpTableData, ref iBufferSize, false);
for (int i = 0; i < aryTcpRowLength; i++)
{
MIB_TCPROW tcpRow = new MIB_TCPROW();
tcpRow = (MIB_TCPROW)Marshal.PtrToStructure(hTcpTableData + sizeof(int) + i * iTcpRowLen, typeof(MIB_TCPROW));
lstTcpRows.Add(tcpRow);
}
dwNumEntries = lstTcpRows.Count;
}
}
}