我想创建一个持续检查IP地址的程序 [英] I want to create a program that checking ip address continuously

查看:87
本文介绍了我想创建一个持续检查IP地址的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个持续检查IP地址的程序



i有一个检查ip地址和端口的代码,就像这样



I want to create a program that checking ip address continuously

i have a code that checking ip address and port, like this

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;

namespace WinNetworkIOCS {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void ConnectButton_Click(object sender, EventArgs e) {
            DisableFields();
            DoNetworkingConnection();
        }

        private void DisableFields() {
            PortBox.Enabled = false;
            IPAddressBox.Enabled = false;
            SendMessageBox.Enabled = false;
            ConnectButton.Enabled = false;
        }

        private void EnableFields() {
            PortBox.Enabled = true;
            IPAddressBox.Enabled = true;
            SendMessageBox.Enabled = true;
            ConnectButton.Enabled = true;
        }

        private void WriteToStatusBar(string Message) {
            //EnableFields();
            ThreadHelperClass.SetText(this, lblStatus, Message);
        }

        private void DoNetworkingConnection() {
            Thread MyThread = null;

            try {
                ThreadStart ThreadMethod = new ThreadStart(ConnectTo);
                MyThread = new Thread(ThreadMethod);
            } catch (Exception e) {
                WriteToStatusBar("Failed to create thread with error: " + e.Message);
                return;
            }

            try {
                MyThread.Start();
            } catch (Exception e) {
                WriteToStatusBar("The thread failed to start with error: " + e.Message);
            }
        }

        private void ConnectTo() {
            string ServerName = this.IPAddressBox.Text;
            int Port = System.Convert.ToInt32(this.PortBox.Text);

            WriteToStatusBar("IP Address: " + ServerName + "Port: " + Port);
            Socket ClientSocket = null;

            try {
                // Let's connect to a listening server
                try {
                    ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
                    WriteToStatusBar("Socket is OK...");
                } catch (Exception e) {
                    throw new Exception("Failed to create client Socket: " + e.Message);
                }

                IPEndPoint ServerEndPoint = new IPEndPoint(IPAddress.Parse(ServerName), Convert.ToInt16(Port));

                try {
                    ClientSocket.Connect(ServerEndPoint);
                    WriteToStatusBar("Connect() is OK...");
                } catch (Exception e) {
                    throw new Exception("Failed to connect client Socket: " + e.Message);
                }
            } catch (Exception e) {
                WriteToStatusBar(e.Message);
                ClientSocket.Close();
                return;
            }

            // Let's create a network stream to communicate over the connected Socket.
            NetworkStream ClientNetworkStream = null;

            try {
                try {
                    // Setup a network stream on the client Socket
                    ClientNetworkStream = new NetworkStream(ClientSocket, true);
                    WriteToStatusBar("Instantiating NetworkStream...");
                } catch (Exception e) {
                    // We have to close the client socket here because the network
                    // stream did not take ownership of the socket.
                    ClientSocket.Close();
                    throw new Exception("Failed to create a NetworkStream with error: " + e.Message);
                }

                StreamWriter ClientNetworkStreamWriter = null;

                try {
                    // Setup a Stream Writer
                    ClientNetworkStreamWriter = new StreamWriter(ClientNetworkStream);
                    WriteToStatusBar("Setting up StreamWriter...");
                } catch (Exception e) {
                    ClientNetworkStream.Close();
                    throw new Exception("Failed to create a StreamWriter with error: " + e.Message);
                }

                try {
                    ClientNetworkStreamWriter.Write(this.SendMessageBox.Text.ToString());
                    ClientNetworkStreamWriter.Flush();
                    WriteToStatusBar("We wrote " + this.SendMessageBox.Text.Length.ToString() + " character(s) to the server.");
                } catch (Exception e) {
                    throw new Exception("Failed to write to client NetworkStream with error: " + e.Message);
                }
            } catch (Exception e) {
                WriteToStatusBar(e.Message);
            } finally {
                // Close the network stream once everything is done
                ClientNetworkStream.Close();
            }
        }

        delegate void SetTextCallback(string text);
    }
}





但不会连续检查IP地址和端口。

我应该在编码中添加什么。





谢谢。



but does not check the IP address and port continuously.
What should I add in my coding.


Thank you.

推荐答案

嗯 - 你写了代码,不是吗?那么为什么不改变呢?你明白它是如何工作的......



你不是想连续不断地做而且你可能不想:因为如果你创造了一个循环连续运行该代码,它将阻止端口并导致您尝试使用它失败的任何其他因为端口将被使用。



我怀疑你从互联网上抓了一大堆随机代码,并且不知道如何处理它:软件开发不能像那样工作。在修改之前,您必须思考并理解您使用的代码。将随机代码块混合在一起并希望不是一个成功的开发策略。
Well - you wrote the code, didn't you? So why not change it? You understand how it works...

You aren't trying to "do it continuously" and probably, you don't want to: because if you create a loop to continuously run that code, it will block the port and cause whatever else you are trying to get to use it to fail because the port will be in use.

I suspect you have grabbed a lump of random code from the internet, and don't have a clue what to do with it: software development does not work like that. You have to think, and understand the code you use before you can modify it. Slamming lumps of random code together and hoping is not a successful development strategy.


这篇关于我想创建一个持续检查IP地址的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