简体   繁体   中英

Client.py in socket connection not working

I have been trying to study the basics of socket programming in python. I have tried the code and the client connection doesnt seems to be working.

The code of 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

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 .

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.

P:SI am running this on my local machine

在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM