- Home /
I cant read from the sockets properly - exception 0x80004005: An invalid argument was supplied
private void ThreadMethod()
{
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 36864);
UdpClient udp = new UdpClient();
UnityEngine.Debug.Log("Entering Thread method...");
while (true)
{
Byte[] receiveBytes = new byte[0];
try
{
// Set up receiver
receiveBytes = udp.Receive(ref RemoteIpEndPoint);
UnityEngine.Debug.Log("receiving bytes...");
}
catch (Exception err)
{
UnityEngine.Debug.Log("UDP Client Socket Exception Error: " + err);
}
// Lock object so that multiple threads dont access data at the same time
lock (lockObject)
{
UnityEngine.Debug.Log("locked onto package...");
returnData = Encoding.ASCII.GetString(receiveBytes);
UnityEngine.Debug.Log("data is "+returnData.ToString());
handInfo items = JsonUtility.FromJson<handInfo>(returnData);
UnityEngine.Debug.Log("Array is "+items);
if (items != null) { translateData = true; }
else {UnityEngine.Debug.Log("Cannot reach data packages...");}
}
}
}
Answer by Bunny83 · Sep 29, 2020 at 08:13 AM
Remove this line:
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 36864);
and add this one inside the while loop before your receive call:
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
This variable is meant to be set by the Receive method. The IP and port represents the sending port, not the receiving port. If you want to listen on port 0x9000 (36864) you should do
UdpClient udp = new UdpClient(36864);
Keep in mind that the sending port nowadays can't really be choosen explicitly since almost everybody is behind a NAT router that exploits the source port for client identification. So the router will choose a random, currently unused source port in order to match a returned packet with the sending client.
Answer by samwood9797 · Sep 30, 2020 at 12:47 AM
Thanks for the reply, I have made those changes but now the program isn't moving past the receive method, returning the following exception:
UDP Client Socket Exception Error: System.Threading.ThreadAbortException at (wrapper managed-to-native) System.Net.Sockets.Socket.ReceiveFrom_internal(intptr,byte*,int,System.Net.Sockets.SocketFlags,System.Net.SocketAddress&,int&,bool)
I have attached the entire script attached to a cube gameObject (Move.cs) if needed. Cheers