Python Forum
socket programming ConnectionRefusedError error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Networking (https://python-forum.io/forum-12.html)
+--- Thread: socket programming ConnectionRefusedError error (/thread-18324.html)



socket programming ConnectionRefusedError error - srm - May-13-2019

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???


RE: socket programming ConnectionRefusedError error - buran - May-13-2019

It would probably help if you post also your code


RE: socket programming ConnectionRefusedError error - srm - May-15-2019

(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 >>>



RE: socket programming ConnectionRefusedError error - LavaCreeperKing - May-16-2019

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.