列出Java中的打开TCP连接

我想知道如何列出系统上所有使用Java打开的TCP连接.我正在使用CentOS.

我也不知道从哪里开始.任何指针都会有所帮助.

提前致谢

谢谢你的提示
我必须做这样的事情

Q)确定当前正在侦听的所有tcp端口的任何新建立的连接
    并继续每5秒轮询一次.当不再有已建立的连接时,脚本应终止.

public class TCPConnections {

    public HashSet<Integer> establishedConnections = new HashSet<Integer>();
    public HashSet<Integer> listeningConnections = new HashSet<Integer>();

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TCPConnections tcpConnections = new TCPConnections();
        try{        
            do{    
                tcpConnections.getListeningConnections();
                Thread.sleep(5000);
                tcpConnections.getEstablishedConnections();    
            }while(!tcpConnections.establishedConnections.isEmpty());    
        }    
        catch(Exception ex){    
            ex.printStackTrace();    
        }
    }

    public void getEstablishedConnections(){
        String netstat = new String();    
        try {    
                String line;
                establishedConnections = new HashSet<Integer>();
                String[] cmd = {    
                        "/bin/sh",    
                        "-c",    
                        "netstat -atn | grep -w tcp | grep ESTABLISHED"    
                        };

                java.lang.Process p = Runtime.getRuntime().exec(cmd);
                BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));

                while ((line = input.readLine()) != null) {    
                    String[] portNo = line.split("\\s+");    
                    if(portNo[3] != null && !portNo[3].equalsIgnoreCase(" ")){    
                        String str = portNo[3].split(":")[1];    
                        if( str != null && str.matches("[0-9]+")){    
                            establishedConnections.add(Integer.parseInt(str));    
                            if(listeningConnections.contains(Integer.parseInt(str))){listeningConnections.remove(Integer.parseInt(str));  
                                System.out.println(" New connection established on port : "+Integer.parseInt(str));    
                            }    
                        }    
                    }    
                        netstat = netstat + " \n" + line;    
                }    
                System.out.println(netstat);    
                input.close();
        } catch (Exception err) {    
                err.printStackTrace();    
        }
    }

    public void getListeningConnections(){    
        String netstat = new String();    
        try {    
                String line;    
                listeningConnections = new HashSet<Integer>();
                String[] cmd = {    
                        "/bin/sh",    
                        "-c",    
                        "netstat -atn | grep -w tcp | grep LISTEN"    
                        };
                java.lang.Process p = Runtime.getRuntime().exec(cmd);    
                BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));    
                while ((line = input.readLine()) != null) {    
                    String[] portNo = line.split("\\s+");    
                    if(portNo[3] != null && !portNo[3].equalsIgnoreCase(" ")){    
                        String str = portNo[3].split(":")[1];    
                        if( str != null && str.matches("[0-9]+")){    
                            listeningConnections.add(Integer.parseInt(str));                        
                        }   
                    }       
                        netstat = netstat + " \n" + line;    
                }    
                System.out.println(netstat);    
                input.close();          
        } catch (Exception err) {    
                err.printStackTrace();    
        }
    }    
}

我面临的问题是,很少有端口始终处于建立状态,而很少有端口始终处于侦听状态,因此do-while循环将永远运行.请帮助我解决这个问题.

解决方法:

Java似乎没有内置任何东西可以支持此功能,这并不奇怪,因为类似于netstat的功能取决于操作系统.

您还有两个选择:

1)解析netstat的输出:

$netstat -tn
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 192.168.1.140:48352     74.125.225.134:80       ESTABLISHED

2)解析/proc/net/tcp(如果需要,还可以解析tcp6,udp,udp6,unix):

$cat /proc/net/tcp
  sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode
   0: 0100007F:0277 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 17120 1 ffff8800797c4700 100 0 0 10 0
   1: 0100007F:0019 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 14821 1 ffff8800797c4000 100 0 0 10 0
   2: 8C01A8C0:BCE0 86E17D4A:0050 01 00000000:00000000 00:00000000 00000000  1000        0 20164 1 ffff8800797c4e00 24 0 0 10 -1  

这可能看起来更令人生畏,但将是首选方法,因为它不依赖于netstat(以及PATH等)的存在.

上一篇:为什么在dot42 C#android中缺少“使用Net.Sockets”?


下一篇:c#-NetworkStream的连接自动关闭