- Home /
Receiving UDP data in Unity?
Hello. I am working on a project, and I have a device that sends data of its rotation (basically data that tells you exactly how the device is rotated on the X, Y, Z axis, pretty much like a head tracker). I need to receive the data in Unity. Unity needs to be the server, and the device that is sending the data is the client. I made it work perfectly in a standard C# console app, but whenever I try to replicate it in Unity, Unity just stops working, or I get some sort of error, or it just doesn't work. I will post the C# code that worked for me below, and if anyone knows how to adjust the code for Unity, and make it work, it would mean a lot. :)
Thanks!
C# Application Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace BasicUDPApp
{
class SimpleUDPServer
{
public static void Main()
{
int receive;
byte[] data = new byte[1024];
IPEndPoint IPEndPoint = new IPEndPoint(IPAddress.Any, 8000);
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
newsock.Bind(IPEndPoint);
Console.WriteLine("Waiting for a client...");
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)(sender);
while (true)
{
data = new byte[1024];
receive = newsock.ReceiveFrom(data, ref Remote);
Console.WriteLine(Encoding.ASCII.GetString(data, 0, receive));
}
}
}
}
Your answer
Follow this Question
Related Questions
What's the best approach for creating a secure authorative server? 0 Answers
Multicast group or a list with all players ? 0 Answers
Network how to send data/activate function 1 Answer
Totally lost with UNET - Client to Server Networking 3 Answers
Error: The requested address is not valid in its context. 1 Answer