C 下實現ping功能

2021-04-02 23:58:54 字數 3107 閱讀 7373

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

set

}private byte mvihl;

public byte tos

set

}private byte mtos;

public short totlen

set

}private short mtotlen;

public short id

set

}private short mid;

public short flagoff

set

}private short mflagoff;

public byte ttl

set

}private byte mttl;

public byte protocol

set

}private byte mprotocol;

public ushort checksum

set

}private ushort mchecksum;

public ulong iasrc

set

}private ulong miasrc;

public ulong iadst

set

}private ulong miadst;

public static string address(ulong obj)

...",s1,s2,s3,s4);//s1+"."+s2+"."+s3+"."+s4;

} public void encode(binarywriter writer)

public void decode(binaryreader reader) }

//icmp header;

public class icmphdr

set

}private byte mtype;

public byte code

set

}private byte mcode=0;

public ushort checksum

set

}private ushort mchecksum=0;

public ushort id

set

}private ushort mid;

public ushort seq

set

}private ushort mseq;

public ulong tmsend

set

}private ulong mtmsend;

public int ntaskid

set

}private int mntaskid;

public void encode(binarywriter writer)

public void decode(binaryreader reader)

public uint sum() }

public class echorequest }

//icmp echo reply

public class echoreply }

public class stateobject

public class ping

public bool pinging(string addr,int id, uint taskid)

catch(exception e)

return true;

} private byte fillechoreq()

private static uint iocntlcheck(socket s)

//used to check reply data by sync

public bool checkreply()

catch(exception e) }

//directly analyze the byte array.

public bool checkechoreply1(byte recv,int len)

: bytes=,icmp_seq=,ttl=,time= ms",

src,bytes,seq,ttl ,ts.milliseconds );

return true;

} //use iphdr, icmphdr to analyze replyk data.

public bool checkechoreply(byte recv,int len)

: bytes=,icmp_seq=,ttl=,time= ms",

src,bytes,seq,reply.iphdr.ttl ,ts.milliseconds );

return true;

} else

,,type=,icmp_seq=",

// src,dst,type,seq);

} return false;

} public bool receive() }

catch(exception e)

",e.tostring());

return false;

} return true;

} public void receivecallback(iasyncresult ar)

",((ipendpoint)tempep).address);

} catch(exception e) }

public void clear() }

}

c 實現ping功能

思路 首先新增一個文字框,用來接收ip地址。用ipaddress myip ipaddress.parse textbox1.text.tostring 把文字框內容變成ip地址,寫到一個異常中,就可以省略掉判斷ip地址是否正確了。定義一個ping的方法 system.net.networkinfo...

實現PING功能

實現ping功能 具體細節可以看註釋 include include include include include include include include include include include include include define max size 1024 char s...

linux下ping的C語言實現

include stdio.h include signal.h include arpa inet.h include sys types.h include sys socket.h include unistd.h include netinet in h include netinet ip...

linux下實現的ping程式

一 設計目的 ping程式是我們使用的比較多的用於測試網路連通性的程式。ping程式基於icmp,使用icmp的回送請求和回送應答來工作。由計算機網路課程知道,icmp是基於ip的一個協議,icmp包通過ip的封裝之後傳遞。課程設計中選取ping程式的設計,其目的是通過ping程式的設計,能初步掌握...

ping命令的C實現

include include include include include include include include include include include include include include define packet size 1024 define max wai...