文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>connection socket pool

connection socket pool

时间:2010-04-10  来源:xiaosuo

Linux connect(2)还有一个不怎么为人所知的属性:如果地址的family是AF_UNSPEC,对于TCP连接将通过发送RST包关闭连接,对于UDP连接将去掉目的地址关联,共同的结果是留下一个未连接的socket,这个socket和刚通过socket(2)创建的socket一样。这使得我们可以通过connect(2)到family是AF_UNSPEC的地址来替代close(2)关闭一个连接,并把这个连接的socket放到一个pool里面,以备后用,因为这能省去一个socket(2)调用的开销。

测试代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define xconnect(fd, addr, len) \
        do { \
                if (connect(fd, addr, len) < 0) { \
                        fprintf(stderr, "connect error with: %s\n", \
                                strerror(errno)); \
                        exit(EXIT_FAILURE); \
                } \
        } while (0)

int main(int argc, char *argv[])
{
        int retval;
        struct sockaddr_in addr;
        socklen_t len;

        retval = socket(AF_INET, SOCK_STREAM, 0);
        memset(&addr, 0, sizeof(&addr));
        addr.sin_family = AF_INET;
        addr.sin_addr.s_addr = inet_addr("192.168.235.10");
        addr.sin_port = htons(22);
        len = sizeof(addr);
        xconnect(retval, (struct sockaddr*)&addr, len);
        getsockname(retval, (struct sockaddr*)&addr, &len);
        printf("%d\n", ntohs(addr.sin_port));
        addr.sin_family = AF_UNSPEC;
        xconnect(retval, (struct sockaddr*)&addr, len);
        addr.sin_family = AF_INET;
        addr.sin_port = htons(22);
        xconnect(retval, (struct sockaddr*)&addr, len);
        getsockname(retval, (struct sockaddr*)&addr, &len);
        printf("%d\n", ntohs(addr.sin_port));

        return 0;
}


以下是抓包的输出:

 # tcpdump -i lo -n
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 96 bytes
22:43:17.608723 IP 192.168.235.10.60438 > 192.168.235.10.22: S 3365801819:3365801819(0) win 32792 <mss 16396,sackOK,timestamp 21687344 0,nop,wscale 5>
22:43:17.609661 IP 192.168.235.10.22 > 192.168.235.10.60438: S 3378619916:3378619916(0) ack 3365801820 win 32768 <mss 16396,sackOK,timestamp 21687345 21687344,nop,wscale 5>
22:43:17.609690 IP 192.168.235.10.60438 > 192.168.235.10.22: . ack 1 win 1025 <nop,nop,timestamp 21687345 21687345>
22:43:17.610715 IP 192.168.235.10.60438 > 192.168.235.10.22: R 1:1(0) ack 1 win 1025 <nop,nop,timestamp 21687346 21687345>
22:43:17.613609 IP 192.168.235.10.60439 > 192.168.235.10.22: S 3365834590:3365834590(0) win 32792 <mss 16396,sackOK,timestamp 21687349 0,nop,wscale 5>
22:43:17.614335 IP 192.168.235.10.22 > 192.168.235.10.60439: S 3372878782:3372878782(0) ack 3365834591 win 32768 <mss 16396,sackOK,timestamp 21687350 21687349,nop,wscale 5>
22:43:17.614351 IP 192.168.235.10.60439 > 192.168.235.10.22: . ack 1 win 1025 <nop,nop,timestamp 21687350 21687350>
22:43:17.614633 IP 192.168.235.10.60439 > 192.168.235.10.22: F 1:1(0) ack 1 win 1025 <nop,nop,timestamp 21687350 21687350>
22:43:17.617225 IP 192.168.235.10.22 > 192.168.235.10.60439: . ack 2 win 1024 <nop,nop,timestamp 21687353 21687350>
22:43:17.729908 IP 192.168.235.10.22 > 192.168.235.10.60439: P 1:22(21) ack 2 win 1024 <nop,nop,timestamp 21687465 21687350>
22:43:17.729943 IP 192.168.235.10.60439 > 192.168.235.10.22: R 3365834592:3365834592(0) win 0

我们尽量在收到对方的FIN包,也就是read(2)等返回0的时候再调用connect(AF_UNSPEC)关闭连接,防止造成误会。

可能的应用:反向HTTP代理中,由代理服务器发往HTTP服务器的连接。
相关阅读 更多 +
排行榜 更多 +
百炼英雄抽卡技巧指南

百炼英雄抽卡技巧指南

休闲益智 下载
英雄没有闪滚雷旋风技能如何搭配

英雄没有闪滚雷旋风技能如何搭配

休闲益智 下载
英雄没有闪雷旋风BD构筑推荐

英雄没有闪雷旋风BD构筑推荐

休闲益智 下载