c#代码中的网络跟踪路由 [英] network trace route in c# code

查看:145
本文介绍了c#代码中的网络跟踪路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



i有一个网络traceroute的代码,但它无法正常工作。

程序返回无响应从远程主机错误消息,连续

有人可以帮助我吗?

代码:



string argv =217.20.130.97;



类ICMP

{

公共字节类型;

public byte Code;

public UInt16 Checksum;

public int MessageSize;

public byte [] Message = new byte [1024 ];

公共ICMP()

{

}

公共ICMP(byte []数据,int size )

{

类型=数据[20];

代码=数据[21];

校验和= BitConverter.ToUInt16(data,22);

MessageSize = size - 24;

Buffer.BlockCopy(data,24,Message,0,MessageSize);

}

public byte [] getBytes()

{

byte [] data = new byte [MessageSize + 9];

Buffer.BlockCopy(BitConverter.GetBytes(Type),0,data,0,1);

Buffer.BlockCopy(BitConverter.GetBytes(Code),0,data,1,1);

Buffer.BlockCopy(BitConverter.GetBytes(Checksum),0,data,2, 2);

Buffer.BlockCopy(消息,0,数据,4,MessageSize);

返回数据;

}



public UInt16 getChecksum()

{

UInt32 chcksm = 0;

byte [] data = getBytes();

int packetsize = MessageSize + 8;

int index = 0;

while(index<数据包)

{

chcksm + = Convert.ToUInt32(BitConverter.ToUInt16(data,index));

index + = 2;

}

chcksm =(chcksm>> 16)+(chcksm& 0xffff);

chcksm + =(chcksm>> ; 16);

返回(UInt16)(~chcksm);

}

}









byte [] data = new byte [1024];

int recv ,timestart,timestop;

套接字主机=新套接字(AddressFamily.InterNetwork,

SocketType.Raw,ProtocolType.Icmp);







IPEndPoint iep =新IPEndPoint(IPAddress.Parse(argv),0);

EndPoint ep =( EndPoint)iep;





ICMP包=新ICMP();

packet.Type = 0x08;

packet.Code = 0x00;

packet.Checksum = 0;

Buffer.BlockCopy(BitConverter。 GetBytes(1),0,packet.Message,0,2);

Buffer.BlockCopy(BitConverter.GetBytes(1),0,packet.Message,2,2);

data = Encoding.ASCII.GetBytes(test packet);

Buffer.BlockCopy(data,0,packet.Message,4,data.Length);

packet.MessageSize = data.Length + 4;

int packetsize = packet.MessageSize + 4;

UInt16 chcksum = packet.getChecksum();

packet.Checksum = chcksum;

host.SetSocketOption(SocketOptionLevel.Socket,

SocketOptionName.ReceiveTimeout,3000);

int badcount = 0;

for(int i = 1;我< 50; i ++)

{



host.SetSocketOption(SocketOptionLevel.IP,

SocketOptionName.IpTimeToLive,i);

timestart = Environment.TickCount;

host.SendTo(packet.getBytes(),packetsize,SocketFlags.None,iep);

试试

{

data = new byte [1024];

recv = host.ReceiveFrom(data,ref ep);

timestop = Environment.TickCount;

ICMP响应=新ICMP(数据,recv);

if(response.Type == 11)

label1.Text = label1.Text +\ n\\\
hop:ms的响应+ i + ep.ToString()+(timestop - timestart).ToString();

if(response.Type == 0)

{

label1.Text = label1.Text +\ n \ n在hops中到达,ms。 + ep.ToString()+ i +(timestop - timestart).ToString();

break;

}

badcount = 0;

}

catch(SocketException)

{

label2.Text =\ nhop:没有来自远程主机+ i;

badcount ++;

if(badcount == 5)

{

label2。 Text = label2.Text +\ n \\ n无法联系远程主机;

休息;

}

}

}

host.Close();



}

hi everyone!

i have a code for network traceroute, but it is not working.
the program return the "No response from remote host" error message, continuously
can anybody help me?
The code:

string argv = "217.20.130.97";

class ICMP
{
public byte Type;
public byte Code;
public UInt16 Checksum;
public int MessageSize;
public byte[] Message = new byte[1024];
public ICMP()
{
}
public ICMP(byte[] data, int size)
{
Type = data[20];
Code = data[21];
Checksum = BitConverter.ToUInt16(data, 22);
MessageSize = size - 24;
Buffer.BlockCopy(data, 24, Message, 0, MessageSize);
}
public byte[] getBytes()
{
byte[] data = new byte[MessageSize + 9];
Buffer.BlockCopy(BitConverter.GetBytes(Type), 0, data, 0, 1);
Buffer.BlockCopy(BitConverter.GetBytes(Code), 0, data, 1, 1);
Buffer.BlockCopy(BitConverter.GetBytes(Checksum), 0, data, 2, 2);
Buffer.BlockCopy(Message, 0, data, 4, MessageSize);
return data;
}

public UInt16 getChecksum()
{
UInt32 chcksm = 0;
byte[] data = getBytes();
int packetsize = MessageSize + 8;
int index = 0;
while (index < packetsize)
{
chcksm += Convert.ToUInt32(BitConverter.ToUInt16(data, index));
index += 2;
}
chcksm = (chcksm >> 16) + (chcksm & 0xffff);
chcksm += (chcksm >> 16);
return (UInt16)(~chcksm);
}
}




byte[] data = new byte[1024];
int recv, timestart, timestop;
Socket host = new Socket(AddressFamily.InterNetwork,
SocketType.Raw, ProtocolType.Icmp);



