【问题标题】:Is it possible to test a specific IP address and port from client machine?是否可以从客户端机器测试特定的 IP 地址和端口?
【发布时间】:2019-11-20 21:32:27
【问题描述】:

我编写了以下类来连接客户端(到服务器):

public class ClientClass
{
    public string Host { get; set; }
    public int Port { get; set; }

    public TcpClient Tcp { get; private set; }
    private BinaryReader reader;
    private BinaryWriter writer;

    /// <summary>
    /// Creates a clienbt at the Client-end. 
    /// This requires Host and Post property to be set.
    /// </summary>
    public ClientClass()
    {

    }

    /// <summary>
    /// Creates a client at the Client-end.
    /// </summary>
    /// <param name="host"></param>
    /// <param name="port"></param>
    public ClientClass(string host, int port)
    {
        Host = host;
        Port = port;
    }

    /// <summary>
    /// creates a proxy-client at the Server-end.
    /// </summary>
    /// <param name="listener"></param>
    public ClientClass(TcpListener listener)
    {
        Tcp = listener.AcceptTcpClient();

        Host = ((IPEndPoint)Tcp.Client.RemoteEndPoint).Address.ToString();
        Port = ((IPEndPoint)Tcp.Client.LocalEndPoint).Port;

        NetworkStream stream = Tcp.GetStream();
        reader = new BinaryReader(stream);
        writer = new BinaryWriter(stream);
    }

    /// <summary>
    /// Connects the client to the server.
    /// </summary>
    /// <returns></returns>
    public bool Connect()
    {
        bool is_connected = IsConnected();

        if (!is_connected)
        {
            Tcp = new TcpClient(Host, Port);

            NetworkStream stream = Tcp.GetStream();
            reader = new BinaryReader(stream);
            writer = new BinaryWriter(stream);

            return true;
        }

        if (is_connected)
        {
            return true;
        }

        return false;
    }

    public bool IsConnected()
    {
        if (Tcp == null)
        {
            return false;
        }
        else
        {
            Socket s = Tcp.Client;

            bool part1 = s.Poll(1000, SelectMode.SelectRead);
            bool part2 = (s.Available == 0);
            if ((part1 && part2) || !s.Connected)
                return false;
            else
                return true;
        }
    }

    public void Write(string str)
    {
        if (IsConnected())
        {
            byte[] strBytes = Encoding.UTF8.GetBytes(str);
            byte[] lenBytes = BitConverter.GetBytes(strBytes.Length);
            Array.Reverse(lenBytes);
            writer.Write(lenBytes);
            writer.Write(strBytes);
            writer.Flush();
        }
        else
        {
            throw new Exception("Client  is not connected!");
        }
    }

    public string Read()
    {
        if (IsConnected())
        {
            byte[] lenBytes = reader.ReadBytes(4);
            Array.Reverse(lenBytes);
            int len = BitConverter.ToInt32(lenBytes, 0);
            byte[] bytes = reader.ReadBytes(len);
            string str = Encoding.UTF8.GetString(bytes);

            return str;
        }
        else
        {
            throw new Exception("Client  is not connected!");
        }
    } 

    public bool Close()
    {
        if (IsConnected())
        {
            if (Tcp != null)
            {
                Tcp.Close();
                Tcp = null;

                return true;
            }
        }

        return false;
    }
}

假设,在连接到服务器之前,我想测试服务器是否在 IP 地址 x.x.x.x 和端口 y 上处于活动状态。

有可能吗?

【问题讨论】:

  • 我想您需要创建一个到所需 tcp 套接字(IP+端口)的实际连接,并将连接超时设置得非常低,并相应地捕获异常。 afaik 有几个抽象层,并非所有抽象层都允许更改连接超时。
  • 一个迂回的方式,你可以创建一个可以 ping 所需服务器的 powershell 任务,然后只检查输出

标签: c# tcp client-server


【解决方案1】:

似乎无法设置连接超时,但您可以这样解决:

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

TcpClient socket = new TcpClient();

if (TestWithTimeout(socket,"1.2.3.4",80,1))
{
//  ....
}   


 bool TestWithTimeout( Socket/TcpClient socket = new TcpClient(); socket, string host, int port, int timeout)
{
    Task result = socket.ConnectAsync(host, port);
    Task.WaitAny(new[] { result }, timeout);
    return socket.Connected;
}

【讨论】:

  • 我使用的是TcpClient,而不是Socket
  • @user366312 基本上是一样的,请看我的编辑,检查和投票。
猜你喜欢
  • 1970-01-01
  • 2014-12-29
  • 2011-07-22
  • 2017-10-09
  • 2011-01-08
  • 2018-03-18
  • 1970-01-01
  • 2014-02-14
相关资源
最近更新 更多