用python套接字连接两台电脑的问题

时间:2018-12-04 15:38:28

标签: python sockets

我写了一个简单的客户端服务器porgram。我想通过此代码连接两台PC。我的服务器PC通过wifi连接,服务器代码为:

import socket

HOST=''
PORT=50007

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST,PORT))

while True:
    try:
        s.listen(1)
        conn, addr=s.accept()
        print('Connected by',addr)
        while 1:
            data=conn.recv(1024)
            if not data:
                break
            conn.send(data)
            print(data.decode('utf-8'))
        conn.close()
    except:
        print('Client disconnected')

我的客户端PC通过USB调制解调器连接。客户端代码为:

import socket

HOST='192.168.1.111'
PORT=50007

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
while True:
    print('Enter your text:')
    output=input()
    s.send(output.encode('utf-8'))
    data = s.recv(1024)
    print ('Posted')

此主机是我的服务器PC的ipv4地址。如果我在一台PC上运行此客户端和服务器程序,则可以正常运行,但是当我尝试通过单个PC连接时,此客户端无法与服务器连接,并且此错误会在客户端出现:

Traceback (most recent call last):
  File "H:\project\final\client.py", line 7, in <module>
    s.connect((HOST, PORT))
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

如果尝试从单个PC连接客户端与服务器,为什么会出现此错误。我该怎么办..... ??

0 个答案:

没有答案