Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

python - TCP socket over seperate networks and port forwarding

I am trying to set up a TCP socket stream between two computers on different networks.

client.py (my computer)

import socket

def client_program():
    host = "46.117.186.128" #server public ip
    port = 45_000  # socket server port number
    s = socket.socket()
    addr = (host, port)
    print("Connected to " + str(addr))
    print(s.connect(addr))
    message = input(" -> ")  # take input
    while message.lower().strip() != 'bye':
        s.send(message.encode('utf-8'))
        print("From Server: ", s.recv(1024))
        message = input("-> ")

    s.close()

client_program()

server.py (computer on a different network)

import socket

s = socket.socket()         # Create a socket object
host = '192.168.0.104'    #private ip address of server
port = 45000
s.bind((host, port))

s.listen(5)
c, addr = s.accept()
print 'Got connection from', addr
while True:
   c.send(raw_input("Server please type: "))
   print "From Client: ", c.recv(1024)

c.close()

The server's network has port forwarding from external port 45000-> internal port 45000 when I try to connect I get TimeoutError: [WinError 10060] The connection could not be established because the connected caller did not respond correctly after a certain period of time. Or the established connection failed because the connected host did not respond.

I've tried a ping between the two networks and it times out too.

running tracert between the two also begins to timeout after 9 or so hops.

both subnet masks are 255.255.255.0 (/24)

I believe the problem lies within the networking and not the code, any help?

question from:https://stackoverflow.com/questions/65642035/tcp-socket-over-seperate-networks-and-port-forwarding

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...