public void run() { try { // FlashLogger.Info("Handling request from "+socket.); byte[] readBuf = new byte[32768]; MemoryStream stream = new MemoryStream(readBuf); //read file name info int r = 0; while (true) { r = socket.ReadData(readBuf); if (r == 0) { Thread.Sleep(100); } else { break; } } if (r == -1) { socket.Close(); return; } //how many bytes to read for the file name byte[] len = new byte[4]; stream.Read(len, 0, 4); if (verbose) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < len.Length; i++) { sb.Append(len[i].ToString()); sb.Append(" "); } Console.WriteLine("[SendFile] name length data: " + sb.ToString()); } long length = ApplicationCode.decode(len, 0); if (verbose) { Console.WriteLine("[SendFile] name length: " + length); } byte[] fileName = new byte[(int)length]; stream.Read(fileName, 0, fileName.Length); string file = Encoding.UTF8.GetString(fileName); Console.WriteLine("[SendFile] File requested: '" + file + "'"); Thread.CurrentThread.Name = ("sendFile_" + file); FileStream fis = new FileStream(file, FileMode.Open); try { long size = fis.Length; fis.Close(); Console.WriteLine("[SendFile] File size: " + size); //send size info socket.SendData(ApplicationCode.encode64(size)); long start = DateTime.Now.Ticks; // //and send the file ApplicationCode.CopySocketFile(file, socket, socket.GetDataStreamLen()); Console.WriteLine("[SendFile] Finished sending data."); long end = DateTime.Now.Ticks; //System.out.println(socket.getSession().getStatistics().toString()); double rate = 1000.0 * size / 1024 / 1024 / (end - start); Console.WriteLine("[SendFile] Rate: " + rate.ToString(format) + " MBytes/sec. " + (8 * rate).ToString(format) + " MBit/sec."); // if(Boolean.getBoolean("udt.sender.storeStatistics")){ // socket.getSession().getStatistics().writeParameterHistory(new File("udtstats-"+System.currentTimeMillis()+".csv")); // } } finally { socket.Close(); if (fis != null) { fis.Close(); } } FlashLogger.Info("Finished request from " + socket.GetDestination()); } catch (Exception ex) { Console.WriteLine(ex); } }
/** * * * 读取文件发送 * * @param 文件,socket,发送包大小 * * @return * * */ internal static void CopySocketFile(string file, judpSocket target, int packagetLen) { //byte[]buf=new byte[8*65536]; FileStream fis = null; try { fis = new FileStream(file, FileMode.Open); } catch (FileNotFoundException e) { return; } byte[] buf = new byte[bufsize]; int c = 0; long read = 0; long size = fis.Length; byte[] data = null; int sendCount = 0; long sendSum = 0; if (packagetLen <= 0) { packagetLen = 65535; } int sendLen = packagetLen - 24; long waitTime = 0; if (speed > 0) { waitTime = (long)(speed * 1000); } Console.WriteLine("sendFile_" + file + ",socketID:" + target.SocketID); while (true) { try { c = fis.Read(buf, 0, buf.Length); } catch (IOException e) { Console.WriteLine(e.Message); } if (c < 0) { break; } read += c; if (sendCount > 128) { try { Thread.Sleep(1000); sendCount = 0; Console.WriteLine("文件发送" + file + "," + sendSum); } catch (Exception e) { Console.WriteLine(e.Message); } } if (c < sendLen) { data = new byte[c]; Array.Copy(buf, 0, data, 0, c); target.SendData(data); sendCount++; sendSum++; } else { int offset = 0; int len = c; while (len > 0) { int clen = len > sendLen ? sendLen : len; data = new byte[clen]; Array.Copy(buf, offset, data, 0, clen); target.SendData(data); len = len - clen; offset += clen; sendCount++; sendSum++; if (waitTime > 0 && sendSum % waitTime == 0) { Thread.Sleep(1000); } } } if (read >= size && size > -1) { break; } } }