luckydonald
luckydonald

Reputation: 6856

Connect to Socket on localhost

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

Upvotes: 5

Views: 26274

Answers (2)

Developer33
Developer33

Reputation: 97

You should check for below items

  1. there is an installed internet information services
  2. iis is running
  3. firewall is grants required ports for running the python.exe

Upvotes: 0

luckydonald
luckydonald

Reputation: 6856

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.

Upvotes: 2

Related Questions