socket通讯(接收、发送、地址重用等)

0.原理说明以及地址重用

linux - How do SO_REUSEADDR and SO_REUSEPORT differ? - Stack Overflow

1. server端socket的接收机制

    def accept(self):
        """accept() -> (socket object, address info)

        Wait for an incoming connection.  Return a new socket
        representing the connection, and the address of the client.
        For IP sockets, the address info is a pair (hostaddr, port).
        """
        fd, addr = self._accept()
        sock = socket(self.family, self.type, self.proto, fileno=fd)
        # Issue #7995: if no default timeout is set and the listening
        # socket had a (non-zero) timeout, force the new socket in blocking
        # mode to override platform-specific socket flags inheritance.
        if getdefaulttimeout() is None and self.gettimeout():
            sock.setblocking(True)
        return sock, addr

源码中明确说明,server端会重新创建一个socket和client通讯。调用该socket.gethostname()函数,输出的ip和port和源socket的完全相同

2. server端recv()函数是否阻塞

python中recv底层调用了unix的recv函数,该函数的解释如下:

If no messages are available at the socket, the receive calls wait for a message to arrive, 
unless the socket is nonblocking (see fcntl(2)), 
in which case the value -1 is returned and the external 
variable errno is set to EAGAIN or EWOULDBLOCK. 
The receive calls normally return any data available, 
up to the requested amount, rather than waiting for 
receipt of the full amount requested.

server端的recv函数会阻塞等待,直到有client发来数据;recv接收的数据的长度不超过函数设定的buf的size

3.client端connect和close过程的用时

while j<50:
    a = time.time()
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
    client.connect((gpu01_ip, gpu01_port))
    b = time.time()
    print("connection time ",a-b)
    client.send(bytes("pid " + str(os.getpid()) + " round " + str(i) + "+++++++++++"))
    c = time.time()
    client.close()
    d = time.time()
    print("closure time",d-c)
    j = j+1

用上述代码测试connect和close过程的用时,测试结果如下,即均不超过1ms。因此,发送数据后即刻断开连接时更好的选择。

('connection time ', -0.00019407272338867188)
('closure time', 1.811981201171875e-05)
('connection time ', -0.00014495849609375)
('closure time', 1.4066696166992188e-05)
('connection time ', -0.0001380443572998047)
('closure time', 1.4066696166992188e-05)
('connection time ', -0.0001308917999267578)
('closure time', 1.9073486328125e-05)
('connection time ', -0.00015997886657714844)
('closure time', 1.5020370483398438e-05)
('connection time ', -0.00013589859008789062)
('closure time', 1.1920928955078125e-05)
('connection time ', -0.00014209747314453125)
('closure time', 2.09808349609375e-05)
('connection time ', -0.00018215179443359375)
('closure time', 1.5020370483398438e-05)
('connection time ', -0.00013589859008789062)
('closure time', 1.9073486328125e-05)
('connection time ', -0.0001850128173828125)
('closure time', 1.3113021850585938e-05)
('connection time ', -0.00014591217041015625)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值