C语言、c++、c#都可以,比如:ping baidu.com 并将ping的结果保存于string...

发布网友 发布时间:2024-09-27 02:33

我来回答

2个回答

热心网友 时间:8分钟前

C++ Ping
/*
#include <windows.h>
#include <winsock2.h>
#include <iphlpapi.h>
#pragma comment (lib, "ws2_32.lib" )
#pragma comment (lib, "Iphlpapi.lib")
*/
DWORD WINAPI PingThread(LPVOID lParam)
{
int n = (int)(INT_PTR)lParam;
IPAddr ip = inet_addr(%%1) + (n << 24); //"192.168.0.0"
BYTE mac[8];
ULONG len = sizeof(mac);
if (SendARP(ip, 0, (PULONG)mac, &len) == NO_ERROR)
{
//printf("192.168.0.%d : %02X-%02X-%02X-%02X-%02X-%02X\n", n, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}
return 0;
}

HANDLE h[255];
for (int i=1; i<255; i++)
{
h[i] = CreateThread(NULL, 0, PingThread, (PVOID)(INT_PTR)i, 0, NULL);
}
for (int j=1; j<255; j++)
{
WaitForSingleObject(h[j], -1);
CloseHandle(h[j]);
}

C# Ping
//using System.Net;
NetworkInformation.Ping p = new NetworkInformation.Ping();
NetworkInformation.PingOptions options = new NetworkInformation.PingOptions();
options.DontFragment = true;
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
//Wait seconds for a reply.
int timeout = 4000
NetworkInformation.PingReply reply = p.Send(%%1, timeout, buffer, options);
%%2=reply.Status;

走什么路,自己选!

热心网友 时间:7分钟前

using System.Diagnostics;

Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine("ping www.163.com");
p.StandardInput.WriteLine("exit");
string str = p.StandardOutput.ReadToEnd();
p.WaitForExit();

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com