c#_判断Tcp连接是否可用(Connected)

//use funciton isConnected to judge if the tcp connection is available
public bool isConnected(Socket socket)
        {
            try{
                if( socket.Connected && socket.Poll( 0, SelectMode.SelectRead ) )
                {
                    byte[] msg = new byte[ 1 ];
                    if( socket.Receive( msg, 0, 1, SocketFlags.Peek ) != 0 )
                        return true;
                    else
                        return false;
                }
            }
            catch( SocketException e )
            {
                string expMsg = "错误信息:\n" + e.ToString();
                MessageBox.Show( expMsg, "socket状态检查出错..." );
            }
            return socket.Connected;
        }

你可能感兴趣的:(C++,c,socket,C#)