Skip to content
This repository was archived by the owner on Aug 22, 2021. It is now read-only.
This repository was archived by the owner on Aug 22, 2021. It is now read-only.

tun2socks not support half-closing  #122

@578141611

Description

@578141611

like that
(1)client connect to server
(2)server send fin to client
(3)client recv fin,then send some data to server
(4)server can recv succ
(5)client send all data,close the socket
(6)server recv fin from client

`
server

import socket
import sys
import time
import datetime

def get_current_time():
    return "[" + str(datetime.datetime.now()) + "]"

def start_tcp_server(ip, port):
    #create socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = (ip, port)
    #bind port
    print (get_current_time() + "starting listen on " + str(server_address))
    sock.bind(server_address)
    #starting listening, allow only one connection
    try:
        sock.listen(1)
    except Exception as e:
        print (get_current_time() + "fail to listen on port " + str(e))
        sys.exit(1)

    while True:
        try:
            print (get_current_time() + "waiting for connection")
            client,addr = sock.accept()
            print (get_current_time() + 'having a connection')

            client.send('your addr is {}'.format(addr).encode('utf-8'))

            time.sleep(2)

            print (get_current_time() + 'shutdown write')
            client.shutdown(socket.SHUT_WR)

            while True:
                cr = client.recv(1024)
                if(len(cr) == 0):
                    print("recv peer shutdown write,then close")
                    break
                print(get_current_time() + "recv data:" + cr.decode('utf-8'))
            client.close()
        except Exception as e:
            print (get_current_time() + "exception " + str(e))
            sys.exit(1)
    
if __name__ == '__main__':
    start_tcp_server('0.0.0.0', 12345)

client

import socket
import datetime
import time

def get_current_time():
    return "[" + str(datetime.datetime.now()) + "]"

if __name__ == '__main__':
    for i in range(1):
        try:
            client_sock = socket.socket()
            address = ('172.22.20.54', 12345)
            begin_time = datetime.datetime.now()
            client_sock.connect(address)
            print(get_current_time() + "connect succ")
            while True:
                content = client_sock.recv(1024)
                if(len(content) == 0):
                    for i in range(10):
                        client_sock.send('your addr is {}'.format(address).encode('utf-8'))
                        time.sleep(0.5)
                    print(get_current_time() + "recv shutdown wirte then close")
                    break
                print(get_current_time() + "recv data:" + content.decode('utf-8'))
                client_sock.send('your addr is {}'.format(address).encode('utf-8'))
            client_sock.close()

            end_time = datetime.datetime.now()
            duration = (end_time - begin_time).total_seconds()
            print(get_current_time() + " succ to close " + str(address) + " peroid:" + str(duration) + "s")
            if(duration > 7.1):
                print("duration too long" + str(duration) + "s")
        except Exception as e:
            print(e)

`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions