我想创建一个持续检查IP地址的程序。
我有一个检查IP地址和端口的代码,如下所示:
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地址和端口。 在我的代码中添加什么来实现这一目标?
答案 0 :(得分:0)
我假设您的意思是您希望能够多次运行您的程序,而不是您希望它一遍又一遍地检查,因此您的问题实际上是您的程序完成了ping的任务但从不允许你输入新的信息?
这是因为你的功能
private void ConnectButton_Click(object sender, EventArgs e) {
DisableFields();
DoNetworkingConnection();
}
您运行函数DisableFields();和DoNetworkingConnection();假设DoNeworkingConnection();工作,它运行代码一次然后你的问题是你从来没有再次启用你的控件,因为在
private void WriteToStatusBar(string Message) {
//EnableFields();
ThreadHelperClass.SetText(this, lblStatus, Message);
}
您已注释掉EnableFields();重新启用您的控件。如果情况并非如此,并且您实际上是在尝试使其连续运行,则必须在while循环中包装ConnectTo()的发送部分
while(true)
{
ClientNetworkStreamWriter.Write(this.SendMessageBox.Text.ToString());
ClientNetworkStreamWriter.Flush();
WriteToStatusBar("We wrote " + this.SendMessageBox.Text.Length.ToString() + " character(s) to the server.");
}
虽然如果是这种情况,请说明你想要实现的目标,你可能会得到一个更好的解决方案,因为这会很快变成DOS攻击......