public static void AcceptThread() ...{ Thread.CurrentThread.IsBackground = true; while (true) ...{ uint dummy = 0; byte[] inOptionValues = new byte[Marshal.SizeOf(dummy) * 3]; BitConverter.GetBytes((uint)1).CopyTo(inOptionValues, 0); BitConverter.GetBytes((uint)5000).CopyTo(inOptionValues, Marshal.SizeOf(dummy)); BitConverter.GetBytes((uint)5000).CopyTo(inOptionValues, Marshal.SizeOf(dummy) * 2); try ...{ Accept(inOptionValues); } catch ...{ } } } private static void Accept(byte[] inOptionValues) ...{ Socket socket = Public.s_socketHandler.Accept(); socket.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null); UserInfo info = new UserInfo(); info.socket = socket; int id = GetUserId(); info.Index = id; Public.s_userList.Add(id, info); socket.BeginReceive(info.Buffer, 0, info.Buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), info); }
掉线的时候 windows 会发个通知你程序启动的时候订阅一下 System.Net.NetworkInformation.NetworkChange 发布了两个事件: NetworkAddressChanged Occurs when the IP address of a network interface changes. NetworkAvailabilityChanged Occurs when the availability of the network changes
[DllImport("wininet.dll")] private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);public bool IsInternetConnected() { int i = 0; return InternetGetConnectedState(out i, 0); }
uint dummy = 0;
byte[] inOptionValues = new byte[Marshal.SizeOf(dummy) * 3];
BitConverter.GetBytes((uint)1).CopyTo(inOptionValues, 0);
BitConverter.GetBytes((uint)1500).CopyTo(inOptionValues, Marshal.SizeOf(dummy));
BitConverter.GetBytes((uint)8000).CopyTo(inOptionValues, Marshal.SizeOf(dummy) * 2);
newRemoteSocket.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
//struct tcp_keepalive
//{
// u_long onoff; //是否启用Keep-Alive
// u_long keepalivetime; //多长时间后开始第一次探测(单位:毫秒)
// u_long keepaliveinterval; //探测时间间隔(单位:毫秒)
//};
记得加分啊
如果网络连接可用,则为 true;否则为 false。
...{
Thread.CurrentThread.IsBackground = true;
while (true)
...{
uint dummy = 0;
byte[] inOptionValues = new byte[Marshal.SizeOf(dummy) * 3];
BitConverter.GetBytes((uint)1).CopyTo(inOptionValues, 0);
BitConverter.GetBytes((uint)5000).CopyTo(inOptionValues, Marshal.SizeOf(dummy));
BitConverter.GetBytes((uint)5000).CopyTo(inOptionValues, Marshal.SizeOf(dummy) * 2);
try
...{
Accept(inOptionValues);
}
catch ...{ }
}
} private static void Accept(byte[] inOptionValues)
...{
Socket socket = Public.s_socketHandler.Accept();
socket.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
UserInfo info = new UserInfo();
info.socket = socket;
int id = GetUserId();
info.Index = id;
Public.s_userList.Add(id, info);
socket.BeginReceive(info.Buffer, 0, info.Buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), info);
}
System.Net.NetworkInformation.NetworkChange 发布了两个事件: NetworkAddressChanged Occurs when the IP address of a network interface changes.
NetworkAvailabilityChanged Occurs when the availability of the network changes
如果网络连接可用,则为 true;否则为 false。 我试过,可行!但请问是否可以判断指定网卡的掉线状态?
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);public bool IsInternetConnected()
{
int i = 0;
return InternetGetConnectedState(out i, 0);
}
{
if (NetworkInterface.GetIsNetworkAvailable ())
{
MessageBox.Show("网络连接可用!");
}
else
{
MessageBox.Show("网络连接已断开!");
}
}
{
if (NetworkInterface.GetIsNetworkAvailable ())
{
MessageBox.Show("网络连接可用!");
}
else
{
MessageBox.Show("网络连接已断开!");
}
}
private void GetNetworkInterface()
{
if (NetworkInterface.GetIsNetworkAvailable ())
{
MessageBox.Show("网络连接可用!");
}
else
{
MessageBox.Show("网络连接已断开!");
}
}
private void GetNetworkInterface()
{
if (NetworkInterface.GetIsNetworkAvailable ())
{
MessageBox.Show("网络连接可用!");
}
else
{
MessageBox.Show("网络连接已断开!");
}
}
try catch 这个捕获socketexception就可以了
{
PPACKET_OID_DATA oidData; // Defined in Ntddpack.h
unsigned int buffLen; // OID memory allocation
unsigned long nRet;
buffLen = sizeof(PACKET_OID_DATA) + sizeof(unsigned long) - 1;
oidData = (PPACKET_OID_DATA) GlobalAllocPtr(GMEM_MOVEABLE | GMEM_ZEROINIT, buffLen);
if (oidData == NULL)
return FALSE;
oidData->Oid = OID_GEN_MEDIA_CONNECT_STATUS;
oidData->Length = sizeof(unsigned long);
if ((PacketRequest(lpAdapter,0, oidData)) > 0)
{
nRet = *(unsigned long *)oidData->Data;
GlobalFreePtr(oidData);
return nRet;
}
else
{
GlobalFreePtr(oidData);
return FALSE;
}
}
定时检测每个adapter的状态
NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);//这个可以判断网络可用性的判断 void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
// throw new NotImplementedException();
//网络可用性变化的事件处理
}
/// <summary>
/// 显示网络适配器的信息
/// </summary>
/// <returns></returns>
public bool DisplayDnsConfiguration()
{
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
listBox1.Items.Clear();
foreach (NetworkInterface adapter in adapters)
{
string str = adapter.Name + "......." + adapter.OperationalStatus;
listBox1.Items.Add(str);
if (adapter.OperationalStatus == OperationalStatus.Up)
{
MessageBox.Show("有网络连接可用");
return true;
}
}
return false;
}</code>