【发布时间】:2012-01-21 07:31:36
【问题描述】:
对于具有以下结构的 TCP 服务器:
main(){
socket();
bind();
listen();
while(1){
accept();
fork();
if(child)
Process;
}
}
它为每个客户端创建一个新的套接字,并使用相同的端口与所有客户端通信。因此,所有套接字都绑定到同一个端口。
我在阅读内核代码(2.6.33.5)时遇到了comments:
48/* There are a few simple rules, which allow for local port reuse by
49 * an application. In essence:
50 *
51 * 1) Sockets bound to different interfaces may share a local port.
52 * Failing that, goto test 2.
53 * 2) If all sockets have sk->sk_reuse set, and none of them are in
54 * TCP_LISTEN state, the port may be shared.
55 * Failing that, goto test 3.
56 * 3) If all sockets are bound to a specific inet_sk(sk)->rcv_saddr local
57 * address, and none of them are the same, the port may be
58 * shared.
59 * Failing this, the port cannot be shared.
60 *
那么,对于上面的 TCP 服务器,是它匹配的第三条规则吗?
【问题讨论】:
-
不管linux的实现如何,一个TCP连接都是通过五元组来区分的:(protocol, local addr, local port, remote addr, remote port)。
标签: c linux sockets linux-kernel