[英]Establishing a socket connection between computers?
I was learning about networking and I have some trouble understanding what went wrong. 我正在学习网络知识,但在理解问题出处时遇到了一些麻烦。
I created a Client and a Server script: 我创建了一个客户端和一个服务器脚本:
Server: 服务器:
import socket
s = socket.socket()
host = socket.gethostname()
port = 12345
s.bind((host,port))
s.listen(5)
while True:
c, addr = s.accept()
print ("Got connection from: " ,addr)
c.send("Thanks".encode('utf-8'))
# c.sendto(("Thank you for connection").encode('utf-8'), addr)
c.close()
and Client: 和客户:
import socket
s=socket.socket()
host=socket.gethostname()
port = 12345
s.connect((host,port))
c=s.recv(1024)
print (c)
s.close
When I am trying to run from my computer I have no problem (both scripts) But when I am running the Client from another Computer, the following error pops up for the Client: ConnectionRefuseError: WinError10061 No connection could be made because the target machine actively refused it
. 当我尝试从我的计算机运行时(两个脚本)都没有问题,但是当我从另一台计算机运行客户端时,为客户端弹出以下错误:
ConnectionRefuseError: WinError10061 No connection could be made because the target machine actively refused it
。
Got any idea what could fix this? 有任何想法可以解决此问题吗?
The problem was that I wasn't referring to the server IP when running from another computer, I fixed it by passing the server IP, in the client script, like this host = "10.xxx" 问题是,当我从另一台计算机运行时,我不是在指服务器IP,而是通过在客户端脚本中传递服务器IP来修复它,例如此主机=“ 10.xxx”。
Sorry for creating a useless question! 很抱歉提出一个无用的问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.