TCP/IP programming in extension

When I want to make an Extension that can transport data through TCP/IP, I add the Server code in the extension.py and compile the Client.py in the same folder with extension.py. When I load the Extension in Omniverse.Kit, the other function can work but the code about Server cannot work. Here is the Error:
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin] TypeError: tcpip() takes 0 positional arguments but 1 was given
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin]
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin] At:
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin] e:\wayne\extension\kit-exts-project\exts\omni.gym.4LegRL\omni\gym\4LegRL\extension.py(50): on_startup
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin] e:/wayne/software/ov/pkg/code-2022.1.0/kit/plugins/bindings-python\omni\ext\impl_internal.py(143): _startup_ext
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin] e:/wayne/software/ov/pkg/code-2022.1.0/kit/plugins/bindings-python\omni\ext\impl_internal.py(191): startup_all_extensions_in_module
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin] e:/wayne/software/ov/pkg/code-2022.1.0/kit/plugins/bindings-python\omni\ext\impl_internal.py(247): startup_all_extensions_in_module
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin] PythonExtension.cpp::startup()(2):
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin] e:\wayne\software\ov\pkg\code-2022.1.0\kit\exts\omni.kit.window.extensions\omni\kit\window\extensions\ext_commands.py(30): do
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin] e:\wayne\software\ov\pkg\code-2022.1.0\kit\extscore\omni.kit.commands\omni\kit\undo\undo.py(75): execute
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin] e:\wayne\software\ov\pkg\code-2022.1.0\kit\extscore\omni.kit.commands\omni\kit\commands\command.py(249): execute
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin] e:\wayne\software\ov\pkg\code-2022.1.0\kit\exts\omni.kit.window.extensions\omni\kit\window\extensions\utils.py(50): toggle_extension
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin] e:\wayne\software\ov\pkg\code-2022.1.0\kit\exts\omni.kit.window.extensions\omni\kit\window\extensions\ext_components.py(79): toggle
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin] e:\wayne\software\ov\pkg\code-2022.1.0\kit\exts\omni.kit.window.extensions\omni\kit\window\extensions\ext_components.py(89):
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin]

and the source:
Client.py (448 Bytes)
extension.py (6.0 KB)
Model.py (884 Bytes)
init.py|attachment (69 Bytes)

In the extension.py, the Server code:
image
and the on_startup()

I also tried adding a new thread in tcpip(), but it still couldn’t work. What the problem?
Hoping for any advice!

Hi @Wayne1407! Thanks for the code. I’ll give it a try and see what I find.

Hi Mati!

Thank you for your attention! I tried to add the TCP in omniverse.connectsample-102.1.5, and it can work successfully, but I still cannot find out the error in the Extension.
Hoping for your any suggestions!

Hi @Wayne1407

According I can see in the images, you are invoking the tcpip() as a class method: self.tcpip().
However, it is defined as a function: def tcpip():.

If this function is defined inside the same class just add the self to the definition: def tcpip(self):

Thank you for your tips!
I have modified it, but when I enabled the extension, the kit.exe blocked and I cannot saw any error in console. I don’t know whether the problem is about my computer, because it worked without blocking yesterday but opening the apps in omniverse took a longer time today and sometimes even failed, since I have just changed a new RTX3090, I don’t know where the problem is.

Hi @Wayne1407

The problem is that the tpcip method is blocking (there is an infinite loop) and it doesn’t let the Omniverse application to continue with the loading process…
A possible solution is to move it to a secondary thread…

import threading
threading.Thread(target=self.tpcip).start()

Hi
I have tried, added the code below into “def on_startup(self, ext_id)”, but it blocked still. Can you have a try? I don’t know if it’s a problem with my computer.

    self.tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    self.tcp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
    
    self.tcp_socket.bind(("127.0.0.1", 40005))
    
    self.tcp_socket.listen(128)
    while True:
        print("connecting")
        self.client_socket, self.ip_port = self.tcp_socket.accept()
        print("Client", self.ip_port, "connected")
        
        self.t1 = threading.Thread(target=recvd, args=(self.client_socket, self.ip_port))
       
        self.t1.setDaemon(True)
        self.t1.start()

Hi @Wayne1407

Again the problem is the infinite loop that is blocking the main application thread…

Next, there is a quick implementation of the threaded method to work with the socket.
Note that the while loop and the recv are inside the threaded method.
Also, there are some flags to handle the secondary thread and the extension shutdown…

import omni.ext
import time
import socket
import threading


class Extension(omni.ext.IExt):
    def on_startup(self, ext_id):

        self.running = False
        self.should_continue = False
        self.tcp_socket = None
        threading.Thread(target=self.tcpip).start()

    def on_shutdown(self):
        self.should_continue = False
        if self.tcp_socket:
            print("shutdown socket")
            self.tcp_socket.shutdown(socket.SHUT_RDWR)
            # wait for the thread to finish
            while self.running:
                print("waiting for thread to finish")
                time.sleep(0.1)
            self.tcp_socket = None

    def tcpip(self):
        self.tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        max_attempts = 10
        for i in range(max_attempts):
            print("attempting to connect to server:", i)
            try:
                self.tcp_socket.bind(("127.0.0.1", 40005)) 
                break
            except:
                time.sleep(1)
            if i == max_attempts:
                print("failed to connect to server")
                self.tcp_socket = None
                return
        self.tcp_socket.listen(1)

        print("connecting...")
        try:
            self.client_socket, self.ip_port = self.tcp_socket.accept()
        except Exception as e:
            print(e)
            return
        print("client", self.ip_port, "connected")
        
        self.running = True
        self.should_continue = True
        while self.should_continue:
            data = self.client_socket.recv(1024)
            if not data:
                break 
            print(data)

        print("client disconnected")
        self.tcp_socket.close()
        self.tcp_socket = None
        self.running = False

Thank you very much! I understand now.

Good stuff @toni.sm! Thank you.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.