Python Forum
socket programming ConnectionRefusedError error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
socket programming ConnectionRefusedError error
#1
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???
Reply
#2
It would probably help if you post also your code
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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 >>>
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  socket.error Karin 0 2,722 Nov-11-2019, 08:53 AM
Last Post: Karin
  Raw Socket Error therealherby 1 3,746 Oct-24-2019, 06:33 AM
Last Post: markfilan
  socket programming (browser) kunz 8 5,198 Dec-18-2018, 10:13 AM
Last Post: siripriya
  Python Socket programming with packets sourabhjaiswal92 1 4,092 Sep-18-2018, 06:24 AM
Last Post: martingever
  Python socket : Error receive data quanglnh1993 1 12,953 Mar-14-2018, 11:59 AM
Last Post: avorane
  socket programming user2103 2 3,579 Dec-19-2017, 11:52 AM
Last Post: user2103

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020