nat hole-punching

2020-11-26 12:11:12 浏览数 (1)

udp

client,接收服务端转发的地址,直接发送即可,路由器上在连通的情况下会自动记录内外端口号,并在短时间内关闭,所以需要循环发送数据

代码语言:javascript复制
def main(host='127.0.0.1', port=9999):
    sock = socket.socket(socket.AF_INET, # Internet
                         socket.SOCK_DGRAM) # UDP     
    a = 0
    while True:
        try:
            print("------------send {}".format(a))
            sock.sendto(b'0', (host, port))
            a =1
            break
        except Exception as e:
            time.sleep(3)
            continue

    while True:
        data, addr = sock.recvfrom(1024)
        print('client received: {} {}'.format(addr, data))
        addr = msg_to_addr(data)
        sock.sendto(b'0', addr)
        data, addr = sock.recvfrom(1024)
        print('client received: {} {}'.format(addr, data))


if __name__ == '__main__':
    logging.basicConfig(level=logging.I

0 人点赞