简体   繁体   English

套接字连接中的 Client.py 不起作用

[英]Client.py in socket connection not working

I have been trying to study the basics of socket programming in python.我一直在尝试学习 python 中的套接字编程基础知识。 I have tried the code and the client connection doesnt seems to be working.我已经尝试了代码,客户端连接似乎不起作用。

The code of server.py 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()

The code of client.py client.py 的代码

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

When i run both client and server files i get the error s.connect((host, port)) ConnectionRefusedError: [Errno 111] Connection refused .当我同时运行客户端和服务器文件时,我收到错误s.connect((host, port)) ConnectionRefusedError: [Errno 111] Connection refused

When i run the server file the connection with the port 12345 is active but the client file is not connecting to it.I have tried modifying the code based on the suggestions online but still no luck.Any help to make this work is really appreciated..Thank you in advance.当我运行服务器文件时,与端口 12345 的连接处于活动状态,但客户端文件未连接到它。我已尝试根据在线建议修改代码,但仍然没有成功。非常感谢任何有助于完成这项工作的帮助。 。先感谢您。

P:SI am running this on my local machine P:SI 在我的本地机器上运行它

在此处输入图像描述

Try Using another port as it seems to be an reserved port尝试使用另一个端口,因为它似乎是一个保留端口

Also在此处输入图像描述

It seems an malware uses it So try disabling your antivirus as it may reset the connection似乎有恶意软件使用它所以请尝试禁用您的防病毒软件,因为它可能会重置连接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅当client.py运行时,Python Socket errno 10054 - Python Socket errno 10054 only when client.py runs Client.py 没有连接到 server.py - Client.py doesnt connect to server.py discord\\client.py 错误中的python 脚本上的回溯错误? - Traceback errors on python script within the discord\client.py error? Python3.5 / http / client.py SyntaxError:语法无效 - Python3.5/http/client.py SyntaxError: invalid syntax Windows 10 不允许 Python server.py 与 client.py 连接 - Windows 10 not allowing Python server.py to connect with client.py /usr/lib64/python3.4/http/client.py的编码问题 - Encoding problem with /usr/lib64/python3.4/http/client.py Dockerfile-python:无法打开文件'/usr/app/client.py':[Errno 2]没有这样的文件或目录 - Dockerfile - python: can't open file '/usr/app/client.py': [Errno 2] No such file or directory 如何从我的本地计算机(client.py)发送一个简单的 UDP 消息到远程服务器 pythonanywhere(server.py) - How to send a simple UDP message from my local computer(client.py) to a remote server pythonanywhere(server.py) 我得到一个 **NumPyClient.fit 没有返回包含 3 个元素的元组。** 运行 client.py 文件时出现错误。 可能是什么问题呢? - I'm getting an **NumPyClient.fit did not return a tuple with 3 elements.** error when I run client.py file. What could be the problem? Python套接字等待客户端连接 - Python socket wait for client connection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM