Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by STBarnard · Apr 05, 2017 at 05:50 PM · c#networkingserializationudp

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)

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Apr 05, 2017 at 07:12 PM 0
Share

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.

avatar image STBarnard · Apr 05, 2017 at 07:21 PM 0
Share

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);
 }
avatar image Bunny83 STBarnard · Apr 05, 2017 at 07:35 PM 0
Share

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?

avatar image STBarnard Bunny83 · Apr 06, 2017 at 09:21 PM 0
Share

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[] { };
         }

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

317 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UDP Packet and EC2 0 Answers

What's the best approach for creating a secure authorative server? 0 Answers

How can I read data sent using the Transport API with python server 1 Answer

[Beginner] /C#/UDP/LocalHost/ receiving data the IPEndPoint.Port is different 1 Answer

Unity Network Device Discovery SSDP or DNS-SD 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges