Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python从不同系统上的软件接收TCP数据的套接字编程_Python_Sockets_Tcpclient - Fatal编程技术网

使用python从不同系统上的软件接收TCP数据的套接字编程

使用python从不同系统上的软件接收TCP数据的套接字编程,python,sockets,tcpclient,Python,Sockets,Tcpclient,我正在尝试使用计算机a上的一个软件,该软件将TCP数据块发送到计算机B。我的客户机程序是在计算机B上用python编写的,用于接收数据块。但是当我运行代码时,我得到以下错误 OSError: [WinError 10065] A socket operation was attempted to an unreachable host 下面是我的client.py def Main(): host = '146.254.8.132' #IP Address of the system

我正在尝试使用计算机a上的一个软件,该软件将TCP数据块发送到计算机B。我的客户机程序是在计算机B上用python编写的,用于接收数据块。但是当我运行代码时,我得到以下错误

OSError: [WinError 10065] A socket operation was attempted to an unreachable host

下面是我的client.py

def Main():
    host = '146.254.8.132' #IP Address of the system which has the software sending TCP data
    port = 5003 #TCP port number used by the software
    s = socket.socket()
    s.connect((host,port))

    i = 0
    #for i < 2:
    len_message = s.recv(3)
    print(len_message)
    while len_message:
        bytes_length = int(len_message.decode())
        #data_length = (bytes_length + 3)
        print(bytes_length)
        #print(data_length)
        data = s.recv(bytes_length)
        print(data)
        write_file(data)
        len_message = s.recv(3)
    #i+=1
    s.close()

def write_file(data):
        with open("Output.txt", "wb") as text_file:
            text_file.write(data)
            text_file.write('\n'.encode())


if __name__ == '__main__':
    Main()

def Main():
主机='146.254.8.132'#具有发送TCP数据的软件的系统的IP地址
端口=5003#软件使用的TCP端口号
s=socket.socket()
s、 连接((主机、端口))
i=0
#对于i<2:
len_message=s.recv(3)
打印(len_信息)
而len_信息:
字节长度=int(len\u message.decode())
#数据长度=(字节长度+3)
打印(字节长度)
#打印(数据长度)
数据=s.recv(字节长度)
打印(数据)
写入文件(数据)
len_message=s.recv(3)
#i+=1
s、 关闭()
def写入_文件(数据):
打开(“Output.txt”、“wb”)作为文本文件:
text_file.write(数据)
text_file.write('\n'.encode())
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
Main()
为什么我会出现错误而无法接收数据?
可能是因为我的客户机未连接到任何网络,但我的服务器位于具有以太网连接的系统上?提前感谢

您能否确保防火墙规则允许传入端口或ip地址?您如何做到这一点?抱歉,我不知道这一点。根据入站规则,我输入了TCP的2055端口,但似乎不起作用。您可以尝试通过443端口连接到www.google.com,看看是否发生连接?您的问题看起来像Windows上的默认高端口阻塞。是否可能是因为我的客户端不在任何网络上,但服务器已连接到以太网而出现此问题?它们是否都应连接到同一网络?