【问题标题】:Client.py in socket connection not working套接字连接中的 Client.py 不起作用
【发布时间】:2021-02-02 07:27:11
【问题描述】:

我一直在尝试学习python中套接字编程的基础知识。我已经尝试了代码,客户端连接似乎不起作用。

server.py的代码

import socket               # Import socket module
s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                # Reserve a port for your service.
s.bind((host, port))        # Bind to the port

s.listen(5)                 # Now wait for client connection.
while True:
c, addr = s.accept()     # Establish connection with client.
print 'Got connection from', addr
c.send('Thank you for connecting')
c.close()

client.py的代码

import socket 
s = socket.socket()        
host = socket.gethostname() 
port = 12345                
s.connect((host, port))
print s.recv(1024)
s.close() 

当我同时运行客户端和服务器文件时,我收到错误 s.connect((host, port)) ConnectionRefusedError: [Errno 111] Connection refused

当我运行服务器文件时,与端口 12345 的连接处于活动状态,但客户端文件未连接到它。我已尝试根据在线建议修改代码,但仍然没有运气。任何有助于完成这项工作的方法是真的很感激..提前谢谢你。

P:S 我在本地机器上运行它

【问题讨论】:

标签: python sockets


【解决方案1】:

尝试使用另一个端口,因为它似乎是保留端口

还有

似乎有恶意软件在使用它所以请尝试禁用您的防病毒软件,因为它可能会重置连接

【讨论】:

  • 我以前试过这个,但我仍然得到错误。
  • harish 我已经厌倦了不同的端口但仍然没有用。我在 ubuntu 机器上运行它
猜你喜欢
  • 2016-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-01
  • 1970-01-01
  • 2020-05-20
  • 2023-01-20
  • 2016-09-25
相关资源
最近更新 更多