This period of time has been learning C #, has been engaged in the network, or from the implementation of the ping program to write it.
The method of calling Ping is as follows:
Ping mping=new Ping ();
Mping.pinging ("127.0.0.1", 255,65535);
Mping.receive (); Successfully received return True,timeout return False
All source code is as follows:
Using System;
Using System.IO;
Using System.Net;
Using System.Net.Sockets;
Namespace Ping
{
<summary>
Summary description for Ping.
</summary>
///
//
//
IP Header
public class Iphdr
{
Public byte VIHL
{
Get{return MVIHL;}
Set{mvihl=value;}
}private byte Mvihl;
Public byte TOS
{
Get{return MTOs;}
Set{mtos=value;}
}private byte MTOs;
public short Totlen
{
Get{return Mtotlen;}
Set{mtotlen=value;}
}private short Mtotlen;
public short ID
{
Get{return MID;}
Set{mid=value;}
}private short MID;
public short Flagoff
{
Get{return Mflagoff;}
Set{mflagoff=value;}
}private short Mflagoff;
Public byte TTL
{
Get{return Mttl;}
Set{mttl=value;}
}private byte Mttl;
Public byte Protocol
{
Get{return Mprotocol;}
Set{mprotocol=value;}
}private byte Mprotocol;
Public ushort Checksum
{
Get{return Mchecksum;}
Set{mchecksum = value;}
}private ushort Mchecksum;
Public ULONG IASRC
{
Get{return miasrc;}
Set{miasrc=value;}
}private ULONG Miasrc;
Public ULONG IADST
{
Get{return MIADST;}
Set{miadst=value;}
}private ULONG MIADST;
}
ICMP Header;
public class Icmphdr
{
Public byte Type
{
Get{return Mtype;}
Set{mtype=value;}
}private byte Mtype;
Public byte Code
{
Get{return Mcode;}
Set{mcode=value;}
}private byte mcode=0;
Public ushort Checksum
{
Get{return Mchecksum;}
Set{mchecksum=value;}
}private ushort Mchecksum=0;
Public ushort ID
{
Get{return MID;}
Set{mid=value;}
}private ushort MID;
Public ushort Seq
{
Get{return Mseq;}
Set{mseq=value;}
}private ushort Mseq;
Public ULONG Tmsend
{
Get{return Mtmsend;}
Set{mtmsend=value;}
}private ULONG Mtmsend;
public int Ntaskid
{
Get{return Mntaskid;}
Set{mntaskid=value;}
}private int mntaskid;
public void Encode (BinaryWriter writer)
{
Writer. Write (Type);
Writer. Write (Code);
Writer. Write ((UInt16) Checksum);
Writer. Write ((UInt16) ID);
Writer. Write ((UInt16) Seq);
Writer. Write ((UInt32) tmsend);
Writer. Write (Ntaskid);
}
public void Decode (BinaryReader reader)
{
Type=reader. ReadByte ();
Code=reader. ReadByte ();
Checksum=reader. ReadUInt16 ();
Id=reader. ReadUInt16 ();
Seq=reader. ReadUInt16 ();
Tmsend=reader. ReadUInt32 ();
Ntaskid=reader. ReadInt32 ();
}
Public UINT Sum ()
{
UINT Sum=0;
Sum + = (ushort) (type+ (code<<8));
Sum + = (ushort) ID;
Sum + = (ushort) Seq;
Sum + = (ushort) tmsend;
Sum + = (ushort) (tmsend>>16);
Sum + = (ushort) ntaskid;
Sum + = (ushort) (ntaskid>>16);
return sum;
}
}
public class Echorequest
{
Private char[] Mchar;
Public Icmphdr icmp=new ICMPHDR ();
Public echorequest (int size,char NChar)
{
Mchar=new Char[size];
for (int i=0;i<size;i++)
Mchar[i]=nchar;
}
public void Encode (BinaryWriter writer)
{
Chksum ();
Icmp. Encode (writer);
Writer. Write (Mchar);
}
/* public void Decode (BinaryReader reader)
{
Icmp. Decode (reader);
String S=reader. ReadString ();
Mchar=s.tochararray ();
}
* private void Chksum ()
{
UINT SUM=ICMP. Sum ();
for (int i=0;i<mchar.length;i+=2)
Sum + = (ushort) (mchar[i]+ (mchar[i+1]<<8));
//
sum = (sum >>) + (sum & 0xFFFF); Add hi to Low 16
sum + = (sum >> 16); Add Carry
Short answer = (short) ~sum; Truncate to Bits
Icmp. checksum= (ushort) answer;
}
}
ICMP Echo Reply
public class Echoreply
{
Public IPHDR Iphdr=null;
Public ICMPHDR Icmphdr=null;
Public char[] Cfiller;
public void Decode (BinaryReader reader)
{
Iphdr=new Iphdr ();
Iphdr.decode (reader);
Icmphdr=new Icmphdr ();
Icmphdr.decode (reader);
int bytes= (int) reader. Basestream.length;
Cfiller=reader. ReadChars (8);
Cfiller=reader. ReadChars (bytes-36);
}
}
public class StateObject
{
Public Socket worksocket = null; Client socket.
public const int buffersize = 256; Size of receive buffer.
Public byte[] buffer = new Byte[buffersize]; Receive buffer.
Public StringBuilder sb = new StringBuilder ();//Received data string.
}
public class Ping
{
Socket Socket=null;
int m_id;
UINT M_taskid;
UINT M_seq;
System.Threading.ManualResetEvent Recvdone=null;
DateTime M_dtsend;
Public Ping ()
{
m_seq=0;
Recvdone=new System.Threading.ManualResetEvent (FALSE);
Socket=new Socket (ADDRESSFAMILY.INTERNETWORK,SOCKETTYPE.RAW,PROTOCOLTYPE.ICMP);
//
Todo:add constructor Logic here
//
}
public bool Pinging (string Addr,int ID, uint taskid)
{
Try
{
M_id=id;
M_taskid=taskid;
Byte[] Byreq =fillechoreq ();
Send To
IPEndPoint LEP = new IPEndPoint (ipaddress.parse (addr), 0);
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.