• 服务端主动关闭 time_wait


    服务端主动关闭:
    
    
    node1:/root/test#cat t2.py 
    # coding = utf-8
    # -*- coding:utf-8 -*-
    import socket
    import time
    ip_port = ('192.168.137.2',8080)
    BUFSIZE = 9000
    SEND_BUF_SIZE =  2
    RECV_BUF_SIZE = 1
    s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)#创建套接字
    print("Buffer size [Before]: %d" % s.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF))
    print("Buffer size [Before]: %d" % s.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF))
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.setsockopt( socket.SOL_SOCKET, socket.SO_SNDBUF, SEND_BUF_SIZE)
    s.setsockopt( socket.SOL_SOCKET, socket.SO_RCVBUF, RECV_BUF_SIZE)
    print 'alter----------------';
    print s.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
    print s.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF)
    #s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 0)
    s.bind(ip_port)#绑定地址
    s.listen(5)#监听链接
    print('server listening8080...')
    while True: #无限等待连接
        conn,addr = s.accept() #接受客户端连接
        print(conn)
        print(addr)
        print('接到来自%s的链接'%addr[0])
        while True: #通信循环,无限接受客户端信息
            #print conn.getpeername()
            try:
             msg = conn.recv(BUFSIZE) #接受消息的内容
             print 'xxx--------------------xxx'
             print msg
             print 'xxx--------------------xxx'
            except Exception,err:
              break
            if len(msg)==0:break  #如果 不加,已连接的客户端突然断开,recv不再阻塞,发生死循环
            print '111-----------------------111'
            print msg
            print '111-----------------------111'
            time.sleep(2)
            try:
             conn.send('response:'+msg)
             conn.close()
            except Exception,err:
              print err
            #conn.send(msg.upper())#服务端发送消息
        conn.close()#关闭链接
    s.close()#关闭套接字
    
    
    
    客户端:
    node2:/root/test#cat t15.py 
    import socket
    import struct
    import time
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    #s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.connect(("192.168.137.2",8080))
    print s.getsockname()
    print s.getpeername()
    s.send('234')
    print s.recv(100);
    time.sleep(10)
    
    
    服务端:
    tcp        0      0 192.168.137.2:8080          192.168.137.3:52039         TIME_WAIT   
    tcp        0      0 192.168.137.2:8080          0.0.0.0:*                   LISTEN      
    tcp        0      0 192.168.137.2:8080          192.168.137.3:52039         TIME_WAIT   
    tcp        0      0 192.168.137.2:8080          0.0.0.0:*                   LISTEN      
    tcp        0      0 192.168.137.2:8080          192.168.137.3:52039         TIME_WAIT 
  • 相关阅读:
    2018.08.02
    加油。
    2018.07.08
    2018.06.22
    LeetCode #169. Majority Element 数组 摩尔投票法
    LeetCode #119 Pascal's Triangle II 数组 滚动数组
    LeetCode #845 Longest Mountain in Array 数组 线性DP
    LeetCode #41 First Missing Positive 数组
    LeetCode #384 Shuffle an Array 数组 洗牌算法
    LeetCode #189 Rotate Array 数组 双变量 双指针
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13348511.html
Copyright © 2020-2023  润新知