[C #] Accurate judgment network available

xiaoxiao2021-03-06  66

The connection attribute of the Socket class often does not accurately determine if the network is connected, and the following code can solve this problem.

///

/// Is /// Public Virtual Bool Connected {get {Try {// Check if the status of the socket is readable if (m_socket.connected && m_socket.poll (0, SelectMode .Selectread) {byte [] abyte = new byte [1]; // Because the TCP / IP protocol cannot accurately determine if the network can be used //, the PEEK parameter specifies that the read character will not be in the buffer. Remove // ​​If readable, the connection is indicated by IF (M_Socket.Receive (Abyte, 0, 1, SocketFlags.peek)! = 0) Return True; Close ("Disconnected."); Return False;}} catch (Socketexception e) {onException (e);} return m_socket.connected;}}

转载请注明原文地址:https://www.9cbs.com/read-110393.html

New Post(0)