0

So I made a simple client / server script using python sockets, and it's been working great on my PC. I decided I'd try to move the server over to a raspberry pi so it would be dedicated, and it's not working out so great.

server.py

import socket
from modules import *


def server_program():
    host = socket.gethostbyname(socket.gethostname())
    port = 5000 

    server_socket = socket.socket()
    server_socket.bind((host, port))
    print("Server is online. Awaiting commands. ")

    while True:
        server_socket.listen(2)
        conn, address = server_socket.accept()
        print("Connection from: " + str(address))
        data = conn.recv(1024).decode()
        print("\tReceived data:", data)
        conn.send("success".encode()) 

        execute_data(data, server_socket)


if __name__ == '__main__':
    print()
    server_program()

client.py

import socket


def client_program():
    # host = socket.gethostname()  # local device: This works on both devices
    # host = '192.168.0.30'        # working computer: This works on my PC
    host = '192.168.0.44'          # raspberry pi: This does NOT work on my Pi
    port = 5000

    client_socket = socket.socket()
    client_socket.connect((host, port))

    message = "roku remote back"

    client_socket.send(message.encode())
    data = client_socket.recv(8).decode() 

    print('Received from server: ' + data) 

    client_socket.close()

if __name__ == '__main__':
    print()
    client_program()
    try:
        pass # This is where client_program() was, I moved it out to see the full error
    except ConnectionRefusedError:
        print("The server doesn't appear to be online.\n")

I'm getting this connection error, but I really don't understand why. I'm not super conversant in RaspPi, so I'm sure there's something I'm missing. Thanks for any and all help!

2
  • Connection refused has three possible causes. 1. Wrong IP. 2. Right IP, wrong port. 3. Right IP, right port, listener not listening.
    – Dougie
    Jan 15, 2021 at 20:48
  • I just double-checked the IP and port, so I'm assuming the listener isn't listening. Is there a reason why the same code successfully listens on my PC but not on my Pi?
    – JShoe
    Jan 16, 2021 at 1:54

1 Answer 1

0

Well, I have no idea why or how this was the issue, but following the advice of another post, changing host in server.py to '' fixed it.

1
  • What host to fix?
    – Ingo
    Jan 16, 2021 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.