jpcap你要自己下好相应的包和配置,不知道的就在网上搜吧··
import javanetInetAddress;
import javautilArrayList;
import javautilGregorianCalendar;
import javautilList;
import jpcapJpcapCaptor;
import jpcapJpcapSender;
import jpcapNetworkInterface;
import jpcappacketEthernetPacket;
import jpcappacketICMPPacket;
import jpcappacketIPPacket;
public class JPing {
private NetworkInterface[] devices = JpcapCaptorgetDeviceList();
private JpcapSender sender;
private JpcapCaptor jpcap;
private ICMPPacket icmpPacket;
private List<String> listResult = new ArrayList<String>();
/
组织ICMP报文发送,并开启线程接收报文
@param ip
/
public void ping(String ip) {
try {
jpcap = JpcapCaptoropenDevice(devices[0], 200, false, 20);
sender = jpcapgetJpcapSenderInstance();
jpcapsetFilter("icmp", true);// 过滤器,只接受ICMP报文
icmpPacket = new ICMPPacket();
icmpPackettype = ICMPPacketICMP_ECHO; // 发送回显请求报文
icmpPacketsetIPv4Parameter(0, false, false, false, 0, false,
false, false, 0, 1010101, 100, IPPacketIPPROTO_ICMP,
devices[0]addresses[1]address, InetAddressgetByName(ip));
// 随意的32bytes数据
icmpPacketdata = "abcdefghijklmnopqrstuvwxyzabcdef"getBytes();
EthernetPacket ethernetPacket = new EthernetPacket();
ethernetPacketframetype = EthernetPacketETHERTYPE_IP;
ethernetPacketsrc_mac = devices[0]mac_address;
// 广播地址
ethernetPacketdst_mac = new byte[] { (byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff };
icmpPacketdatalink = ethernetPacket;
listResultadd("Pinging " + icmpPacketdst_ip + " with "
+ icmpPacketdatalength + " bytes of data: ");
startCapThread(jpcap);
for (int i = 0; i < 5; i++) {
icmpPacketsec = 0;
//icmpPacketusec = SystemcurrentTimeMillis();
icmpPacketusec = new GregorianCalendar()getTimeInMillis();// 记录发送时间
icmpPacketseq = (short) (1000 + i);
icmpPacketid = (short) (999 + i);
sendersendPacket(icmpPacket);
try {
Threadsleep(1000);
} catch (Exception e) {
eprintStackTrace();
}
}
} catch (Exception e) {
eprintStackTrace();
}
}
/
接收ICMP报文
@param jpcap
/
public void getIcmpPacket(JpcapCaptor jpcapCaptor) {
try {
while (true) {
long tmp = 0;
String tmpStr = null;
ICMPPacket rp;
rp = (ICMPPacket) jpcapCaptorgetPacket();
if ((rp != null) && (rpseq - rpid == 1)
&& (rptype == ICMPPacketICMP_ECHOREPLY)) {// 若是ICMP回应报文,则列出。。。
tmp = (rpsec 1000 + rpusec / 1000 - icmpPacketsec
1000 - icmpPacketusec); // 计算发送与接受的时间差
if (tmp <= 0)
tmpStr = " < 1 ms ";
else
tmpStr = "= " + tmp + " ms ";
Systemoutprintln("Reply from "
+ rpsrc_ipgetHostAddress() + ": bytes = "
+ rpdatalength + " time " + tmpStr + "TTL = "
+ rphop_limit);
listResultadd("Reply from " + rpsrc_ipgetHostAddress()
+ ": bytes = " + rpdatalength + " time " + tmpStr
+ "TTL = " + rphop_limit);
}
}
} catch (Exception e) {
eprintStackTrace();
}
}
/
接收ICMP报文
@param jpcap
/
public void startCapThread(final JpcapCaptor jpcap) {
Runnable runner = new Runnable() {
public void run() {
getIcmpPacket(jpcap);
}
};
new Thread(runner)start();
}
public static void main(String[] args) {
new JPing()ping(">
学习网络编程不建议用现成的库。就算要用也不推荐QT。
Windows下请看《windows网络与通信程序设计》
Linux请看《unix环境高级编程》(必读)、《Unix网络编程》、《TCP/IP详解》
另外一些著名的网络库:ACE,Boost::ASIO,libevent
中间件:ICE(ICE也提供了通信能力,但个人不太推荐使用)
至于基础方面,TCP/IP等理论还是必须要知道的,所以学习网络通信最好从socket开始。从Linux下着手最佳。
《Java网络编程核心技术详解》百度网盘pdf最新全集下载:
htjm
简介:本书结合大量典性的实例,详细介绍了用Java来编写网络应用程序的技术。本书的范例都基于最新的JDK10版本。本书内容包括:Java网络编程的基础知识、 套接字编程、非阻塞通信、创建>
以上就是关于有谁知道怎样使用java编写ping程序全部的内容,包括:有谁知道怎样使用java编写ping程序、如何用java实现一个简单网络通信过程、学java网络编程有没有推荐计划等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)