gpt4 book ai didi

c# - Ping 和通用结构 C# - Visual Studio 2010

转载 作者:太空宇宙 更新时间:2023-11-03 14:11:20 24 4
gpt4 key购买 nike

首先让我概述一下我的申请。我正在尝试使用 Visual C# 对用户指定的地址执行 PING。用户通过在文本框中输入他们希望 PING 的地址来与系统交互——然后用户单击 pingButton,它将 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)' 的最佳重载方法匹配有一些无效 参数

正如我所说的,我只是在学习 - 这只是为了开个玩笑 - 感谢任何帮助。

最佳答案

您需要一个 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);

Source

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

关于c# - Ping 和通用结构 C# - Visual Studio 2010,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7773999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com