在两台远程计算机之间发送数据,从而使互联网(无法正常工作)

时间:2019-04-28 06:52:32

标签: python-3.x sockets networking tcp

我正在尝试通过Internet在两台计算机之间发送二进制数据 我正在使用tcpip python套接字,并已在路由器中启用端口转发。仍然我无法成功连接它们。请找出我的错误

我尝试使用其他协议(DCP),但问题仍然存在

class Server:
    def __init__(self):
        #port forwarding and finding ip
        p.EnablePortForward()
        self.connections=[]
        self.peers=[]
        self.sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        self.sock.bind((p.serverhost,10000))
        self.sock.listen(1)

    def run_server(self):
        print('started')
        while True:
            print('running')
            c,a=self.sock.accept()
            self.peers.append(a)
            cThread=threading.Thread(target=self.handler,args=(c,a))
            cThread.deamon=True
            cThread.start()
            self.connections.append(c)
            print(str(a[0])+':'+str(a[1])+'connected')

    def handler(self,c,a):
        try:
            while True:
                data=c.recv(4096)
                for conn in self.connections:
                    if data:
                        print(data)
                        self.disconnect(c,a)
        except:
            self.disconnect(c,a)

    def disconnect(self,c,a):
        self.connections.remove(c)
        self.peers.remove(a)
        c.close()

class Client:
    def __init__(self,address):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.sock.connect((address,10000))

    def SendMsg(self,message):
        self.sock.send(message)

    def RecMsg(self):
        data = self.sock.recv(1024)
        print(data)

i使服务器对象无效并执行Server.runserver() 服务器正确监听 当我尝试使用服务器ip创建客户端对象时,它无法连接并给出超时错误。

0 个答案:

没有答案