- Home /
How can I receive UDP packets into Unity Webplayer?
I am building a simple webplayer that accept remote user input. However, I can't seem to send anything into it. The programs below compiles file and run fine, but will not receive anything. I tried port 1023 - 25000, no avail. I am running this from Unity Editor. I use free UDP Test tool to send message to 127.0.0.1. When I re-do this code for a C# console application, it works well. But not from Unity Webplayer build. Could someone help?
using UnityEngine;
using System.Collections;
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
public class UDPDemo : MonoBehaviour {
private UdpClient receivingUdpClient;
private IPEndPoint remoteIpEndPoint;
private Thread thread;
// port number
private int receivePort = 843;
// Use this for initialization
void Start () {
try {
receivingUdpClient = new UdpClient(receivePort);
} catch (Exception e) {
Debug.Log (e.ToString());
}
remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
// start the thread for receiving signals
thread = new Thread(new ThreadStart(ReceiveDataBytes));
thread.Start();
// debug
Debug.Log("Thread started");
}
void ReceiveDataBytes() {
while (true) {
Debug.Log ("Threading inside while");
// NOTE!: This blocks execution until a new message is received
Byte[] receivedBytes = receivingUdpClient.Receive(ref remoteIpEndPoint);
String receivedStringData = Encoding.ASCII.GetString(receivedBytes);
Debug.Log ("I received : " + receivedStringData);
Debug.Log ("The message was sent from " + remoteIpEndPoint.Address.ToString() + " on port number " + remoteIpEndPoint.Port.ToString());
Thread.Sleep(8);
}
}
void CloseClient() {
thread.Abort();
receivingUdpClient.Close();
}
void OnApplicationQuit() {
CloseClient();
}
}
Answer by ikelaiah · Aug 20, 2013 at 01:35 AM
Finally! I'll answer my own question again. After googling and digging into Unity forum, documentation (see here, and here, see how it begins with "In Unity 3.0...") and Answer website (see this and this one too), I found the answer! I come to a conclusion that the answer (at the moment) is ...
NOT POSSIBLE
...but there are workarounds, such as this one.
If you could please share your work arounds, it would be much appreciated as I'm facing the same issue.
Thank you
The usual workaround is to not use UDP but using TCP. That way the webplayer can open the connection and once open you can send and receive data.
UDP is connection-less, so each packet is on it's own. When Unity wants to receive a packet from a remote source, it would have to connect to the policy port, load the crossdomain.xml and check if it's allowed to receive from this source. That would be required for every packet (since connection-less).
Also i doubt that binding to "any" would work in the webplayer, but i'm not sure when and how Unity does it's policy check.
Yes, TCP makes sense than UDP in this case. I updated my answer with Flynn's thread.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
[MMO - Everquest] How to connect Unity to a non-unity Server? 1 Answer
UDP/TCP Simple example on how to send a receive messages from two Unity builds 2 Answers
How can I get deep info on configuration of Unity networking? 0 Answers
Do i need networking? 0 Answers