linux – 如何找到哪个进程绑定套接字而不是监听?
当我使用nc来监听端口时,它会显示出来
nc -l -vv -p 21000 retrying local 0.0.0.0:21000 : Address already in use Can't grab 0.0.0.0:21000 with bind 但是我无法使用工具netstat / ss找到哪个任务占用了这个端口 netstat -an|grep 21000 没有找到 ss -a|grep 21000 没有找到 这个端口被我的java程序占用,代码是: public class Test1 { public static void main(String[] args) throws InterruptedException { Socket s = new Socket(); try { s.bind(new InetSocketAddress("127.0.0.1",21000)); } catch (IOException e) { e.printStackTrace(); } Thread.sleep(500000000000L); } } 当我绑定一个套接字,但不要与连接或监听一起使用它. 是否有任何方法可以找到绑定套接字但不监听或连接的进程. 谢谢. 我看到linux 3.10.0-327源代码.我认为文件/ proc / net / tcp的内容来自net / ipv4 / tcp_ipv4.c. 在tcp_proc_register方法中, static void *tcp_get_idx(struct seq_file *seq,loff_t pos) { void *rc; struct tcp_iter_state *st = seq->private; st->state = TCP_SEQ_STATE_LISTENING; rc = listening_get_idx(seq,&pos); if (!rc) { st->state = TCP_SEQ_STATE_ESTABLISHED; rc = established_get_idx(seq,pos); } return rc; } 它仅显示侦听中的socks或从tcp_hashinfo建立的socks.但是tcp_hashinfo有三个结构 struct inet_bind_hashbucket *bhash; struct inet_listen_hashbucket listening_hash[INET_LHTABLE_SIZE]; struct inet_ehash_bucket *ehash; bhash可用于绑定. 解决方法
我在Ubuntu下测试了你的Java程序.
如何找到绑定套接字但不监听或连接的进程: lsof的 lsof | grep "can't identify protocol" 您将得到如下结果: COMMAND PID TID USER FD TYPE DEVICE SIZE/OFF NODE NAME java 29644 29653 stephan 12u sock 0,7 0t0 312066 can't identify protocol 请注意TYPE袜子和NAME无法识别协议. 这是如何运作的?看看lsof的常见问题解答:
验证过程 lsof输出中的PID为29644. ls -l /proc/29644/fd 这导致: ... lrwx------ 1 stephan stephan 64 Jul 7 22:52 11 -> socket:[312064] lrwx------ 1 stephan stephan 64 Jul 7 22:52 12 -> socket:[312066] ... 和 grep 312066 /proc/net/* 给出一个空的结果. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |