Python Forum

Full Version: socket programming ConnectionRefusedError error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am working with IDLE for python 3.7.2
when i am trying to get connected through socket programming between server and client
through IDLE shell it gives me following error
Traceback (most recent call last):
File "C:/Users/Administrator/AppData/Local/Programs/Python/Python37/Client4.py", line 13, in <module>
s.connect((host, port))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

but the it is connected through command prompt then it is working fine....

Can anybody explain???
It would probably help if you post also your code
(May-13-2019, 11:54 AM)buran Wrote: [ -> ]It would probably help if you post also your code

server file:
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 file:
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.connect((host, port))
print (s.recv(1024))

s.close()                     # Close the socket when done
error is
Error:
Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\Client1.py", line 6, in <module> s.connect((host, port)) ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it >>>
Change host to host = 'localhost' on client.
When you try to get the socket host name the socket first needs a host. If you are not connected to a server, there is no host to return a host name. The socket is just an empty socket until you connect to a server. If the client and server are on the same computer host will be 'localhost' I do't know yet what you do if they are on different computers. Also you need to do this:
from socket import socket, AF_INET, SOCK_STREAM
sock = socket(AF_INET, SOCK_STREAM)
You might also need to change host = '' on the server code.