[英]Client.py in socket connection not working
我一直在嘗試學習 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:SI 在我的本地機器上運行它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.