这段时间一直在学习C#, 以前一直搞网络的,还是从Ping程序的实现写起吧.
ping的调用方法如下:
    Ping mPing=new Ping();
    mPing.Pinging(“127.0.0.1“,255,65535);
    mPing.Receive(); //成功接收返回true,timeout 返回false
全部源代码如下:
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;
  public static string Address(ulong obj)
  {
   byte s1=(byte)obj;
   byte s2=(byte)(obj>>8);
   byte s3=(byte)(obj>>16);
   byte s4=(byte)(obj>>24);
   return String.Format("{0}.{1}.{2}.{3}",s1,s2,s3,s4);//s1+"."+s2+"."+s3+"."+s4;
  }
  public void Encode(BinaryWriter writer)
  {
   writer.Write(VIHL);
   writer.Write(TOS);
   writer.Write((Int16)TotLen);
   writer.Write((Int16)ID);
   writer.Write((Int16)FlagOff);
   writer.Write(TTL);
   writer.Write(Protocol);
   writer.Write((UInt16)Checksum);
   writer.Write((UInt32)iaSrc);
   writer.Write((UInt32)iaDst);
  }
  public void Decode(BinaryReader reader)
  {
   VIHL=reader.ReadByte();
   TOS=reader.ReadByte();
   TotLen=reader.ReadInt16();
   ID=reader.ReadInt16();
   FlagOff=reader.ReadInt16();
   TTL=reader.ReadByte();
   Protocol=reader.ReadByte();
   Checksum=reader.ReadUInt16();
   iaSrc=reader.ReadUInt32();
   iaDst=reader.ReadUInt32();
  }
 }
 //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.R