[英]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 在我的本地机器上运行它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.