import socket
s = socket.socket()
host = socket.gethostname()
port = 1234
s.connect((host,port))
print(s.recv(1024))
---------------------------------------------------------------------------
ConnectionRefusedError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_30292\3384143259.py in line: 7>()
5 port = 1234
6
----> 7 s.connect((host,port))
8 print(s.recv(1024))
ConnectionRefusedError: [WinError 10061] 由于目标计算机积极拒绝,无法连接。
|
可参考:https://www.pudn.com/news/6228c6f89ddf223e1ad06bc7.html
import socket
s = socket.socket()
host_name = socket.gethostname()
host = socket.gethostbyname(host_name)
print(host)
port = 1234
s.bind((host, port))
s.connect((host, port))
msg = input("What's your name?")
s.send(msg.encode('utf-8'))
print(s.recv(1024).decode('utf-8'))
s.close()
积极拒绝一般表示对方没有针对于此端口号的侦听
对方到底是个什么玩意,端口号怎么会是1234,是你自己写的服务端程序在执行吗
如果对方是在其他机器上,你的host应该填写对方的ip地址
如果是在本机上,填写"127.0.0.1"即可
socket.gethostname()这种操作是多余的
看你写了一个socket程序,目标计算机拒绝,应该是socket 的server端没有启动吧。先确定一下socket server是否启动