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 and compile the in the same folder with 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\ 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\ _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\ 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\ 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\ 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\ 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\ 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\ 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\ 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\
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin]

and the source: (448 Bytes) (6.0 KB) (884 Bytes)|attachment (69 Bytes)

In the, the Server code:
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

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(("", 40005))
    while True:
        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))

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

    def on_shutdown(self):
        self.should_continue = False
        if self.tcp_socket:
            print("shutdown socket")
            # wait for the thread to finish
            while self.running:
                print("waiting for thread to finish")
            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)
                self.tcp_socket.bind(("", 40005)) 
            if i == max_attempts:
                print("failed to connect to server")
                self.tcp_socket = None

            self.client_socket, self.ip_port = self.tcp_socket.accept()
        except Exception as e:
        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:

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

Thank you very much! I understand now.

Good stuff! Thank you.

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