C#中实现Ping

C# 如何telnet IP的某端口/ping 是否通 //检查是否ping通 private bool checkPingEnable(string _ip) { bool _isEnable = false; try { Ping pingSender = new Ping(); PingReply reply = pingSender.Send(_ip, 120);//第一个参数为ip地址. 阅读详情

在C#中实现Ping,代码如下:

using System;
using System.Net;
using System.Net.Sockets;
///ping.cs
namespace  SystemMonitor {
///
///  主要的类:ping
///
class  Ping {
//声明几个常量
const  int  SOCKET_ERROR  =  -1;
const  int  ICMP_ECHO  =  8;
///
///  这里取得Hostname参数
///
public  static  void  PingUrl(string[]  argv) {
if(argv.Length==0) {
//If  user  did  not  enter  any  Parameter  inform  him
Console.WriteLine("Usage:Ping    /r")  ;
Console.WriteLine("  The  name  of  the  Host  who  you  want  to  ping");
Console.WriteLine("/r  Ping  the  host  continuously")  ;
}
else  if(argv.Length==1) {
//Just  the  hostname  provided  by  the  user
//call  the  method  "PingHost"  and  pass  the  HostName  as  a  parameter
PingHost(argv[0])  ;
}
else  if(argv.Length==2) {
//the  user  provided  the  hostname  and  the  switch
if(argv[1]=="/r") {
//loop  the  ping  program
while(true) {
//call  the  method  "PingHost"  and  pass  the  HostName  as  a  parameter
PingHost(argv[0])  ;
}
}
else {
//if  the  user  provided  some  other  switch
PingHost(argv[0])  ;
}
}
else {
//Some  error  occurred
Console.WriteLine("Error  in  Arguments")  ;
}
}
///
///  主要的方法,用来取得IP,
///  并计算响应时间
///
public  static  void  PingHost(string  host) {
//Declare  the  IPHostEntry
IPHostEntry  serverHE,  fromHE;
int  nBytes  =  0;
int  dwStart  =  0,  dwStop  =  0;
//Initilize  a  Socket  of  the  Type  ICMP
Socket  socket  =
new  Socket(AddressFamily.InterNetwork,  SocketType.Raw,  ProtocolType.Icmp);
//  Get  the  server  endpoint
try {
serverHE  =  Dns.GetHostByName(host);
}
catch(Exception) {
Console.WriteLine("Host  not  found");  //  fail
return  ;
}
//  Convert  the  server  IP_EndPoint  to  an  EndPoint
IPEndPoint  ipepServer  =  new  IPEndPoint(serverHE.AddressList[0],  0);
EndPoint  epServer  =  (ipepServer);
//  Set  the  receiving  endpoint  to  the  client  machine
fromHE  =  Dns.GetHostByName(Dns.GetHostName());
IPEndPoint  ipEndPointFrom  =  new  IPEndPoint(fromHE.AddressList[0],  0);
EndPoint  EndPointFrom  =  (ipEndPointFrom);
int  PacketSize  =  0;
IcmpPacket  packet  =  new  IcmpPacket();
//  Construct  the  packet  to  send
packet.Type  =  ICMP_ECHO;  //8
packet.SubCode  =  0;
packet.CheckSum  =  UInt16.Parse("0");
packet.Identifier  =  UInt16.Parse("45");
packet.SequenceNumber  =  UInt16.Parse("0");
int  PingData  =  32;  //  sizeof(IcmpPacket)  -  8;
packet.Data  =  new  Byte[PingData];
//Initilize  the  Packet.Data
for  (int  i  =  0;  i  <  PingData;  i++) {
packet.Data[i]  =  (byte)'#';
}
//Variable  to  hold  the  total  Packet  size
PacketSize  =  PingData  +  8;
Byte  []  icmp_pkt_buffer  =  new  Byte[  PacketSize  ];
Int32  Index  =  0;
//Call  a  Method  Serialize  which  counts
//The  total  number  of  Bytes  in  the  Packet
Index  =  Serialize(
packet,
icmp_pkt_buffer,
PacketSize,
PingData  );
//Error  in  Packet  Size
if(  Index  ==  -1  ) {
Console.WriteLine("Error  in  Making  Packet");
return  ;
}
//  now  get  this  critter  into  a  UInt16  array
//Get  the  Half  size  of  the  Packet
Double  double_length  =  Convert.ToDouble(Index);
Double  dtemp  =  Math.Ceiling(  double_length  /  2);
int  cksum_buffer_length  =  Convert.ToInt32(dtemp);
//Create  a  Byte  Array
UInt16  []  cksum_buffer  =  new  UInt16[cksum_buffer_length];
//Code  to  initialize  the  Uint16  array
int  icmp_header_buffer_index  =  0;
for(  int  i  =  0;  i  <  cksum_buffer_length;  i++  )  {
cksum_buffer[i]  =
BitConverter.ToUInt16(icmp_pkt_buffer,icmp_header_buffer_index);
icmp_header_buffer_index  +=  2;
}
//Call  a  method  which  will  return  a  checksum
UInt16  u_cksum  =  checksum(cksum_buffer,  cksum_buffer_length);
//Save  the  checksum  to  the  Packet
packet.CheckSum  =  u_cksum;
//  Now  that  we  have  the  checksum,  serialize  the  packet  again
Byte  []  sendbuf  =  new  Byte[  PacketSize  ];
//again  check  the  packet  size
Index  =  Serialize(
packet,
sendbuf,
PacketSize,
PingData  );
//if  there  is  a  error  report  it
if(  Index  ==  -1  ) {
Console.WriteLine("Error  in  Making  Packet");
return  ;
}
dwStart  =  System.Environment.TickCount;  //  Start  timing
//send  the  Pack  over  the  socket
if  ((nBytes  =  socket.SendTo(sendbuf,  PacketSize,  0,  epServer))  ==  SOCKET_ERROR) {
Console.WriteLine("Socket  Error  cannot  Send  Packet");
}
//  Initialize  the  buffers.  The  receive  buffer  is  the  size  of  the
//  ICMP  header  plus  the  IP  header  (20  bytes)
Byte  []  ReceiveBuffer  =  new  Byte[256];
nBytes  =  0;
//Receive  the  bytes
bool  recd  =false  ;
int  timeout=0  ;
//loop  for  checking  the  time  of  the  server  responding
while(!recd) {
nBytes  =  socket.ReceiveFrom(ReceiveBuffer,  256,  0,  ref  EndPointFrom);
if  (nBytes  ==  SOCKET_ERROR) {
Console.WriteLine("Host  not  Responding")  ;
recd=true  ;
break;
}
else  if(nBytes>0) {
dwStop  =  System.Environment.TickCount  -  dwStart;  //  stop  timing
Console.WriteLine("Reply  from  "+epServer.ToString()+"  in  "
+dwStop+"MS  :Bytes  Received"+nBytes);
recd=true;
break;
}
timeout=System.Environment.TickCount  -  dwStart;
if(timeout>1000) {
Console.WriteLine("Time  Out")  ;
recd=true;
}
}
//close  the  socket
socket.Close();
}
///
///  This  method  get  the  Packet  and  calculates  the  total  size
///  of  the  Pack  by  converting  it  to  byte  array
///
public  static  Int32  Serialize(IcmpPacket  packet,  Byte[]  Buffer,
Int32  PacketSize,  Int32  PingData  ) {
Int32  cbReturn  =  0;
//  serialize  the  struct  into  the  array
int  Index=0;
Byte  []  b_type  =  new  Byte[1];
b_type[0]  =  (packet.Type);
Byte  []  b_code  =  new  Byte[1];
b_code[0]  =  (packet.SubCode);
Byte  []  b_cksum  =  BitConverter.GetBytes(packet.CheckSum);
Byte  []  b_id  =  BitConverter.GetBytes(packet.Identifier);
Byte  []  b_seq  =  BitConverter.GetBytes(packet.SequenceNumber);
//  Console.WriteLine("Serialize  type  ");
Array.Copy(  b_type,  0,  Buffer,  Index,  b_type.Length  );
Index  +=  b_type.Length;
//  Console.WriteLine("Serialize  code  ");
Array.Copy(  b_code,  0,  Buffer,  Index,  b_code.Length  );
Index  +=  b_code.Length;
//  Console.WriteLine("Serialize  cksum  ");
Array.Copy(  b_cksum,  0,  Buffer,  Index,  b_cksum.Length  );
Index  +=  b_cksum.Length;
//  Console.WriteLine("Serialize  id  ");
Array.Copy(  b_id,  0,  Buffer,  Index,  b_id.Length  );
Index  +=  b_id.Length;
Array.Copy(  b_seq,  0,  Buffer,  Index,  b_seq.Length  );
Index  +=  b_seq.Length;
//  copy  the  data
Array.Copy(  packet.Data,  0,  Buffer,  Index,  PingData  );
Index  +=  PingData;
if(  Index  !=  PacketSize/*  sizeof(IcmpPacket)  */)  {
cbReturn  =  -1;
return  cbReturn;
}
cbReturn  =  Index;
return  cbReturn;
}
///
///  This  Method  has  the  algorithm  to  make  a  checksum
///
public  static  UInt16  checksum(  UInt16[]  buffer,  int  size  ) {
Int32  cksum  =  0;
int  counter;
counter  =  0;
while  (  size  >  0  )  {
UInt16  val  =  buffer[counter];
cksum  +=  Convert.ToInt32(  buffer[counter]  );
counter  +=  1;
size  -=  1;
}
cksum  =  (cksum  >>  16)  +  (cksum  &  0xffff);
cksum  +=  (cksum  >>  16);
return  (UInt16)(~cksum);
}
}  //  class  ping
///
///  Class  that  holds  the  Pack  information
///
public  class  IcmpPacket {
public  Byte  Type;  //  type  of  message
public  Byte  SubCode;  //  type  of  sub  code
public  UInt16  CheckSum;  //  ones  complement  checksum  of  struct
public  UInt16  Identifier;  //  identifier
public  UInt16  SequenceNumber;  //  sequence  number
public  Byte  []  Data;
}  //  class  IcmpPacket
}  
C#中实现定时器Timer定时判断IP是否ping通(连通)和端口号是否telnet可达(可用) 在上面的基础上,将定时器定时调用http接口改为定时判断某ip是否联通,端口是否可用。 阅读详情

