linux – 如何找到哪个进程绑定套接字而不是监听?

linux – 如何找到哪个进程绑定套接字而不是监听?,第1张

概述当我使用nc来监听端口时,它会显示出来 nc -l -vv -p 21000retrying 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|gr 当我使用nc来监听端口时,它会显示出来

nc -l -vv -p 21000retrying 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);    }}

当我绑定一个套接字,但不要与连接或监听一起使用它.
我进入/ proc / [java task ID] / fd,找到这个socket的inode是“socket:[3073501]”
但即使在/ proc / net / tcp或/ proc / net / tcp6中我也找不到inode或端口

是否有任何方法可以找到绑定套接字但不监听或连接的进程.

谢谢.

我看到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可用于绑定.
但是不会在/ proc / net / tcp中导出.

解决方法 我在Ubuntu下测试了你的Java程序.

如何找到绑定套接字但不监听或连接的进程:

lsof的

lsof | grep "can't IDentify protocol"

您将得到如下结果:

COMMAND     PID   TID       USER   FD      TYPE             DEVICE SIZE/OFF    NODE namejava      29644 29653    stephan   12u     sock                0,7      0t0  312066 can't IDentify protocol

请注意TYPE袜子和name无法识别协议.

这是如何运作的?看看lsof的常见问题解答:

Why does /proc-based lsof report “can’t IDentify protocol” for some socket files?

/proc-based lsof may report:

06002

This means that it can’t IDentify the protocol (i.e.,the AF_*
designation) being used by the open socket file. Lsof IDentifIEs
protocols by matching the node number associated with the
/proc//fd entry to the node numbers found in selected files of
the /proc/net sub-directory.

You may not be able to find the desired node number,because not all
kernel protocol modules fully support /proc/net information.

验证过程

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/*

给出一个空的结果.

总结

以上是内存溢出为你收集整理的linux – 如何找到哪个进程绑定套接字而不是监听?全部内容,希望文章能够帮你解决linux – 如何找到哪个进程绑定套接字而不是监听?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/yw/1030877.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存