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 Roggin · Jun 28, 2017 at 08:23 PM · networkingnetworkserializationbinaryformatterstream

BinaryFormatter and MemoryStream cause a loop of "SerializationException: Unexpected binary element: 0"

Hi, Iam trying to make a multiplayer game using webgl.

Sending a message from player to server works, but not the other way around.

I have this in Update function

         int recConnectionId;
         int recChannelId;
         byte[] recBuffer = new byte[1024];
         int bufferSize = 1024;
         int dataSize;
         byte error;
         NetworkEventType recNetworkEvent = NetworkTransport.ReceiveFromHost(
             socketId, out recConnectionId, out recChannelId, recBuffer, 
             bufferSize, out dataSize, out error);
 
         switch (recNetworkEvent)
         {
             case NetworkEventType.DataEvent:
 
                 MemoryStream str = new MemoryStream(recBuffer);
                 
                 BinaryFormatter formatter = new BinaryFormatter();

                 //the line below causes a loop of exceptions in console
                 string msg = formatter.Deserialize(str) as string; 

                 Debug.Log("from server: "  + msg.ToString());                    
                 break;
         }

The switch has other cases but they are mostly empty.

The mentioned line causes a loop of "SerializationException: Unexpected binary element: 0" in console and the message itself is never written.

This exact same code works when I run it in unity editor but not when I build the app and run it in browser.

What am I doing wrong?

Thank you.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Jun 28, 2017 at 08:50 PM

Your buffer size is quite small. If your message is greater than 1024 bytes it can't be obtained by a single "ReceiveFromHost" call and would be fragmented.

I barely used the new networking system. So i'm not sure if "ReceiveFromHost" guarantees to receive a complete message as long as it fits into the provided buffer. Maybe it's still fragmented.

You can only deserialize a binary stream when you can guarantee that all data from the serialized stream is available. Also you need to use the "dataSize" out value and create a Memory stream that is limited to that size. You need this constructor.

Also you should wrap your memory stream into a using statement (just for good practise):

 using(MemoryStream str = new MemoryStream(recBuffer, 0, dataSize))
 {
     // ...
 }

Comment
Add comment · Show 3 · Share
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 · Jun 28, 2017 at 08:57 PM 0
Share

ps: the BinaryFormatter has a quite verbose serialization format. It seems like a waste of space to use it to send a single string value. It would be much more compact to use something like

 // sending
 byte[] data = System.Text.Encoding.UTF8.GetBytes(str);
 
 // receiving
 string str = System.Text.Encoding.UTF8.GetString(data, 0, dataSize);
avatar image Roggin Bunny83 · Jun 28, 2017 at 09:12 PM 0
Share

The message being sent is really short.

I tried the other constructor and made sure the message would fit. It still ends up with SerializationException: Unexpected binary element: 0

I'll try the other method of sending a string but the general purpose of what Iam doing is to send more than just a string.

avatar image Roggin Roggin · Jun 28, 2017 at 09:21 PM 0
Share

wait, guess I still did something wrong. I think I fixed it with a bigger buffer. making the sending message smaller didn't work but making the buffer bigger did. Thank you.

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

109 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

Related Questions

Client to client Serialization 1 Answer

NetworkReader/Writer with GameObject 0 Answers

Acess server stuff by the client 0 Answers

Network Instiantiate Terrain (Voxel) issues C# 0 Answers

Bug in editor regarding networkspawn 0 Answers


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