相关推荐

C#网络诊断工具:Ping的实现

Ping类位于命名空间下,它的主要功能是检测目标主机是否可达,并能够测量往返时间(Round-Trip Time, RTT)。这个类提供了一种简单的方式来进行网络诊断测试,比如检测网络连接是否正常、网络延迟等情况。在实际应用中,Ping请求可能会由于多种原因失败,如网络超时、目标主机不可达,或本地权限不足等问题。此外,为了适应不同的网络环境和诊断需求,我们可能需要对Ping请求的参数进行自定义,例如设置缓冲区大小、超时时间和重试次数等。

weixin_42629522的博客 844

c#调用ping命令

c#调用ping命令

anlog的专栏 1756

C#多线程ping和DOS工具

本程序采用VS2010软件开发,用的是C#语言,提供源码,主要功能如下 1, 可持续ping 1个IP地址或多个IP地址,可选择记录在指定的文件或自动生成文件记录或不记录。可查看IP什么时候丢包。保存的文件的路径也会做相应的记录,下次重启也不会丢失 2 可对某一网段的IP地址利用ping协议测试连通性,如192.168.1.1 - 192.168.1.254(处理的IP数254) 或者 192.168.1.1 - 192.168.254.244(像这处理的IP数6万多个) 1秒钟大约可以测出50个IP地址的连通性,用的是多线程技术,如果用单线就太慢了,对ping完的结果做了相应的处理 3 调用了系统的DOS程序,可以处理相应的DOS命令,如"ipconig /all,ping www.baidu.com" 程序也内置了简单的几个DOS命令做演示,同样也方便操作 4 对输入的内容可提取所有的IP地址, 并对测出所有提取IP地址的连通性, 通不与不通的IP地址将以不同的颜色显示并做相应的处理。 也可提取有重叠的IP地址 5 调用了系统的DOS程序,可以处理相应的DOS命令,如"ipcongi /all,ping www.baidu.com" 程序也内置了简单的几个DOS命令做演示,同样也方便操作 6 本程序还提供了相关C#的经典视频教程链接!欢迎下载!

C#实现通用对IP地址的检查是否合法与Ping指定IP是否畅通的

一、实现功能 ①实现检查IP地址、端口是否合法 ②实现Ping指定的IP地址是否畅通 二、核心脚本 /*** * Title:"三维可视化" 项目 * 主题:【公共层】IP信息检查 * Description: * 功能: * 1、检查IP地址是否合法 * 2、检查端口是否合法 * * Date:2020 * Version:0.1版本 * Author:Coffee * Modify Recoder: */ using System; using Syste

提供C#相关开发 、云计算、运维测试、网络安全相关技术分享与服务,有意向可联系。 2085

C# .Net 设置固定IP、测试端口、获取本机ip、ping 地址。

基于 .net framework 4.6 的winfrom 小程序 功能有 获取本机IP 设置本机IP配置为自动获取或者手动设置 测试端口是否可访问 Ping IP 功能实现通过用Socket类、Process类、来调用命令行程序测试和Socket访问端口。 温习知识写的小工具,如果有bug,那么理解万岁

C#实现ping服务器检测并返回结果支持循环扫描

C#实现对服务器的 ping处理,并且返回相应结果,支持单次ping和无限ping 开发工具:vs2010 程序为本人写,绝对正确,如有发现运行不起来,请与本人联系。 邮箱:654054668@qq.com

