The short answer is, you send a byte[]
multiple times...
Essentially, you will need to fill a buffer ('byte[]') with a subset of the file:
int count = fileIO.Read(buffer, 0, buffer.Length);
And then send the buffer over the socket:
clientSocket.Send(buffer, 0, count);
Just do these two processes until you have sent the entire file... (Which will be when count <= 0
) However, the server has to know how many bytes to read... so we should start out by sending a Int64
with the file's length.
What we have so far...
using (var fileIO = File.OpenRead(@"C:\temp\fake.bin"))
using(var clientSocket = new System.Net.Sockets.TcpClient(ip, port).GetStream())
{
// Send Length (Int64)
clientSocket.Write(BitConverter.GetBytes(fileIO.Length, 0, 8));
var buffer = new byte[1024 * 8];
int count;
while ((count = fileIO.Read(buffer, 0, buffer.Length)) > 0)
clientSocket.Write(buffer, 0, count);
}
Server Side
Int64 bytesReceived = 0;
int count;
var buffer = new byte[1024*8];
// Read length - Int64
clientStream.Read(buffer, 0, 8);
Int64 numberOfBytes = BitConverter.ToInt64(buffer, 0);
using(var fileIO = File.Create("@c:\some\path"))
while(bytesReceived < numberOfBytes && (count = clientStream.Read(buffer, 0, buffer.Length)) > 0)
{
fileIO.Write(buffer, 0, count);
bytesReceived += count;
}