- Home /
EndOfStreamException: Failed to read past end of stream.
Trying to run an UDP client server setup and I am sending and receiving the same amount of data but am getting the error "EndOfStreamException: Failed to read past end of stream. " when I go to deserialize the buffer.
Here is the code:
Recieve:
void ReceiveCallback (IAsyncResult result) {
IPEndPoint endpoint = (IPEndPoint)result.AsyncState;
byte[] data = GetUdp ().EndReceive (result, ref endpoint);
Debug.Log ("SERVER " + "Recieved Data " + "Packet Size: " + data.Length);
Network.singleton.Recieve (data);
Receive ();
}
public void Receive () {
IPEndPoint endpoint = new IPEndPoint (IPAddress.Any, 0);
GetUdp ().BeginReceive (new AsyncCallback (ReceiveCallback), endpoint);
}
Deserialize:
public object Deserialize (byte[] bytes) {
BinaryFormatter formatter = new BinaryFormatter ();
using (var stream = new System.IO.MemoryStream (bytes)) {
stream.Seek (0, System.IO.SeekOrigin.Begin);
Debug.Log ("FORMATTER " + "Deserializing " + "Stream Length: " + stream.Length);
return formatter.Deserialize (stream);
}
}
Here is the console output:
CLIENT Sent Data Packet Size: 160
SERVER Recieved Data Packet Size: 160
FORMATTER Deserializing Stream Length: 160
EndOfStreamException: Failed to read past end of stream.
Error Info:
System.IO.BinaryReader.ReadByte () (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/BinaryReader.cs:293) System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadNextObject (System.IO.BinaryReader reader) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:142) System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObjectGraph (BinaryElement elem, System.IO.BinaryReader reader, Boolean readHeaders, System.Object& result, System.Runtime.Remoting.Messaging.Header[]& headers) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:110) System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.NoCheckDeserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:179) System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:136) Formatter.Deserialize (System.Byte[] bytes) (at Assets/Formatter.cs:24) Network.Process (System.Byte[] data) (at Assets/Network.cs:40) Network+c__Iterator0.MoveNext () (at Assets/Network.cs:32) UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
What's the data / object / class that you serialized on the sender side? Are you sure that both, sender and receiver, use the exact same class(es) / assemblies? The BinaryFormatter stores the exact assembly that contains the classes. If the versions aren't the same you can't deserialize it on the other side.
They both use the same method and the method serializes the same type of object from a common network class.
public void Send (string targetID, string script, string method, params object[] data) {
byte[] packet = Formatter.singleton.Serialize (new Network.Packet ("Server", targetID, script, method, data));
GetUdp ().Send (Formatter.singleton.Serialize (packet), packet.Length, FindConnection (targetID));
Debug.Log ("SERVER " + "Sent Data " + "Packet Size: " + packet.Length);
}
It's still not clear what data you send / serialize. What are the params that you pass to you method? Are you sure those are serializable and deserializable classes?
Bunny, Thanks for your reply, sorry it took so long to get back to you. Here is my Packet class. Bear in $$anonymous$$d that I had a websocket-sharp set up that I used this very class on as my base packet and it sent and deserialized fine. I switched to UDP and now it will send but not deserialize. Strange.
[Serializable]
public class Packet {
public Packet (string senderID, string targetID, string script, string method, params object[] data) {
this.senderID = senderID; this.targetID = targetID; this.script = script; this.method = method; this.data = data;
}
public string senderID, targetID, script, method;
public object[] data = new object[] { };
}