WinError 10038 an operation was attempted on something that is not a socket, connection with Unity

141 views Asked by At

I am trying to send data from a Pose Estimation program (MediaPipe) with a TCP socket connection in Unity. I get the error 10038 on the client side.

Client code:

import socket
import PoseEstimation as pm
import cv2

host, port = "127.0.0.1", 25001
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
cap = cv2.VideoCapture(0)
detector = pm.PoseEstimatior()

def sendData(lmString, sock, host, port):
    #sock.connect((host, port))
    sock.send(lmString.encode("utf-8"))
    response = sock.recv(1024).decode("utf-8")
    print(response)
    #sock.close()

while cap.isOpened():
    _, img = cap.read()
    img = detector.findPose(img)
    lmList, complete = detector.getPosition(img)
    lmString = ','.join([','.join(map(str, inner_list)) for inner_list in lmList])
    
    if complete:
        sendData(lmString, sock, host, port)

    cv2.imshow("Image", img)
    
    if cv2.waitKey(1) == ord('q'):
        break
sock.close()
cap.release()
cv2.destroyAllWindows()

Here is my server code. I took a large part from a Youtube video (link to the video and GitHub code: https://www.youtube.com/watch?v=Dm0CiAiZk14 and https://github.com/ConorZAM/Python-Unity-Socket/blob/master/MyListener.cs)

using System.Net;
using System.Net.Sockets;
using System.Text;
using UnityEngine;
using System.Threading;
using System.Linq;

public class MyListener : MonoBehaviour
{
    Thread thread;
    public int connectionPort = 25001;
    TcpListener server;
    TcpClient client;
    bool running;
    public GameObject[] Body;
    Vector3[] position = Enumerable.Repeat(Vector3.zero, 33).ToArray();

    void Start()
    {
        // Receive on a separate thread so Unity doesn't freeze waiting for data
        ThreadStart ts = new ThreadStart(GetData);
        thread = new Thread(ts);
        thread.Start();
    }

    void GetData()
    {
        // Create the server
        server = new TcpListener(IPAddress.Any, connectionPort);
        server.Start();

        // Create a client to get the data stream
        client = server.AcceptTcpClient();

        // Start listening
        running = true;
        while (running)
        {
            Connection();
        }
        server.Stop();
    }

    void Connection()
    {
        // Read data from the network stream
        NetworkStream nwStream = client.GetStream();
        byte[] buffer = new byte[client.ReceiveBufferSize];
        int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize);

        // Decode the bytes into a string
        string dataReceived = Encoding.UTF8.GetString(buffer, 0, bytesRead);

        // Make sure we're not getting an empty string
        //dataReceived.Trim();
        if (dataReceived != null && dataReceived != "")
        {
            // Convert the received string of data to the format we are using
            position = ParseData(dataReceived);
            nwStream.Write(buffer, 0, bytesRead);
        }
    }

    // Use-case specific function, need to re-write this to interpret whatever data is being sent
    public static Vector3[] ParseData(string dataString)
    {
        Debug.Log(dataString);

        // Split the elements into an array
        string[] stringArray = dataString.Split(',');
        Vector3[] vectors = new Vector3[33];

        for (int i = 0; i < 33; i++)
        {
            float x = float.Parse(stringArray[1 + (i * 4)]) / 100;
            float y = float.Parse(stringArray[2 + (i * 4)]) / 100;
            float z = float.Parse(stringArray[3 + (i * 4)]) / 100;
            vectors[i] = new Vector3(x, y, z);
        }

        return vectors;
    }

    void Update()
    {
        // Set this object's position in the scene according to the position received
        for (int i = 0; i < 33; i++)
        {
            Body[i].transform.localPosition = position[i];
        }
    }
}
0

There are 0 answers