Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 car104 · Nov 04, 2014 at 07:53 PM · c#datahttpsave file

How would you send binary data over web?

What I am wondering is, how would one send binary data over the web to a web server? I was following along with Mike Geig's "Data persistence" live training which talks about using serialization and the BinaryFormatter to save player data to local files on your device. He had mentioned that you could probably send it through a POST Request to a web server. I was just curious as how to do that.

It seems the BinaryFormatter.Serialize() function wants to send an object map to a stream. As I can tell, the WWW class sends a URL request, but I'm not sure if it behaves the same as like an IO stream.

I'm guessing you couldn't do something like this:


    BinaryFormatter bf = new BinaryFormatter();
    WWW www = new WWW("http://test.url.com/test.php");
    bf.Serialize(www, playerData);

I'm just wondering how to go about doing this. What I want to be able to do is send player information to a server to be saved into a database table for the player (inventory, player stats). I'd love to be able to send the data in a similar manner as I would saving a binary file. I guess one option would be to save the binary locally, then send it through the WWWForm.AddBinaryData method. I was just wondering if there is some direct way to serialize through the BinaryFormatter and send via HTTP.

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

2 Replies

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

Answer by Jeff-Kesselman · Nov 10, 2014 at 07:49 PM

The answer is don't use HTTP.

HTTP stands for "Hyper*text Transfer Protocol". It is a single push/fetch text protocol layered ontop* of TCP/IP which is a connected in-order guaranteed binary protocol. if you want to send and receive binary packets TCP/Ip is whole lot more efficient.

The one advantage of HTTP is that it can traverse most firewalls. Websockets brings in the ability to make a connection as if you were an HTTP request but then upgrade and maintain the connection as a straight TCP/IP connection, which is the best of both worlds.

To use Websockets you need a server that supports them and a client library for them on your client computer. Such libraries are available for C++ on the web.

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

Answer by KulestarUK · Nov 10, 2014 at 08:43 PM

To directly answer your question, if you'd like to specifically use HTTP, use a WWWForm with a MemoryStream and BinaryFormatter as you're suggesting above.

Something like this (C#):

 // Create the memory stream:
 MemoryStream theStream=new MemoryStream();
 
 // Create a formatter:
 BinaryFormatter format=new BinaryFormatter();
 
 // Serialize into the memory stream:
 format.Serialize(theStream,playerData);
 
 // Create the form:
 WWWForm form=new WWWForm();
 
 // Add the stream data to your form:
 form.AddBinaryData("post-field-name",theStream.ToArray(), "filename.bytes", "application/octet-stream");
 
 // Upload:
 WWW www = new WWW("http://test.url.com/test.php",form);

Having said that, I wouldn't recommend this approach at all! The main reasoning is updates. For example, you might introduce a new feature into your game which results in your player data object gaining fields or loosing fields. When using straight serialization, this has a nasty habit of corrupting your save data. A better approach is to create either specific fields in your database, and map each one to a post field, or alternatively create a textual (XML, config, JSON) storage format and use that instead, or as another alternative, use BinaryReader/BinaryWriter and construct the binary object yourself - this way you can at least be aware of the exact structure of your binary data, and how flexible it is to things like adding a new bit of data to it without it breaking all your players existing saves.

That's before mentioning the security aspects of this approach too. Don't forget that as soon as someone discovers which URL to send requests to, something which is fairly straight forward to do, it becomes really easy to give their account a few "upgrades" :) This should be considered with any storage system of course, but especially with ones over HTTP due to the open nature of the web. You'll want to create some form of key exchange system so you can verify requests too.

Comment
Add comment · Show 1 · 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 car104 · Nov 10, 2014 at 09:07 PM 0
Share

Yeah, I wasn't sure if HTTP was necessarily a good option. Ideally, this would be used to save information on a server, such as position, rotation, health, experience or other player related stats and states. If it's a web game, create some sort of server program that would handle input authoritatively and handle save data as well. But as I'm just experimenting, and possibly doing a single player with multiplayer aspects, I could create the save files onto the local disk. I'd love to be able to test if a player has attempted to tamper with the binary file. I guess this could be done by sending the initial binary created to the server. Whenever a player logs in, I could do some kind of md5 check to see if the save files match, let the server know, load the file and flag it for saving, and then upon closing the game and logging out, write the file and send the new save data back to the server.

I'm guessing, though, that there are a million different directions to approach this from. I was just originally curious as to the context of $$anonymous$$ike Geig mentioning you could send it to a server via POST $$anonymous$$ethod.

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

28 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Best way to store and use small game data? 1 Answer

Syncing variables between two objects of same script 1 Answer

How to get A Camera Collision? 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