Ping 和一般结构 C# - Visual Studio 2010

发布于 2024-12-10 03:57:11 字数 1699 浏览 0 评论 0原文

首先让我概述一下我的申请。我正在尝试使用 Visual C# 对用户指定的地址执行 PING。用户通过在文本框中输入他们想要 PING 的地址来与系统交互 - 然后用户单击 ping 按钮,该按钮将 ping 所需的地址,然后通过消息框将结果返回给用户。

这只是应用程序的初始阶段。

我对以下代码有问题:

using System;
using System.Collections.Generic;
using System.Net;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;

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

        private void pingButton_Click(object sender, EventArgs e)
        {
            int timeOut = 300;
            int ttl = 300;
            string stat = "", data = "[012345678901234567890123456789]";
            PingOptions pingOpts = new PingOptions();
            pingOpts.Ttl = ttl;
            pingOpts.DontFragment = true;
            Ping pinger = new Ping();
            PingReply reply = pinger.Send(pingAddressTextBox.Equals, timeOut, Buffer, pingOpts);
            if (reply.Status.ToString() != "Success")
                stat = "Failed";
            else
                stat = reply.Status.ToString();
            pinger.Dispose();
            MessageBox.Show("Congratulations!");
        }
    }
}

此代码来自有关堆栈溢出的另一个主题 - 我试图通过使代码正常工作然后修改它来获得理解。

错误是:

错误 1“System.Buffer”是“类型”,但像“变量”一样使用 错误 3 参数 1:无法从“方法组”转换为“System.Net.IPAddress”
错误 4 参数 3:无法从“System.Buffer”转换为“byte[]” 错误 2 'System.Net.NetworkInformation.Ping.Send(System.Net.IPAddress, int, byte[], System.Net.NetworkInformation.PingOptions)' 的最佳重载方法匹配有一些无效 正如我所说,

我只是在学习 - 这只是为了开怀大笑 - 任何帮助表示赞赏。

Initially let me give an overview of my application. I am attempting to use Visual C# to do a PING on an address the user specifies. The user interacts with the system by entering the address they wish to PING into the textbox - the user then clicks the pingButton which will ping the desired address and then return the results to the user via a message box.

This is just the initial stage of the application.

I am having problem with the following code:

using System;
using System.Collections.Generic;
using System.Net;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;

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

        private void pingButton_Click(object sender, EventArgs e)
        {
            int timeOut = 300;
            int ttl = 300;
            string stat = "", data = "[012345678901234567890123456789]";
            PingOptions pingOpts = new PingOptions();
            pingOpts.Ttl = ttl;
            pingOpts.DontFragment = true;
            Ping pinger = new Ping();
            PingReply reply = pinger.Send(pingAddressTextBox.Equals, timeOut, Buffer, pingOpts);
            if (reply.Status.ToString() != "Success")
                stat = "Failed";
            else
                stat = reply.Status.ToString();
            pinger.Dispose();
            MessageBox.Show("Congratulations!");
        }
    }
}

This code is from another topic on stack overflow - I am trying to gain an understanding by getting the code working and then modifying it.

The errors are:

Error 1 'System.Buffer' is a 'type' but is used like a 'variable'
Error 3 Argument 1: cannot convert from 'method group' to 'System.Net.IPAddress'
Error 4 Argument 3: cannot convert from 'System.Buffer'to 'byte[]'
Error 2 The best overloaded method match for 'System.Net.NetworkInformation.Ping.Send(System.Net.IPAddress, int, byte[], System.Net.NetworkInformation.PingOptions)' has some invalid
arguments

As I say I am just learning - this is just for a bit of a laugh - any help appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

絕版丫頭 2024-12-17 03:57:11

您需要一个 Buffer 类型的对象来传递给 pinger.Send

这里你只有类型名称。您需要一个实际的 byte 数组:

Ping pingSender = new Ping ();

// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes (data);

// Wait 10 seconds for a reply.
int timeout = 10000;

// Set options for transmission:
// The data can go through 64 gateways or routers
// before it is destroyed, and the data packet
// cannot be fragmented.
PingOptions options = new PingOptions (64, true);

// Send the request.
PingReply reply = pingSender.Send ("www.contoso.com", timeout, buffer, options);

这里 buffer 是从字符串创建的。你错过了这一步。

You need an object of type Buffer to pass to pinger.Send.

Here you just have the type name. You need an actual byte array:

Ping pingSender = new Ping ();

// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes (data);

// Wait 10 seconds for a reply.
int timeout = 10000;

// Set options for transmission:
// The data can go through 64 gateways or routers
// before it is destroyed, and the data packet
// cannot be fragmented.
PingOptions options = new PingOptions (64, true);

// Send the request.
PingReply reply = pingSender.Send ("www.contoso.com", timeout, buffer, options);

Source

Here buffer is created from the string. You're missing this step.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文