IPEndPoint iep = new IPEndPoint(IPAddress.Parse(argv), 0);
EndPoint ep = (EndPoint)iep;


ICMP packet = new ICMP();
packet.Type = 0x08;
packet.Code = 0x00;
packet.Checksum = 0;
Buffer.BlockCopy(BitConverter.GetBytes(1), 0, packet.Message, 0, 2);
Buffer.BlockCopy(BitConverter.GetBytes(1), 0, packet.Message, 2, 2);
data = Encoding.ASCII.GetBytes("test packet");
Buffer.BlockCopy(data, 0, packet.Message, 4, data.Length);
packet.MessageSize = data.Length + 4;
int packetsize = packet.MessageSize + 4;
UInt16 chcksum = packet.getChecksum();
packet.Checksum = chcksum;
host.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, 3000);
int badcount = 0;
for (int i = 1; i < 50; i++)
{

host.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.IpTimeToLive, i);
timestart = Environment.TickCount;
host.SendTo(packet.getBytes(), packetsize, SocketFlags.None, iep);
try
{
data = new byte[1024];
recv = host.ReceiveFrom(data, ref ep);
timestop = Environment.TickCount;
ICMP response = new ICMP(data, recv);
if (response.Type == 11)
label1.Text = label1.Text + "\n\nhop : response from ms" + i + ep.ToString() + (timestop - timestart).ToString();
if (response.Type == 0)
{
label1.Text = label1.Text + "\n\n reached in hops, ms." + ep.ToString() + i + (timestop - timestart).ToString();
break;
}
badcount = 0;
}
catch (SocketException)
{
label2.Text = "\nhop : No response from remote host" + i;
badcount++;
if (badcount == 5)
{
label2.Text = label2.Text + "\nUnable to contact remote host";
break;
}
}
}
host.Close();

}

推荐答案

hi everyone!
 
i have a code for network traceroute, but it is not working.
the program return the "No response from remote host" error message, continuously
can anybody help me?
The code:
 
string argv = "217.20.130.97";
 
class ICMP
{
public byte Type;
public byte Code;
public UInt16 Checksum;
public int MessageSize;
public byte[] Message = new byte[1024];
public ICMP()
{
}
public ICMP(byte[] data, int size)
{
Type = data[20];
Code = data[21];
Checksum = BitConverter.ToUInt16(data, 22);
MessageSize = size - 24;
Buffer.BlockCopy(data, 24, Message, 0, MessageSize);
}
public byte[] getBytes()
{
byte[] data = new byte[MessageSize + 9];
Buffer.BlockCopy(BitConverter.GetBytes(Type), 0, data, 0, 1);
Buffer.BlockCopy(BitConverter.GetBytes(Code), 0, data, 1, 1);
Buffer.BlockCopy(BitConverter.GetBytes(Checksum), 0, data, 2, 2);
Buffer.BlockCopy(Message, 0, data, 4, MessageSize);
return data;
}
 
public UInt16 getChecksum()
{
UInt32 chcksm = 0;
byte[] data = getBytes();
int packetsize = MessageSize + 8;
int index = 0;
while (index < packetsize)
{
chcksm += Convert.ToUInt32(BitConverter.ToUInt16(data, index));
index += 2;
}
chcksm = (chcksm >> 16) + (chcksm & 0xffff);
chcksm += (chcksm >> 16);
return (UInt16)(~chcksm);
}
}
 

 

byte[] data = new byte[1024];
int recv, timestart, timestop;
Socket host = new Socket(AddressFamily.InterNetwork,
SocketType.Raw, ProtocolType.Icmp);
 

 
IPEndPoint iep = new IPEndPoint(IPAddress.Parse(argv), 0);
EndPoint ep = (EndPoint)iep;
 

ICMP packet = new ICMP();
packet.Type = 0x08;
packet.Code = 0x00;
packet.Checksum = 0;
Buffer.BlockCopy(BitConverter.GetBytes(1), 0, packet.Message, 0, 2);
Buffer.BlockCopy(BitConverter.GetBytes(1), 0, packet.Message, 2, 2);
data = Encoding.ASCII.GetBytes("test packet");
Buffer.BlockCopy(data, 0, packet.Message, 4, data.Length);
packet.MessageSize = data.Length + 4;
int packetsize = packet.MessageSize + 4;
UInt16 chcksum = packet.getChecksum();
packet.Checksum = chcksum;
host.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, 3000);
int badcount = 0;
for (int i = 1; i < 50; i++)
{
 
host.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.IpTimeToLive, i);
timestart = Environment.TickCount;
host.SendTo(packet.getBytes(), packetsize, SocketFlags.None, iep);
try
{
data = new byte[1024];
recv = host.ReceiveFrom(data, ref ep);
timestop = Environment.TickCount;
ICMP response = new ICMP(data, recv);
if (response.Type == 11)
label1.Text = label1.Text + "\n\nhop : response from ms" + i + ep.ToString() + (timestop - timestart).ToString();
if (response.Type == 0)
{
label1.Text = label1.Text + "\n\n reached in hops, ms." + ep.ToString() + i + (timestop - timestart).ToString();
break;
}
badcount = 0;
}
catch (SocketException)
{
label2.Text = "\nhop : No response from remote host" + i;
badcount++;
if (badcount == 5)
{
label2.Text = label2.Text + "\nUnable to contact remote host";
break;
}
}
}
host.Close();
 
}


这篇关于c#代码中的网络跟踪路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