基于TCP协议传输大文件

 不建议用WCF做文件传输
参考以下代码(VS2008下测试通过)
Service端:
C# code
   
   
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.IO; using System.ServiceModel.Dispatcher; using System.ServiceModel.Description; using System.ServiceModel.Channels; namespace FSDownloadService { [MessageContract] public class MyFileInfo { [MessageHeader] public string FileName; [MessageHeader] public long FileSize; [MessageBodyMember] public Stream Stream; public MyFileInfo() { } public MyFileInfo(Stream stream, string fileName, long fileSize) { this .Stream = stream; this .FileSize = fileSize; this .FileName = fileName; } } [MessageContract] public class DownloadFileRequest { [MessageBodyMember] public readonly string FileName; public DownloadFileRequest() { } public DownloadFileRequest( string fileName) { this .FileName = fileName; } } [ServiceContract] public interface IFileManager { [OperationContract] MyFileInfo DownloadFile(DownloadFileRequest request); } [ServiceBehavior(IncludeExceptionDetailInFaults = true )] public class MyService : IFileManager { public MyFileInfo DownloadFile(DownloadFileRequest request) { FileInfo fi = new FileInfo(request.FileName); MyFileInfo result = new MyFileInfo(File.OpenRead(request.FileName), request.FileName, fi.Length); return result; } } public class MyHost { static ServiceHost host = null ; public static void Open() { string baseAddress = " net.tcp://localhost:2008/FileService " ; host = new ServiceHost( typeof (MyService), new Uri(baseAddress)); host.AddServiceEndpoint( typeof (IFileManager), GetTcpBinding(), "" ); host.Open(); } public static void Close() { if (host != null && host.State == CommunicationState.Opened) { host.Close(); } host = null ; } public static Binding GetTcpBinding() { NetTcpBinding binding = new NetTcpBinding(); binding.TransferMode = TransferMode.Streamed; binding.MaxReceivedMessageSize = int .MaxValue; return binding; } } }

Host到Console
C# code
   
   
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace FSDownloadService { class Program { static void Main( string [] args) { System.Console.WriteLine( " Press <ENTER> to start the service. " ); System.Console.ReadLine(); MyHost.Open(); System.Console.WriteLine( " The service is started. " ); System.Console.WriteLine( " Press <ENTER> to stop the service. " ); System.Console.ReadLine(); MyHost.Close(); } } }

Client端代码如下
C# code
   
   
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.IO; using System.ServiceModel.Dispatcher; using System.ServiceModel.Description; using System.ServiceModel.Channels; using FSDownloadService; namespace FSDownloadClient { class Program { static void Main( string [] args) { System.Console.WriteLine( " Press <ENTER> to start the download. " ); System.Console.ReadLine(); string baseAddress = " net.tcp://localhost:2008/FileService " ; ChannelFactory < IFileManager > factory = new ChannelFactory < IFileManager > (MyHost.GetTcpBinding(), new EndpointAddress(baseAddress)); IFileManager manager = factory.CreateChannel(); string remoteFileName = " 11.rar " ; MyFileInfo fileInfo = manager.DownloadFile( new DownloadFileRequest(remoteFileName)); FileStream fs = File.Create( " Client_ " + fileInfo.FileName); int bytesRead = 0 ; long totalBytesRead = 0 ; byte [] buffer = new byte [ 10000 ]; do { bytesRead = fileInfo.Stream.Read(buffer, 0 , buffer.Length); fs.Write(buffer, 0 , bytesRead); totalBytesRead += bytesRead; double currentProgress = (( double )totalBytesRead) / fileInfo.FileSize; Console.WriteLine( " Read so far {0:.00}% of the file " , currentProgress * 100 ); } while (bytesRead > 0 ); ((IClientChannel)manager).Close(); factory.Close(); } } }
 
 
备注:未经测试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值