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.
(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.
Your answer
Follow this Question
Related Questions
Best way to learn Networking in general 0 Answers
Calling Bool in Network Command Not Working 0 Answers
Exiting a UNET lobby causes future ClientRPC calls to be ignored. 1 Answer
ClientRPC not called from command on client instance during Start() 0 Answers
Managed to set up Multiplayer Online on one Computer, but not on 2 0 Answers