/** * 关闭 */ public void Close() { if (client != null) { if (sumLen == 0) { if (!client.isClosed()) { client.Shutdown(); } } else { if (!client.isClosed()) { client.Shutdown(); } } } }
public void Run() { Configure(); verbose = true; try{ UDTReceiver.connectionExpiryDisabled = true; UDTClient client = new UDTClient(localPort, localIP); client.Connect(this.serverHost, this.serverPort); UDTInputStream inStream = client.GetInputStream(); UDTOutputStream outStream = client.GetOutputStream(); Console.WriteLine("[ReceiveFile] Requesting file " + remoteFile); byte[] fName = Encoding.UTF8.GetBytes(remoteFile);//兼容java //send file name info byte[] nameinfo = new byte[fName.Length + 4]; Array.Copy(Encode(fName.Length), 0, nameinfo, 0, 4); Array.Copy(fName, 0, nameinfo, 4, fName.Length); outStream.Write(nameinfo); outStream.Flush(); //pause the sender to save some CPU time outStream.PauseOutput(); //read size info (an 64 bit number) byte[] sizeInfo = new byte[8]; int total = 0; while (total < sizeInfo.Length) { int r = inStream.Read(sizeInfo); if (r < 0) { break; } total += r; } //读取文件长度 long size = Decode(sizeInfo, 0); FileInfo file = new FileInfo(localFile); Console.WriteLine("[ReceiveFile] Write to local file <" + file.FullName + ">"); FileStream fos = new FileStream(file.FullName, FileMode.Append); //准备写入文件 try{ Console.WriteLine("[ReceiveFile] Reading <" + size + "> bytes."); long start = DateTime.Now.Ticks; //and read the file data //Util.copy(in, os, size, false); CopyFile(fos, inStream, size, false); long end = DateTime.Now.Ticks; double rate = 1000.0 * size / 1024 / 1024 / (end - start); Console.WriteLine("[ReceiveFile] Rate: " + rate.ToString(format) + " MBytes/sec. " + (8 * rate).ToString(format) + " MBit/sec."); client.Shutdown(); if (verbose) { Console.WriteLine(client.GetStatistics()); } }finally{ fos.Close(); } }catch (Exception ex) { //throw new RuntimeException(ex); } }