[python&php 网络编程]socket缓冲区大小设置

1.python

import socket

SEND_BUF_SIZE = 4096
RECV_BUF_SIZE = 4096

def modify_buff_size():
    sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
    
    # Get the size of the socket's send buffer
    bufsize = sock.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
    print "Buffer size [Before]:%d" %bufsize
    
    sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
    sock.setsockopt(
            socket.SOL_SOCKET,
            socket.SO_SNDBUF,
            SEND_BUF_SIZE)
    sock.setsockopt(
            socket.SOL_SOCKET,
            socket.SO_RCVBUF,
            RECV_BUF_SIZE)
    bufsize = sock.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
    print "Buffer size [After]:%d" %bufsize

if __name__ == '__main__':
    modify_buff_size()

2.php


socket的缓冲区太小 则tcp窗口不能完全打开,影响性能,缓冲区太大,则浪费内存,因此缓冲区设置合适大小还是很有必要的

对于socket缓冲区大小的设置 python和php都有类似的方法直接操作~

转载请标注原文地址:http://blog.csdn.net/ddjohn/article/details/49993155

你可能感兴趣的:(python,php)