CÁC GIẢI PHÁP LẬP TRÌNH C# - Trang 449

449

Chương 11: Lập trình mạng

// Thiết lập endpoint cho máy client.

IPHostEntry fromHE = Dns.GetHostByName(Dns.GetHostName());

IPEndPoint ipEndPointFrom =

new IPEndPoint(fromHE.AddressList[0], 0);

EndPoint EndPointFrom = (ipEndPointFrom);

// Tạo packet.

int PacketSize = 0;

IcmpPacket packet = new IcmpPacket();

for (int j = 0; j < 1; j++) {

packet.Type = ICMP_ECHO;

packet.SubCode = 0;

packet.CheckSum = UInt16.Parse("0");

packet.Identifier = UInt16.Parse("45");

packet.SequenceNumber = UInt16.Parse("0");

int PingData = 32;

packet.Data = new Byte[PingData];

for (int i = 0; i < PingData; i++)

packet.Data[i] = (byte)'#';

PacketSize = PingData + 8;

Byte [] icmp_pkt_buffer = new Byte [PacketSize];

int index = 0;

index = Serialize(packet,

icmp_pkt_buffer, PacketSize, PingData);

// Tính checksum cho packet.

double double_length = Convert.ToDouble(index);

double dtemp = Math.Ceiling(double_length / 2);

int cksum_buffer_length = Convert.ToInt32(dtemp);

UInt16[] cksum_buffer = new UInt16[cksum_buffer_length];

int icmp_header_buffer_index = 0;

for (int i = 0; i < cksum_buffer_length; i++) {