Java-traffic statistics source code

Source: Internet
Author: User


Collects statistics on the traffic received and sent by all ports.

 

:

 

Source code:

Network. Java is used to calculate the port traffic value.

Networkthread. Java Thread class, used to display information to the interface

 

 

 

Import Java. io. bufferedreader; <br/> Import Java. io. inputstreamreader; <br/> public class network {<br/> Public String recevied; <br/> Public string sent; <br/> Public void networkflux (string skip line) {<br/> try {<br/> string line; <br/> PROCESS p = runtime.getruntime(cmd.exe C (cmdline ); <br/> bufferedreader input = new bufferedreader (New inputstreamreader (P. getinputstream (); <br/> int C = 0; <br/> while (( Line = input. Readline ())! = NULL) {</P> <p> If (C> 3) <br/> {<br/> string s [] = line. replaceall ("bytes ",""). trim (). replace ("","@"). split (""); <br/> // system. out. println (S. length); </P> <p> for (string STR: S) <br/>{< br/> recevied = Str. substring (0, str. indexof ("@"); <br/> sent = Str. substring (Str. lastindexof ("@") + 1, str. length (); <br/>}< br/>/** system. out. println (recevied + "" + sent); <br/> system. out. println ("accept traffic:" + long. parselong (recevied)/1024/1024 + "MB"); <br/> system. out. println ("sent traffic:" + long. parselong (sent)/1024/1024 + "MB"); <br/> **/<br/> recevied = long. parselong (recevied)/1024 + "kb"; <br/> sent = long. parselong (sent)/1024 + "kb"; <br/> break; <br/>}< br/> C ++; <br/>}< br/> input. close (); <br/>}catch (exception ERR) {<br/>}< br/>

 

 

 

Import Java. AWT. color; <br/> Import Java. AWT. gridlayout; <br/> Import javax. swing. jframe; <br/> Import javax. swing. jlabel; <br/> Import javax. swing. jpanel; <br/> public class networkthread extends jframe implements runnable {<br/> network = New Network (); </P> <p> private string recevied; <br/> private string sent; </P> <p> private jlabel lab1 = new jlabel ("accept traffic :"); <br/> private jlabel lab2 = new jlabel (); <br/> private jlabel lab3 = new jlabel ("Send traffic :"); <br/> private jlabel lab4 = new jlabel (); </P> <p> private jpanel p1 = new jpanel (); <br/> private jpanel P2 = new jpanel (); <br/> private jpanel P = new jpanel (); </P> <p> Public networkthread () {</P> <p> p1.add (lab1); <br/> p1.add (lab2); <br/> p2.add (lab3); <br/> p2.add (lab4 ); <br/> p1.setbackground (color. pink); <br/> p2.setbackground (color. yellow); <br/> P. add (P1); <br/> P. add (P2); <br/> P. setlayout (New gridlayout (2, 1); <br/> This. settitle ("Traffic calculation"); <br/> This. add (p); <br/> This. setsize (200,100); <br/> This. setresizable (false); <br/> This. setdefaclocloseoperation (this. exit_on_close); <br/> This. setvisible (true); </P> <p >}</P> <p> Public void run () {<br/> DO <br/> {<br/> network. networkflux ("netstat-e"); <br/> This. recevied = network. recevied; <br/> This. sent = network. sent; <br/> lab2.settext (recevied); <br/> lab4.settext (sent); </P> <p> try {<br/> thread. sleep (100); <br/>} catch (interruptedexception e) {<br/> E. printstacktrace (); <br/>}< br/>}while (true); <br/>}< br/> Public static void main (string [] ARGs) {<br/> New thread (New networkthread ()). start (); <br/>}< br/>

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.