当我使用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);
}
}
当我绑定一个套接字,但不要与连接或监听一起使用它.
我进入/ 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中导出.
如何找到绑定套接字但不监听或连接的进程:
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的常见问题解答:
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/*
给出一个空的结果.