【问题标题】:Check status on the server with specific port使用特定端口检查服务器上的状态
【发布时间】:2015-03-27 12:15:38
【问题描述】:

我需要检查本地网络中任何计算机的状态。但是我的代码无效,因为响应时间很长。我尝试了线程,该检查器在后台运行。但我需要更快的检查。我的程序作为客户端服务器运行。

在我的主程序中,我需要将可用的计算机写入 listbox。 示例:

我的主要客户想要获得本地网络中的可用计算机。计算机在端口 13000 上运行服务器。但是,当我想找出可用的计算机时,响应太长了。

变量

string message = "!";
string temp = "";
public static string list = "";

检查可用计算机:

public static void checker()
    {
        string IP;
        int statResp = 0;

        for (int i = 1; i < 10; i++)
        {              
            IP = "192.168.0." + i;

            string msg = "!";

            try
            {
                Int32 port = 13000;

                TcpClient client = new TcpClient(IP, port);


                Byte[] data = System.Text.Encoding.ASCII.GetBytes(msg);
                msg = "!";


                NetworkStream stream = client.GetStream();


                stream.Write(data, 0, data.Length);

                data = new Byte[256];
                String responseData = String.Empty;

                Int32 bytes = stream.Read(data, 0, data.Length);
                responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                if (responseData == " " || responseData == "!")
                {
                    statResp = 1; // response is correct server runs
                }
                stream.Close();
                client.Close();
            }
            catch (ArgumentNullException)
            {
                continue; // continue with next iteration
            }
            catch (SocketException)
            {
              continue; // continue with next iteration
            }
            if (statResp == 1)
            {
                list = IP; // list is variable
            }
        } 
    }

TIMER - 检查 list 变量中的数据

private void timer2_Tick(object sender, EventArgs e)
    {
        if (temp != list)
        {
            listBox1.Items.Add(list);
            temp = list;
        }
    }

如果 list 变量的值与 temp 变量中的值相同,则不会将旧地址添加到列表中。

【问题讨论】:

标签: c# .net client server status


【解决方案1】:

我解决了我的问题。当我将 UDP 广播数据包发送到 UDP 侦听器(客户端)时,发送者的 IP 地址将被传输到向最后一个发送者发送新数据包。原始发件人(服务器)将从远程客户端接收新的 IP 地址。

【讨论】:

    猜你喜欢
    • 2019-07-10
    • 2018-02-05
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-18
    相关资源
    最近更新 更多