void ReadSend(Socket client) { uint length = (uint)new FileInfo(mData.FilePath).Length; byte[] buffer = new byte[Constants.BUFFER_SIZE]; int ini = Environment.TickCount; using (NetworkStream ns = new NetworkStream(client)) { LengthSerializer.Serialize(ns, length); using (FileStream fs = File.OpenRead(mData.FilePath)) { int read = fs.Read(buffer, 0, buffer.Length); while (read > 0) { ns.Write(buffer, 0, read); read = fs.Read(buffer, 0, buffer.Length); } } } LogPrinter.LogTime("readsend: sent", length, ini); }
void MemorySend(Socket client) { byte[] buffer = new byte[Constants.BUFFER_SIZE]; (new Random()).NextBytes(buffer); uint totalLen = (uint)mData.SizeInMB * 1024 * 1024; int ini = Environment.TickCount; using (NetworkStream ns = new NetworkStream(client)) { LengthSerializer.Serialize(ns, totalLen); uint toSent; uint totalSent = 0; do { if ((totalLen - totalSent) < Constants.BUFFER_SIZE) { toSent = totalLen - totalSent; } else { toSent = Constants.BUFFER_SIZE; } ns.Write(buffer, 0, (int)toSent); totalSent += toSent; }while (totalSent < totalLen); } LogPrinter.LogTime("memory: sent", totalLen, ini); }
void TransmitFile(Socket client) { uint length = (uint)new FileInfo(mData.FilePath).Length; int ini = Environment.TickCount; using (NetworkStream ns = new NetworkStream(client)) { byte[] lengthBytes = LengthSerializer.Serialize(length); // send the header client.SendFile(mData.FilePath, lengthBytes, null, TransmitFileOptions.UseKernelApc); } LogPrinter.LogTime("transmitfile: sent", length, ini); }
internal void Download() { IPHostEntry ipHostInfo = Dns.Resolve(mServer); IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint remoteEP = new IPEndPoint(ipAddress, Constants.PORT); Socket socket = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(remoteEP); try { byte[] buffer = new byte[Constants.BUFFER_SIZE]; using (NetworkStream ns = new NetworkStream(socket)) { int ini = Environment.TickCount; uint length = LengthSerializer.Deserialize(ns); uint remaining = length; int read = 0; while (remaining > 0) { int readSize = buffer.Length; if (readSize > remaining) readSize = (int)remaining; read = ns.Read(buffer, 0, readSize); remaining -= (uint)read; } LogPrinter.LogTime("received", length, ini); } } finally { socket.Shutdown(SocketShutdown.Both); socket.Close(); } }