Need to transfert a file over tcp any advice?

I think having Available equal to 0 in the middle of a transfer is a normal thing. You can hit a point in time when you have already processed all available bytes so far and the new packet is just about to arrive. In this case Available will be 0.

You just have to account for that in your programming.

Before the modifications, Available was printing 24kB left, just reading a chunk of 1024bytes and at the next iteration, available was printing 0!!!

Unless there’s a timeout on tcp frame this should not be the case right?

That doesn’t look right.

This is what works for me.

b = new byte[32768];
int filesizerec = 0;
int rec = 0;
while ((rec = receive(activeSocket, ref b, 1000)) != -1)
{
    if (rec > 0)
    {
         iFile.Write(b, 0, rec);
         filesizerec += rec;
    }
}
........




private int receive(Socket rSocket, ref byte[] buf, int timeout)
{
   rSocket.ReceiveTimeout = timeout;
   int rec = 0;
   try
   {
      rec = rSocket.Receive(buf, buf.Length, SocketFlags.None);
   }
   catch
   {
      rec = -1;
   }

   return rec;
}

1 Like

@ Cowboy - Looks good.

@ Architect - Thanks

@ andre.m - That’s from the timeout at the end of the file. That’s what the try catch is for but you already new that. Its to bad there isn’t someway to detect the end of file. I would of thought that the socket would close at the end but it doesn’t and there isn’t a byte count of the file size to work with either that I have been able to find.

In my solution I send the file size before sending files data.