如何使用Java监控我的网络连接? [英] How to monitor my network connection with Java?

查看:1270
本文介绍了如何使用Java监控我的网络连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何示例代码可以报告我的PC与Java连接的网址?

Is there any sample code that can report the url that my PC is connecting with Java?

我使用浏览器连接到不同的网站并在线观看视频,它应该捕获网址。它在Java中是否可行?

While I use my browser to connect to different sites and watch video online, it should capture the urls. Is it doable in Java?

我不想要详细的流量,只记录网址。

I don't want detailed traffic, just record the urls.

推荐答案

最快的方法可能是捕获命令行 tshark 程序的输出(至少对于windows和linux)。这适用于我的linux盒子:

The fastest way is probably going to be to capture the output of the command-line tshark program (for at least windows and linux). This works on my linux box:

sudo tshark -f 'port 80' -R 'http' -V | grep -A 1 '^Hypertext Transfer Protocol'

并产生如下输出:

Running as user "root" and group "root". This could be dangerous.
Capturing on eth0
Hypertext Transfer Protocol
    GET /questions/4494294/r-gplots-barplots-how-to-fix-bar-width-independent-of-paper-setting HTTP/1.1\r\n
--
Hypertext Transfer Protocol
    HTTP/1.1 200 OK\r\n
--
Hypertext Transfer Protocol
    GET /posts/4494294/ivc/086e HTTP/1.1\r\n
--
Hypertext Transfer Protocol
    HTTP/1.1 204 No Content\r\n
--
Hypertext Transfer Protocol
    [truncated] GET /__utm.gif?utmwv=4.8.6&utmn=20455052&utmhn=stackoverflow.com&utmcs=UTF-8&utmsr=1600x1200&utmsc=24-bit&utmul=en-us&utmje=1&utmfl=10.1%20r102&utmdt=graph%20-%20R%3A%20gplots%2C%20barplots%3A%20how%20to%20fix%20bar%20width%20i
--
Hypertext Transfer Protocol
    HTTP/1.1 200 OK\r\n
--

您应该为Windows获得类似的东西。实验。如何将tshark输出到java中取决于你。

You should get something similar for Windows. Experiment. How you get the output of tshark into java is up to you.

如果你想要一个全java解决方案, http://jnetpcap.com/

If you want an all-java solution, how about http://jnetpcap.com/ ?

你将不得不写(或使用库提供了一些特定于平台的JNI代码。

You're going to have to write (or use a library that provides) some platform specific JNI code.

libpcap / WinPcap jNetPcap 使用它可靠地提供根据我的经验,最少的Windows和Linux。我有使用 libpcap / WinPcap 的经验,但不能使用 jNetPcap

libpcap/WinPcap that jNetPcap uses provides this reliably on at least Windows and Linux from my experience. I have experience with libpcap/WinPcap but not with jNetPcap.

tshark 实际上也使用 libpcap / WinPcap

无论哪种方式,你都会看到很多误报。打开HTML页面需要获取许多图片,样式表,javascript库等。上面的代码段是打开stackoverflow.com页面。

Either way, you're going to see very many false positives. Opening a HTML page requires getting many, many images, style sheets, javascript libraries etc. The snippet above is from opening a stackoverflow.com page.

这篇关于如何使用Java监控我的网络连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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