07-19-2018 06:28 AM
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
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);
}
}
}
}
Solved! Go to Solution.
07-22-2018 07:41 AM
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);
}
}
07-22-2018 07:41 AM
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);
}
}
09-27-2018 08:46 PM
'private void ReceiveCallback()' should read '
private void ReceiveCallback(IAsyncResult ar)'