UDP between C# in Unity and a program in C++

Hi,

I am trying to send data via UDP from a program written in C++ (that I use for tracking) to Unity using .Net in C#. I am kind of lost, all my attempts did not work so far and Unity crashes quite often. Here my code, could anyone tell me were I am wrong ?

Server code in C++

#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")

int sendUDP()
{
	WSADATA WSAData; //start WSAStartup()
    SOCKET sock; //init socket
    SOCKADDR_IN sin; //infos socket
    WSAStartup(MAKEWORD(2,2), &WSAData); //enable socket use
    sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); //create socket
    sin.sin_addr.s_addr = htonl(INADDR_ANY);
    sin.sin_family = AF_INET; //type socket
    sin.sin_port = htons(28280); //port nb
	char SendBuf[32]="From port 28280";

    while(1)
    {
		sendto(sock, SendBuf, 32, 0, (SOCKADDR *)&sin, sizeof(sin));
    }
	closesocket(sock);
	WSACleanup();
    return 0;
}

Client code C# in Unity

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

public class capUDP : MonoBehaviour {

static IPEndPoint ep = new IPEndPoint(IPAddress.Any, 0);
static UdpClient udpClient = new UdpClient(28280);
static byte[] receiveBytes = new byte[32]; 
static string returnData = "";
	
	void Start () {
		Thread receiveThread = new Thread(ReceiveData);
        receiveThread.IsBackground = true;
        receiveThread.Start();
	}
	
	void Update () {
		Debug.Log(returnData);
	}
	
	void ReceiveData()
	{
		Debug.Log("receiving...");
		receiveBytes = udpClient.Receive(ref ep); 
		returnData = Encoding.ASCII.GetString(receiveBytes);
	}
}

Thanks,
Gratoo

Just two guesses:

In C you should initialize your char-array with 0. It might send whatever is in memory and that might not be ASCII?

Your returnData is used in two threads: one modifies it when something arrives and one prints it. Maybe you should lock() it.

Thanks for your reply TobiasS. You were right about the returnData into Update(), it was not a good idea at all :stuck_out_tongue: But to make it work I also changed “htonl(INADDR_ANY);” by “inet_addr(“xxx.xxx.x.xxx”);” in the server code.

So here the working code for the client:

	void Start () {
	}
	
	void Update () {
        receiveThread = new Thread(ReceiveData);
        receiveThread.IsBackground = true;
        receiveThread.Start();
        receiveThread.Abort();
	}
	
	void ReceiveData()
	{
		receiveBytes = udpClient.Receive(ref ep); 
		returnData = Encoding.ASCII.GetString(receiveBytes);
		if(returnData != "") Debug.Log(returnData);
	}

But I guess the code is far from perfect yet, what do you think ? Do I really have to specify my IP ? (I would like my application to run easily on another computer)

Thanks a lot for your help !
Gratoo

Hi Gratoo,
I am also working on Unity c# to c++ communication. You have faced the same problem that I am facing now. Did you find the solution. Can you put it here or write an article about it on answers.unity3d.com to help fellow developers.

1 Like