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();
}
}