Hello,
I would like to receive data by UDP (address, port) like that script in C# in Unity.
using UnityEngine;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using UnityEngine.UI;
public class Art_Net : MonoBehaviour {
private byte [] UDP;
string targetIP = "127.0.0.1";
private int port = 6454;
// receive DMX
private Thread udpThread;
private UdpClient udpClient;
private bool running;
private int universe = 0;
public void Start() {
target_IP = IPAddress.Parse(targetIP);
ipEndPoint = new IPEndPoint (target_IP, port);
// put Artnet header into UDP frame
UDP = createEmptyArtnetPackage ();
// receive DMX
running = true;
udpThread = new Thread (new ThreadStart(receive));
udpThread.Start ();
}
public void Update()
{
Debug.Log("Universe : "+GetUniverse().ToString());
for (int x = 0; x < 512; x++)
Debug.Log("Channel : "+x+" / Value : "+GetValueChannel(x).ToString());
}
public int GetUniverse()
{
return universe;
}
// send DMX
public byte GetValueChannel(int channel)
{
return UDP [channel + 18];
}
//send a single DMX value
public void send(int channel, byte value){
UDP[channel + 18] = value;
Socket myClient = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
myClient.SendTo (UDP, ipEndPoint);
myClient.Close ();
}
//send a whole received DMX package
public void send(byte [] values){
Socket myClient = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
myClient.SendTo (UDP, ipEndPoint);
myClient.Close();
}
public byte [] createEmptyArtnetPackage(){
byte [] artnetFrame = new byte[530];
// start value 0 for each channel in the UDP frame
for (int i = 0; i < 530; i++) {
artnetFrame [i] = 0;
}
return artnetFrame;
}
// receive DMX
private void receive()
{
IPEndPoint RemoteIpEndPoint;
try{
udpClient = new UdpClient(port);
RemoteIpEndPoint = new IPEndPoint(IPAddress.Any,0);
while(running)
{
if(udpClient.Available > 500)
{
byte[] data;
data = udpClient.Receive(ref RemoteIpEndPoint);
if (data.Length >= 530)
{
universe = data[14];
for (int i = 0; i < 512; i++)
{
UDP[i + 18] = data[i + 18];
}
// Debug.Log("Data14 :" + data[14].ToString());//Universe
// Debug.Log("Data15 :" + data[15].ToString()); //Net
send(UDP);
}
Thread.Sleep(20);
}
else{
Thread.Sleep(20);
// Debug.Log("No Message received");
}
}
}
catch{
Debug.Log ("Port 6454 already reserved.");
}
}
void OnApplicationQuit(){
Debug.Log ("Quit Playmode");
if (udpThread != null) {
udpThread.Abort ();
}
if (udpClient != null) {
udpClient.Close ();
}
}
}
I want to translate that script in C++, but I don’t know use UDP with Unreal.
Someone can explain me what I have to use for do something similar ?
Thanks for your patience.