本文介绍了如何通过 Internet 将套接字连接到另一台计算机的套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在通过互联网将一个套接字连接到另一台计算机的套接字时遇到了一些困难,一个图像值一千字:

计算机 A 正在运行此listener.py"脚本:

导入套接字端口 = 50007缓冲区 = 2048主机 = ''如果 __name__ == '__main__':使用 socket.socket(socket.AF_INET, socket.SOCK_STREAM) 作为 s:s.bind((主机,端口))s.听(1)conn, addr = s.accept()与连接:打印('连接',地址)为真:数据 = conn.recv(BUFFER)如果不是数据:中断conn.sendall(数据)

计算机 B 正在运行此sender.py"脚本:

导入套接字HOST = '101.81.83.169' # 远程主机PORT = 50007 # 与服务器使用的端口相同如果 __name__ == '__main__':使用 socket.socket(socket.AF_INET, socket.SOCK_STREAM) 作为 s:s.connect((主机,端口))s.sendall(b'你好,世界')

所以首先,我运行计算机A侦听器"脚本.然后,我运行计算机 B 的sender"脚本.但是,当我执行sender"脚本时,我收到一个错误消息解释了我无权连接到此远程地址.

所以我想知道如何在不更改路由器配置的情况下通过互联网将一个套接字连接到另一个套接字.

非常感谢您的帮助.

编辑:这里是错误信息(由于某些原因我没有执行相同的脚本,但它是相同的错误信息)

sock.connect(('101.81.83.169',50007)) 回溯(最近一次调用最后一次):文件",第 1 行,在<模块>文件/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py",第 224 行,在meth return getattr(self._sock,name)(*args) socket.error: [Errno 61] 连接被拒绝
解决方案

来自

所以:

- 第 1 步:Alice 连接到跟踪器,跟踪器将 Bob 和 Mick 的 IP 地址提供给 Alice.

- 第二步:Alice 收到 Bob 和 Mick 的 IP 地址,然后她可以尝试建立 TCP/IP 连接以下载文件.

当我想从 Bittorent 下载文件时,我不记得必须设置任何路由器配置.

那我错过了什么?

I recently have some difficulties to connect a socket to another computer's socket through Internet, an image is worth a thousand words:

Computer A is running this "listener.py" script:

import socket
PORT = 50007              
BUFFER = 2048              
HOST = ''  

if __name__ == '__main__':
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind((HOST, PORT))
        s.listen(1)
        conn, addr = s.accept()
        with conn:
            print('Connected by', addr)
            while True:
                data = conn.recv(BUFFER)
                if not data: break
                conn.sendall(data)

Computer B is running this "sender.py" script:

import socket
HOST = '101.81.83.169'    # The remote host
PORT = 50007              # The same port as used by the server


if __name__ == '__main__':
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect((HOST, PORT))
        s.sendall(b'Hello, world')

So first of all, I run the "listener" script of the computer A. Then, I run the "sender" script of the computer B. However, when I execute the "sender" script, I received a error message which explains me that I am not authorized to connect to this remote address.

So I would like to know how can I connect a socket to another socket through internet without changing the router configurations.

Thank you very much for your help.

Edit: Here the error message (I didn't execute the same script for some reasons, but it's the same error message)

sock.connect(('101.81.83.169',50007)) Traceback (most recent call last):   File "<stdin>", line 1, in
 <module>   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 224, in
meth     return getattr(self._sock,name)(*args) socket.error: [Errno 61] Connection refused
解决方案

From the book "Computer Networking: A Top-Down Approach", there is a part which is very interesting on page 149 about how Bittorents work:

So:

- Step 1: Alice connects to the tracker, the tracker gives to Alice the ip addresses of Bob and Mick.

- Step 2:Alice receives the ip addresses of Bob and Mick, then she can try to establish TCP/IP connections for downloading the file.

I don't remember having to set up any router configuration when I wanted to download files from Bittorent.

So what am I missing?

这篇关于如何通过 Internet 将套接字连接到另一台计算机的套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 23:08