Connect to Socket on localhost

15,864

What I didn't expected was that localhost/127.0.0.1 did not work.

Ping results
100% package loss

I had a malformatted entry in my hosts file.

Share:
15,864

Related videos on Youtube

luckydonald
Author by

luckydonald

Just an wastelander.

Updated on June 04, 2022

Comments

  • luckydonald
    luckydonald almost 2 years

    I have trouble connecting to my own socket on localhost.
    s.connect(('127.0.0.1', 4458)) (or "localhost") will just take forever,
    and eventually timeout with TimeoutError: [Errno 110] Connection timed out

    It should open port 4458, another script will then send some chars to it. Both scripts are supposed to run on the same Raspberry Pi, while 'server' one will execute with sudo (to access the GPIOs) and one without, being a chat bot.

    I have no trouble running the server on the Pi (with python 3.4.1) and the client on my Laptop (mac, python 3.4.2).

    Also it does work in reverse direction, server script on the laptop and client on the Raspberry.

    As final test, it works with both, the server and the client on the said macbook.

    Just server + client on the Pi does not work.

    The program freezes

    My shortened code if it helps:

    # $ sudo python3 server.py
    
    __author__ = 'luckydonald'
    import socket # server
    import time # wait for retry
    import threading
    class Server(threading.Thread):
        port = 4458;
        QUIT = False
    
         def run(self):
            s = socket.socket()
            failed = True
            print ("Starting Server on Port %d" % (self.port))
            while failed:
                try:
                    s.bind(("", self.port))
                except Exception as err:
                    print(err)
                    print("Port assignment Failed. Retring in 1 second.")
                    time.sleep(1)
                else:
                    failed = False
                    print("Success.")
            while not self.QUIT:
                print("Listening!")
                conn, addr = s.accept() # freezes here
                print("Got something: %s , %s" %(str(conn), str(addr)))
                while not self.QUIT:
                    result = conn.recv(1)
                    print("Got result: " + str(result))
    server = Server();
    server.daemon = True
    server.run();
    # server.start();
    

    And for the client:

    # python3 cilent.py
    s = socket.socket()
    print("connecting...")
    s.connect(("localhost",4458)) # also tried "172.0.0.1" # freezes here
    print("connected!")
    s.sendall("+".encode("utf-8"))
    s.sendall("-".encode("utf-8"))
    s.close()
    

    It will result in this:

    Picture of console output

    • Maulik
      Maulik over 3 years
      Hello luckydonald can you tell me that what exact solution you find it for this problem because i am facing the same issue.
    • luckydonald
      luckydonald over 3 years
      @MaulikBarad the accepted answer was my solution.