Python, Connectin Refused 10061

34,240

Taking a guess at your indentation, and running your code… it works just fine.* (As long as I type in 127.0.0.1 when it asks me for the IP.)

Of course the second time I run the client (if I haven't restarted the server) I get a connection-refused error. But that's just because you've coded a server that immediately quits as soon as it gets the first connection. So the second time you run the client, there is no server, so the OS rejects the connection.

You can always run the server again, which lets you run the client one more time. (Except that the server may get a 10048 error when it tries to bind the socket, because the OS is keeping it around for the previous owner. If you see that, look at SO_REUSEADDR in the docs.)

* By "works just fine" I mean that it connects, and prints out the following before quitting:

Socket created
127.0.0.1
8934
You are connected to 127.0.0.1 with IP adress of 127.0.0.1

Obviously it never sends anything to the server or receives anything back, because the server has no send or recv calls, or anything else.

Share:
34,240
cgasser
Author by

cgasser

Updated on July 09, 2022

Comments

  • cgasser
    cgasser almost 2 years

    I keep getting this error

    [Errno 10061] No connection could be made because the target machine actively refused it.

    I'm running Windows 7 64 bit, no virus or protection software, and python is allowed through my firewall (I've also tried turning my firewall completely off but same result). When I run the server and use telnet it connects just fine. When I try to connect to the server with the client it fails. Any suggestions as to what I could try to fix this? If you need more information just ask and I'll provide.

    Client Code

    import socket
    import sys
    def main():
       host = ""
       port = 8934
       message = "Hello World!"
    
       host = raw_input("Enter IP: ")
       #Create Socket
       try:
          s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
       except socket.error, msg:
          print "Failed to create socket. Error code: %s Error Message: %s"%(str(msg[0]),msg[1])
          sys.exit()
       print "Socket created"
    
       #Connec to Server
       print host
       print port
       s.connect((host,port))
       print "You are connected to %s with IP adress of %s"%(host,host)
    
       #Send Data
       try:
          s.sendall(message)
       except socket.error:
          print "Failed to send."
       #Receive Data
          reply = s.recv(4096)
    
    if __name__ == "__main__":
       main()
    

    Server Code

    # !usr/bin/python
    
    import socket
    import sys
    
    HOST = ""
    PORT = 8934
    
    def main():
       #Setup socket
       try:
          s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
       except socket.error,msg:
          print "Unable to create socket"
          sys.exit()
       print "Socket created."
    
       #Bind to adress
       try:
          s.bind((HOST,PORT))
       except socket.error,msg:
          print "Bind failed. Closing..."
          sys.exit()
       print "Socket bound."
    
       #Start listening
       s.listen(10)
       print "Socket Listening"
    
       #Accept connection
       conn, addr = s.accept()
       print "Connected to %s:%s"%(addr[0],addr[1])
    
    if __name__ == "__main__":
       main()