Question by
eubrunomiguel · Jan 24, 2017 at 10:01 PM ·
networkingmultiplayermultiplayer-networkingudp
UDP Client receiving Disposed error
To be really honest I do not know what is going on. The exception relate to file, but the code, running on a separate thread is dealing with udp connection. Anyone has any idea?
I am receiving the follow error:
Error receiving: System.ObjectDisposedException: The object was used after being disposed. at System.Net.Sockets.UdpClient.CheckDisposed () [0x00000] in <filename unknown>:0 at System.Net.Sockets.UdpClient.Receive (System.Net.IPEndPoint& remoteEP) [0x00000] in <filename unknown>:0 at GameNetwork.ReceiveData () [0x00018] in C:\Users\epvaverka\Desktop\udp server\Assets\Scripts\GameNetwork.cs:66
My Code:
void InitConnection()
{
IP="192.168.1.136";
port = 7171;
remoteEndPoint = new IPEndPoint(IPAddress.Parse(IP), port);
client = new UdpClient();
receiveThread = new Thread(new ThreadStart(ReceiveData));
receiveThread.IsBackground = true;
receiveThread.Start();
}
// receive thread
void ReceiveData()
{
while (true)
{
try
{
IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
byte[] data = client.Receive(ref anyIP);
UInt32 byteSize = (UInt32)data.Length;
//Debug.Log("Receiving incoming message size: " + byteSize);
if(Core.gameStatus == GameState.GAME_PAUSED){
Debug.Log("Deleting incoming message size: " + byteSize);
continue;
}
if (byteSize > GameVariables.MINPACKETBYTESIZE)
{
MessageParcel message = new MessageParcel();
message.mBuffer = data;
message.mByteSize = byteSize;
Core.gMail.addIncomingMessage(message);
}
}
catch (Exception error)
{
Debug.Log ("Error receiving: " + error);
}
}
}
Comment