• 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 /
  • Help Room /
avatar image
0
Question by alex_e_m · Feb 10, 2018 at 10:30 AM · networkingclient-servertransferring

zero bytes in networkStream

Hi, I am trying to implement file transfer from android to pc. The android client made in unity (c#) and pc server made in visual studio (c#).

when I sending file in loop by 3x1000Byte parts, i'm getting zero bytes came from nowhere in output log.

alt text

(its not the case that the connection is closed and server reads 0 bytes...)

as u can see on the screenshot - file is transferring good till 4460(dec) byte, then those zero bytes then resuming with correct bytes.

if someone knows where from those zero bytes came from, please help..

I doubt it will help, but here is the

server part:

 static void receivMediaFile(int partSize)
 {
     NetworkStream _ns = tcpClient.GetStream();

     byte[] longB = new byte[8];
     _ns.Read(longB, 0, 8);
     long fileLength = BitConverter.ToInt64(longB, 0);

     Console.Title += @"fileLength - (fileLength % "+partSize+")=" + (fileLength - (fileLength % partSize)).ToString();
     Console.Title += @"fileLength % "+partSize+"=" + (fileLength % partSize).ToString();

     Console.Title += "  fileLength specified=(long)" + fileLength.ToString();

     Thread.Sleep(200);

     byte[] fileData = new byte[fileLength];
     for(long i = 0; i < fileLength-partSize;)
     {

         for(long j = i; j < (i + 3 * partSize <= fileLength ? i + 3 * partSize : fileLength); j += partSize)
         {
             int byteCountToRead = (int)(fileLength - j > partSize ? partSize : fileLength % partSize);
             _ns.Read(fileData, (int)j, byteCountToRead);
         }
         byte[] hash = MD5.Create().ComputeHash(fileData, (int)i, (int)(fileLength - i >= 3000 ? 3000 : fileLength - i));//part size * n

         _ns.Write(hash, 0, hash.Length);

         while(!_ns.DataAvailable)
         {
         }

         byte[] hashResultBuf = new byte[4];
         _ns.Read(hashResultBuf, 0, 4);
         if(BitConverter.ToInt32(hashResultBuf, 0) == int.MaxValue / 3)
         {
             printl("good hash");
             //continue loop
             i += 3 * partSize;
         }
         else if(BitConverter.ToInt32(hashResultBuf, 0) == int.MaxValue / 5)
         {
             //wrong hash
             if(fileLength - i < 6000)
                 printl("\nwrong hash");
             else
                 printl("\nwrong hash with bytes:\n" + BitConverter.ToString(fileData, (int)i, 3000));
         }
     }
     receivingData = false;
     //play
 }

client part:

 void sendMediaFile(string path)
 {
 tcpClient.Client.DontFragment = true;
 maxPartSizeToSend = 1000;

 int partSize = maxPartSizeToSend;
 print("send file started");

 log("starting send media() with path=" + path);
 Stream _fs = new FileStream(path, FileMode.Open);//new Mp3FileReader(path);

 NetworkStream _ns = tcpClient.GetStream();


 _ns.Write(System.BitConverter.GetBytes((int)ioCommands.mediaFromBeyond), 0, 4);// it goes to receiver
 _ns.Write(System.BitConverter.GetBytes((int)partSize), 0, 4);// it goes throught receiver

 //server supose to wait for my commands and not send nothing, maybe a ping int.. so:
 while(_ns.DataAvailable)
 {
     _ns.ReadByte();
 }

 _ns.Write(System.BitConverter.GetBytes(_fs.Length), 0, 8);//it goes to func()
 byte[] _fsData = new byte[_fs.Length];

 _fs.Read(_fsData, 0, (int)_fs.Length);//so small, it's developed for songs

 for(long i = 0; i < _fs.Length;)
 {
     byte[] hash = MD5.Create().ComputeHash(_fsData, (int)i, (int)(_fs.Length - i >= 3000 ? 3000 : _fs.Length - i));//part size * n

     for(long j = i; j < (i+3*partSize<=_fs.Length?i+3*partSize:_fs.Length); j += partSize)
     {
         int byteCountToWrite = (int)(_fs.Length - j >= partSize ? partSize : _fs.Length % partSize);

         _ns.Write(_fsData, (int)j, byteCountToWrite);

     }

     byte[] hashBuf = new byte[hash.Length];
     _ns.Read(hashBuf, 0, hash.Length);

     if(hash.SequenceEqual(hashBuf))
     {
         _ns.Write(System.BitConverter.GetBytes(int.MaxValue / 3), 0, 4);//hash is good, next
         i += 3 * partSize;
     }
     else
     {
         print("resending");
         _ns.Write(System.BitConverter.GetBytes(int.MaxValue / 5), 0, 4);//resending
     }
 }
 }

one more thing - when i running client in unity editor and sending file, it works very well with 100% good hash sums.

2.gif (94.5 kB)
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

161 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

Related Questions

Looking for tips on identifying reason behind random client disconnects 0 Answers

Determinism with character controller issue 0 Answers

non-player object can't execute's "[Command]"'s attributes 0 Answers

running car when button is pressed on client side 0 Answers

Advanced/native unity client with custom .net server 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