Socket from a docker container

Hi,
I am trying to run a socket server on my Verdin mini that will send some measurement data to my PC.
I have very limited experience with containers, so I started following this simple example Docker Containers: IPC using Sockets — Part 1 | by Aniket Pingley, Ph. D | Techanic | Medium

When trying to run the first ipc_server.py code:

#!/usr/bin/env python3
# ipc_server.py

import socket

HOST = '0.0.0.0'  
# 0.0.0.0 is a non-routable meta-address that also specifies all IP # addresses on all interfaces on the system

PORT = 9898        # Port to listen on (non-privileged ports are > 1023)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen()
    conn, addr = s.accept()
    
    with conn:
        print('Connected by', addr)
        while True:
            data = conn.recv(1024)
            if not data:
                break
            conn.sendall(data)

When trying to run this code over VSCode extension on the board, the debugger comes to the line s.accept() and then then never goes over it. The article also mentions that the port has to be exposed, which I did following this topic Expose Port in Docker by just adding 9898/tcp:9898 to the port field of the extension.

Is there something else I need to do in order to make this work? Also I don’t know if it makes any difference but my plan is to have this server run over RNDIS, as I said very little experience here so any help would be appreciated.

Thanks!

Hi @swiss ,

I looked at the article you referenced and I was able to run the client-server example with the module being the server.

When trying to run this code over VSCode extension on the board, the debugger comes to the line s.accept() and then then never goes over it.

I think this is an expected behavior of the server code if it doesn’t find any clients.

Here is what I did:

  1. Created a new Torizon Python project on VSCode, then I put your code in the main.py file;
  2. I exposed port 9898 just like you did, by adding 9898/tcp:9898 to the port field of the extension;
  3. Connected a module to VSCode using the extension;
  4. Started a debug session, the server code is deployed on the SoM and the code hangs at s.accept() ;
  5. On the host PC, I changed the ipc_client.py file so that the client looks for the IP address of the module i.e. change line 6 to
HOST = '<SoM IP addr>'
  1. Run ipc_client.py on the host machine. The server should find the client and send a response.

See if this helps you.

Best regards,
Lucas Akira

1 Like

It works when the client is running too, thanks!

As I said - almost no experience with this, I didn’t know client must be running as well.

Glad I was able to help!

Best regards,
Lucas Akira