在C#中实现Ping

Console.WriteLine("正在 Ping " + MyUrl + " ……Console.WriteLine("请输入要 Ping 的IP或者主机名字:");// 把 Server IP_EndPoint转换成EndPoint。return "主机没有响应";// 得到Server EndPoint。return "没有发现主机";// 声明 IPHostEntry。// 初始化Packet.Data。//初始化ICMP的Socket。/// Ping类。// 构建要发送的包。

biyusr的专栏 1445

STM32G030C8T6 ADC+DMA实战:同时采集外部电压和芯片温度的完整代码流程

本文详细介绍了STM32G030C8T6芯片通过ADC+DMA实现外部电压和芯片内部温度采集的完整流程。从硬件设计到CubeMX配置,再到驱动模块化实现与系统优化,提供了高精度多通道采集系统的实战指南,特别适合嵌入式开发者在工业控制和环境监测项目中应用。

weixin_42653691的博客 474

在c#中实现ping

在c#中实现pingusing System;using System.Collections;using System.Data;using System.Net.Sockets;using System.Net;/// Ping类public class Ping{//声明常量const int SOCKET_ERROR = -1;const int ICMP_ECHO

SunTojava的专栏 839

同步辐射材料科学应用指南:从XAFS原位实验到数据处理

材料结构分析是材料研究的核心环节,但传统实验室X射线受限于能量固定、亮度不足,往往难以回答原子局域配位、非晶结构及动态演变等关键问题。同步辐射光源以高通量、连续可调、偏振可控及脉冲时间结构等特性,将材料表征从静态观察推进到原位动态追踪层次。基于X射线吸收精细结构谱(XAFS)、原位实验、X射线衍射与散射、微区成像等技术,研究人员可实现元素选择性分析、近邻原子环境解析及真实工况下的结构演变监测。本文从技术原理出发,覆盖实验方案设计、样品制备、现场对光、数据处理与常见故障排查,并结合典型应用场景,为材料科学领域

weixin_32612263的博客 204

Mac上安装虚拟机搭载window系统

用Mac的都知道,很多window用的软件是Mac版本没有的。平时有的还得要求去用那些软件,有几种方式可供大家选择: 1.再买个window本; 2.装双系统; 3.装虚拟机,搭载window系统。 我比较推荐第3条,这样可以在Mac本上做到双开,一边开着mac一边开着window,还能做到同时处理文件,不至于重新启动操作系统,下边是具体实现步骤,其中有几个需要下载的,建议在网速好的时候完...

Coder_Mumu的博客 1万+

c#下实现ping操作

这里我写的是一个窗体程序。首先添加textbox,listbox,button控件,其中textbox录入域名或IP,listbox显示结果.   在button1_click事件键入   private void button1_Click(object sender, EventArgs e)   {   Ping p1 = new Ping(); //只是演示,没有做错误处理

pplsunny----舞动青春 1808

mapbox高阶,加载倾斜摄影

本文详细介绍如何基于mapbox-gl v3.8.0版本实现倾斜摄影的加载,亲测可用。希望能帮助到您。一起学习,加油!加油!

我哎GIS博客 353

C#下实现ping功能

这段时间一直在学习C#, 以前一直搞网络的,还是从Ping程序的实现写起吧.ping的调用方法如下:    Ping mPing=new Ping();    mPing.Pinging(“127.0.0.1“,255,65535);    mPing.Receive(); //成功接收返回true,timeout 返回false全部源代码如下:using System;using System.

silverwgy的专栏 1190

099、多摄融合与变焦系统:广角到长焦的无缝切换算法

本文分享了多摄融合与变焦系统开发中的实战经验和关键挑战。作者通过一个旗舰机项目案例,揭示了广角到长焦切换时画面抖动问题的根源——融合算法误将视差当作运动补偿,并提出"视差冻结窗口"的解决方案。文章总结了三大多摄融合难点:物理视差处理、亮度差异补偿和切换时机把控,详细阐述了分层架构设计、融合权重计算等核心技术。同时强调了产线校准的重要性,并给出五项实用建议:允许边缘鬼影保留、参数场景化配置、加强产线检测、完善调试接口,以及合理运用AI技术。全文展现了多摄融合技术在时间、空间和感知维度上的复杂平衡艺术。

sundong_81的博客 56

【Bug已解决】Codex 报错 MCP client for context7 failed to start: program not found 解决方案

方案适用场景推荐指数改用绝对路径Windows 上最常见、最有效的解决方式⭐⭐⭐⭐⭐提前全局安装避免依赖 npx减少运行时网络下载的不确定性⭐⭐⭐⭐使用 .cmd 后缀Windows 平台快速尝试⭐⭐⭐⭐开启调试日志需要更精确定位失败原因⭐⭐⭐⭐统一封装启动脚本需要跨平台兼容多个 MCP 服务⭐⭐⭐报错的本质是Codex 启动 MCP 子进程时,无法正确定位配置文件中写的简写命令名对应的可执行文件,尤其容易发生在 Windows 平台上使用npx这类简写命令的场景。

一键难忘的博客 5819

C# 实现Ping

   核心代码:using System;using System.Collections.Generic;using System.Drawing;using System.Windows.Forms;using System.Net.NetworkInformation;using System.Text; //....     //arg为主机IP,timeout

yi861801的专栏 284

移位寄存器——用Verilog实现串入串出、串并和并串转换并进行Modelsim仿真

用Verilog实现串并转换 首先我们先要了并串和串并转换的原理 并串转换:先将八位数据暂存于一个八位寄存器器中,然后左移输出到一位输出端口,这里可以通过一个“移位”来实现,相当于实现了移位寄存器的功能。 串并转换:新输入的位值成为原来数据的最低位,将原来数据的最高位舍去,这里可以通过一个简单的“连接符”来实现。 我们这里采用两种方式来进行串并转换 一:lsb优先 Least Significant Bit,最低比特,最低位优先 二:msb优先 Most Significant Bit,最高比特,

H19981118的博客 2万+

C# 实现的Ping功能

 using System;using System.Drawing;using System.Collections;using System.Data;using System.Net;using System.Net.Sockets;///  Ping类public class Ping{    //声明常量    const int SOCKET_ERROR = -1;    cons

坚持,才是力量.老实做人,踏实做事. 1300

【ArcGIS微课1000例】0145:如何按照自定义形状裁剪数据框?

使用绘图 工具条上的新建圆工具 可创建一个椭圆,使其包含要在该数据框中显示的数据范围。. 在基础工具工具条上单击选择元素工具 ,然后选择圆形。. 在内容列表中右键单击要裁剪的数据框,然后单击属性。. 单击裁剪选项下拉菜单,然后选择裁剪至形状。. 单击数据框属性 对话框中的数据框选项卡。还可以更改裁剪形状轮廓的边框选项。打开软件,添加配套实验数据包中。修改椭圆的属性,使其透明。. 单击所选图形的轮廓。. 单击指定形状按钮。

智慧空间实验室 965

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

c# 下实现ping 命令操作

1>通过.net提供的类实现 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using System.Diagnostics; 7 using System.Net.NetworkInformatio...

336

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

C# ping的实现(非CMD调用)

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.NetworkInformation; namespace _20通过编程方式实现Ping { class Program {

mengfick的专栏 820

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

上一篇: [转][小说]我是一名黑客
下一篇: WMI问题答案集锦
JavaK
博客等级 码龄22年 18粉丝 2原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址