Forum Discussion

shogo_ajina's avatar
shogo_ajina
Honored Guest
7 years ago
Solved

Oculus Go:UDPClient is not working.

Dear community,

 I attempted to receive UDP packets using the following code.
However, the following exceptions are occurring when I launch the application on oculus go.
Is there anyone in the same situation?
Please tell me the solution.

Unity 2018.1.8f1

Exception:
System.Threading.ThreadAbortException: Thread was being aborted
at (wrapper managed-to-native) System.Net.Sockets.Socket:RecvFrom_internal (intptr,byte[],int,int,System.Net.Sockets.SocketFlags,System.Net.SocketAddress&,int&)
at System.Net.Sockets.Socket.ReceiveFrom_nochecks_exc (System.Byte[] buf, Int32 offset, Int32 size, SocketFlags flags, System.Net.EndPoint& remote_end, Boolean throwOnError, System.Int32& error) [0x00000] in <filename unknown>:0
at System.Net.Sockets.Socket.ReceiveFrom_nochecks (System.Byte[] buf, Int32 offset, Int32 size, SocketFlags flags, System.Net.EndPoint& remote_end) [0x00000] in <filename unknown>:0
at System.Net.Sockets.Socket.ReceiveFrom (System.Byte[] buffer, System.Net.EndPoint& remoteEP) [0x00000] in <filename unknown>:0

Source Code:
using UnityEngine;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

public class UDPReceive : MonoBehaviour
{
int LOCA_LPORT = 22222;
static UdpClient udp;
Thread thread;

void Start ()
{
udp = new UdpClient(LOCA_LPORT);
udp.Client.ReceiveTimeout = 1000;
thread = new Thread(new ThreadStart(ThreadMethod));
thread.Start();
}

void Update ()
{
}

void OnApplicationQuit()
{
thread.Abort();
}

private static void ThreadMethod()
{
while(true)
{
try {
IPEndPoint remoteEP = null;
byte[] data = udp.Receive(ref remoteEP); // The exception raises here.
string text = Encoding.ASCII.GetString(data);
Debug.Log(text);
} catch(Exception ex) {
Debug.LogError(ex);
}
}
}
}

Best Regards,
  • This problem is has been resolved.
    It seems to be bad that I handle UDPClient from thread not main.





    It worked after modifying as follows. 


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

    public class UDPReceive : MonoBehaviour
    {
        int LOCA_LPORT = 22222;
        UdpClient udp;
        Thread thread;

        void Start ()
        {
            udp = new UdpClient(LOCA_LPORT);
            udp.BeginReceive(ReceiveCallback, udp);
        }

        void Update ()
        {
        }

        void OnApplicationQuit()
        {
    udp.Close();
        }

        private void ReceiveCallback()
        {       
            System.Net.IPEndPoint remoteEP = null;
            byte[] rcvBytes;
            try
            {
                rcvBytes = udp.EndReceive(ar, ref remoteEP);
            } catch (System.Net.Sockets.SocketException ex)
            {
                return;
            } catch (ObjectDisposedException ex)
            {
                return;
            }
            string rcvMsg = System.Text.Encoding.UTF8.GetString(rcvBytes);
            Debug.Log(rcvMsg);
            udp.BeginReceive(ReceiveCallback, udp);
        } 
    }



     

2 Replies

  • This problem is has been resolved.
    It seems to be bad that I handle UDPClient from thread not main.





    It worked after modifying as follows. 


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

    public class UDPReceive : MonoBehaviour
    {
        int LOCA_LPORT = 22222;
        UdpClient udp;
        Thread thread;

        void Start ()
        {
            udp = new UdpClient(LOCA_LPORT);
            udp.BeginReceive(ReceiveCallback, udp);
        }

        void Update ()
        {
        }

        void OnApplicationQuit()
        {
    udp.Close();
        }

        private void ReceiveCallback()
        {       
            System.Net.IPEndPoint remoteEP = null;
            byte[] rcvBytes;
            try
            {
                rcvBytes = udp.EndReceive(ar, ref remoteEP);
            } catch (System.Net.Sockets.SocketException ex)
            {
                return;
            } catch (ObjectDisposedException ex)
            {
                return;
            }
            string rcvMsg = System.Text.Encoding.UTF8.GetString(rcvBytes);
            Debug.Log(rcvMsg);
            udp.BeginReceive(ReceiveCallback, udp);
        } 
    }



     
  • pascalpm's avatar
    pascalpm
    Honored Guest
    'private void ReceiveCallback()' should read 'private void ReceiveCallback(IAsyncResult ar)'