Search code examples
pythonsocketslego-mindstormslego-mindstorms-ev3

s.bind((hostIPAddress,22)) OSError: 99


I am trying to get my robot to communicate with my PC via sockets by using local IP addresses on my own home network (not devices outside my network). The robot is acting as the server and my own PC is acting as the client/host. I don't really know what ports are open on my robot but I do definitely know that port 22 on the robot is open (which is the SSH port). The robot is a lego EV3 robot apart from it has had some ev3python software put onto it. When I run my program I am getting the following error on the server (my robot):

File "/home/robot/server/server.py", line 24, in <module>
  File "socket/socket.py", line 50, in bind
OSError: 99

Here is my annotated code

#!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import Motor
from pybricks.parameters import Port

# Create your objects here

# Initialize the EV3 Brick.
ev3 = EV3Brick()

# Initialize a motor at port B.
#test_motor = Motor(Port.B)

import socket

hostIPAddress = "xx.xx.xx.xx" #the local IP address of my PC on my home network 
backlog = 1
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((hostIPAddress,22))#22 is the port number I am using. (THIS IS ALSO THE LINE WHERE THE ERROR IS COMING FROM) 
s.listen(backlog)
try:
    client, address = s.accept()
    while 1:
        data = client.recv(size)
        if data:
            draw_text(89, 64, data, text_color=Color.RED, background_color=None)
            clear()

        if data == "beep":
            ev3.speaker.beep(frequency=1000, duration=500)
        
        #if data == "move":
            #test_motor.run_target(500, 90)

except: 
    print("Closing socket") 
    client.close()
    s.close()

Here is the code for the client/host (I don't this will help but it might do):

import socket

serverIPAddress = "xx.xx.xx.xx" #the local IP address of my robot on my home network 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((serverIPAddress,22))
while 1:
    text = input()
    if text == "quit":
        break
    s.send(bytes(text, 'UTF-8'))
s.close()

Solution

  • hostIPAddress = "xx.xx.xx.xx" #the local IP address of my PC on my home network 
    backlog = 1
    size = 1024
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((hostIPAddress,22))#22 is the port number I am using. (THIS IS ALSO THE LINE WHERE THE ERROR IS COMING FROM) 
    s.listen(backlog)
    

    There are at least two problems here:

    You're trying to bind the socket to the IP address of a remote system. That's not what bind does; bind sets the local address of the socket (usually used only for listening sockets like yours). Since you probably don't care what IP address the robot listens on, you can use 0.0.0.0 which means "any IP".

    Next, you're trying to listen on a port that you know is already used by another service (ssh on port 22). You should decide on another port for your service. I recommend using a port above 1024, as ports below that are often "privileged" and require root to listen on. As long as there is no firewall involved, any port will do.

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind(("0.0.0.0", 5555))