Click here to Skip to main content
15,911,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi guys

I have a simple File transfer application that use socket
My problem is in sending bytes of file
this code send whole byte of 15Mb file but send only 20-40 kb of 2mb file
or 30-50 kb of 750 kb

I think problem is on File.ReadAllbyte()
so i used Thread.Sleep(5000)

but result is same with little change

how to force filling whole file in byte[] ?
private void StartSendingData(IPAddress ServerIP, string FileName)
{
    Socket MySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

    byte[] bt = File.ReadAllBytes(FileName);
    //Thread.Sleep(5000);
    MySocket.Connect(new IPEndPoint(ServerIP, 33000));
    MySocket.Send(bt);

}
Posted

File.ReadAllBytes should read all binary file content in the specified byte array, are you sure it doesn't ?!?! anyway if I were you, I won't do that ! I won't read all the contents of the file in one byte array, specially when the file is a big file, this will make the buffer with the size of the file which is really not good.

A better way to do that is to start reading the file content in small buffer chunks of size 1k or 2k then start sending each chunk after another.
 
Share this answer
 
I believe the other answer was correct; ReadAllBytes should read the entire file into memory before going to the next line of code. The problem is likely with your networking code.

Check the length of the byte array after you read all the bytes. And check the value returned from the Send() method... that will be the number of bytes sent. If it doesn't return the number of bytes loaded from the file, then you know that is where your problem is. You should also check on the receiving end of that Send() to see how many bytes are received... that could also provide some clues as to what is going on.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900