www.pudn.com > FileTranslate.rar > Sender.cs


using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Net; 
using System.Net.Sockets; 
using System.IO; 
using System.Threading; 
 
namespace TransSender 
{ 
	class Sender 
	{ 
		private string fileName; 
		private string filePath; 
		private string hostName; 
		private int bufferSize; 
		private int portNo; 
		private int blockIndex; 
		private Int64 sendLength; 
		private TcpClient tcpClient; 
		private Block block; 
 
		public Sender(string filePath,string hostName,int portNo,int bufferSize,int blockIndex,Int64 sendLength,Block block) 
		{ 
			this.filePath = filePath; 
			this.hostName = hostName; 
			this.portNo = portNo; 
			this.bufferSize = bufferSize; 
			this.blockIndex = blockIndex; 
			this.sendLength = sendLength; 
			this.block = new Block(block.startBlockNo, block.endBlockNo); 
			this.tcpClient = new TcpClient(); 
			this.tcpClient.NoDelay = false; 
			this.tcpClient.SendTimeout = 30000; 
			FileInfo fi = new FileInfo(filePath); 
			this.fileName = fi.Name; 
		} 
 
		private void setStatus(string msg) 
		{ 
			Console.WriteLine(msg); 
		} 
 
		public void sendFile()  
		{ 
			try 
			{ 
				DateTime time1 = DateTime.Now; 
				setStatus(time1.ToString()); 
				try 
				{ 
					tcpClient.Connect(hostName, portNo); 
				} 
				catch (Exception e) 
				{ 
					setStatus("error1:"+e.ToString()); 
					return; 
				} 
				BinaryWriter writer = new BinaryWriter(tcpClient.GetStream()); 
				try 
				{ 
					FileInfo fi = new FileInfo(filePath); 
					setStatus("Sending file information..."); 
					writer.Write(fileName+"_"+blockIndex); 
					writer.Write(sendLength); 
					setStatus("Sending file content..."); 
					FileStream fs = fi.OpenRead(); 
					if (block.startBlockNo == 0) 
						fs.Seek(0, SeekOrigin.Begin); 
					else 
						fs.Seek(block.startBlockNo * bufferSize + 1, SeekOrigin.Begin); 
					try 
					{ 
						Int64 total = sendLength; 
						byte[] buffer = new byte[bufferSize]; 
						int len; 
						Int64 currentBlockNo = block.startBlockNo; 
						while (((len = fs.Read(buffer, 0, bufferSize)) != 0) && currentBlockNo < block.endBlockNo) 
						{ 
							//if (currentBlockNo == 0) 
							//    writer.Write((long)0); 
							//else writer.Write(block.startBlockNo * bufferSize + 1); 
							//writer.Flush(); 
							//setStatus("sending file pos..."); 
							//Thread.Sleep(300); 
							writer.Write(buffer, 0, len); 
							writer.Flush(); 
							currentBlockNo++; 
						} 
						DateTime time2 = DateTime.Now; 
						setStatus(time2 + ":: completed"); 
						Thread.CurrentThread.Abort(); 
						return; 
					} 
					catch (Exception ex) 
					{ 
						setStatus("error2:" + ex.ToString()); 
 
					} 
					finally 
					{ 
						fs.Close(); 
					} 
				} 
				catch (Exception ee) 
				{ 
					setStatus("error3:" + ee.ToString()); 
				} 
				finally { 
					try { writer.Close(); } 
					catch { } 
					try { tcpClient.Close(); } 
					catch { } 
				} 
			} 
			catch (ThreadAbortException te) 
			{ 
				//setStatus("error4: "+te.ToString()); 
				return; 
			} 
		} 
	} 
}