1.打开VS选择控制台项目新建一个解决方案Server,然后添加两个类库Contract和Service.

2.在Contract中添加一个接口IFileDownload

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel; namespace Contract
{
[ServiceContract]
public interface IFileDownload
{
[OperationContract]
Dictionary<string, byte[]> DownloadFile(string[] fileNames);
}
}

3.在Service中实现接口IFileDownload

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Contract;
using System.IO; namespace Service
{
public class FileDownload : IFileDownload
{
public Dictionary<string, byte[]> DownloadFile(string[] fileNames)
{
Dictionary<string, byte[]> filedata = new Dictionary<string, byte[]>();
string queryDirectory = AppDomain.CurrentDomain.BaseDirectory + "数据"; //假设服务器数据存放在运行目录下的“数据”文件夹中
DirectoryInfo drtInfo = new DirectoryInfo(queryDirectory);
FileInfo[] fileInfo = drtInfo.GetFiles();
foreach (FileInfo item in fileInfo)
{
foreach (string filename in fileNames)
{
if (filename == item.Name)
{
FileStream fsRead = new FileStream(queryDirectory+"\\"+filename,FileMode.Open,FileAccess.Read);
byte[] bytDt = new byte[fsRead.Length];
fsRead.Read(bytDt,,bytDt.Length);
fsRead.Close();
filedata.Add(filename,bytDt);
break;
}
}
} return filedata;
}
}
}

3.在Server中添加如下代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Service; namespace Server
{
class Program
{
static void Main(string[] args)
{
ServiceHost download_host = new ServiceHost(typeof(FileDownload));
download_host.Open();
Console.WriteLine("文件下载服务已启动");
Console.Read();
}
}
}

4.在Server中添加一个应用程序配置文件App.xml,并选择菜单栏->工具->WCF服务配置编辑器对配置文件进行编辑,具体操作可参考http://www.cnblogs.com/judastree/archive/2012/08/26/2657555.html

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="DownloadFileBindingConfig" closeTimeout="20:56:00" openTimeout="20:30:00" sendTimeout="20:56:00" maxBufferSize="2073741824" maxReceivedMessageSize="2073741824" transferMode="Buffered" maxBufferPoolSize="2073741824" >
<security mode="None">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign"/>
<message clientCredentialType="Windows"/>
</security>
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="DownloadBehavior">
<serviceMetadata httpGetEnabled="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Service.FileDownload" behaviorConfiguration="DownloadBehavior">
<endpoint address="net.tcp://localhost:2310/Download" binding="netTcpBinding"
bindingConfiguration="DownloadFileBindingConfig" contract="Contract.IFileDownload" />
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:2310" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>

5.启动Server

6.新建一个窗体应用程序的解决方案Client,添加服务引用

7.修改Client的配置文件app.xml为

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IFileDownload" maxBufferSize="2073741824" maxReceivedMessageSize="2073741824">
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:2310/Download" binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_IFileDownload" contract="ServiceReference1.IFileDownload"
name="NetTcpBinding_IFileDownload" />
</client>
</system.serviceModel>
</configuration>

8.添加一个TextBox和一个Button,在Button下添加代码

 private void button1_Click(object sender, EventArgs e)
{
ServiceReference1.FileDownloadClient fileDownload = new ServiceReference1.FileDownloadClient();
List<string> filenames = new List<string>();
filenames.Add(textBox1.Text); //为了简单,这里只查找一个文件
Dictionary<string,byte[]> downloadfiles = fileDownload.DownloadFile(filenames.ToArray());
foreach (var item in downloadfiles)
{
if (item.Value!=null)
{
FileStream fsWrite = new FileStream(@"C:\Users\yujian\Desktop\DownloadFiles\"+item.Key,FileMode.Create);//这里下载wav文件,可根据实际情况调整
fsWrite.Write(item.Value,,item.Value.Length);
fsWrite.Close();
}
}
MessageBox.Show("下载成功");
}

9.运行结果

源码

WCF客户端从服务器下载数据的更多相关文章

  1. java技术用ssh从linux服务器下载数据

    通常需要从linux服务器获取数据文件,而通常能有的访问方式只有ssh,所以就可以用ssh进行数据下载. java连接远程主机的方式有多种,这里和大家分享的是通过ssh方式连接远程主机,使用的是jsc ...

  2. ICE学习第四步-----客户端请求服务器返回数据

    这次我们来做一个例子,流程很简单:客户端向服务器发送一条指令,服务端接收到这条指令之后,向客户端发送数据库中查询到的数据,最终显示在DataGridView上. 根据上一篇文章介绍的Slice语法,我 ...

  3. C语言Socket-单工通信(客户端向服务器发送数据)

    服务端(server) #include <stdio.h> #include <winsock2.h> #pragma comment(lib,"ws2_32.li ...

  4. tcp客户端从服务器下载文本文件

    代码讲解: server import socket def send_file_client(new_client_socket, new_client_addr): # 接收客户端需要下载的文件名 ...

  5. WCF客户端和服务器时间不一致,导致通道建立失败的问题)

    本文转载:http://www.cnblogs.com/bcbr/articles/2288374.html 最近,经常有客户反应,前天还用的好好的系统,今天就不能用了. 考虑到系统近来没有做过改动和 ...

  6. WCF客户端获取服务器返回数据报错

    错误信息:An error occurred while receiving the HTTP response to http://127.0.0.1/SIHIS/Infection/PubExec ...

  7. nodejs的cs模式聊天客户端和服务器实现

    学习完nodejs的基础后,自然要写点东西练练手,以下是一个基于nodejs的cs模式的聊天软件代码: net模块是nodejs的网络编程必定用到的一个模块,对socket通信进行了封装 实现的功能: ...

  8. 加密解密(2)*客户端,服务器,CA(Certificate Authority),公钥,私钥,证书,签名,验证

    加密解密(2)*客户端,服务器,CA(Certificate Authority),公钥,私钥,证书,签名,验证 各角色比喻 客户端:通常为请求方,要验证服务器的身份. 服务器:通常为响应方,有时也要 ...

  9. WCF客户端调用服务器端错误:"服务器已拒绝客户端凭据"。

    WCF客户端和服务器端不在同一台机器上时,客户端调用服务器端会报如下错误:"服务器已拒绝客户端凭据". 解决办法:在服务端配置文件与客户端配置文件中加入下面红色部分

随机推荐

  1. 循环屏障CyclicBarrier以及和CountDownLatch的区别

    CyclicBarrier 的字面意思是可循环使用(Cyclic)的屏障(Barrier).它要做的事情是,让一组线程到达一个屏障(也可以叫同步点)时被阻塞,直到最后一个线程到达屏障时,屏障才会开门, ...

  2. java元注解 @Documented注解使用

    @Documented 注解表明这个注解应该被 javadoc工具记录. 默认情况下,javadoc是不包括注解的. 但如果声明注解时指定了 @Documented,则它会被 javadoc 之类的工 ...

  3. u-boot移植(一)---准备工作

    一.工具链的制作 1.1 工具 软件工具:crosstool-ng 下载地址:git clone https://github.com/crosstool-ng/crosstool-ng crosst ...

  4. JavaScript之函数调用与被调用的上下文对象this

    不同的调用机制决定了函数上下文对象的不同: 1.  作为普通函数进行调用时,其上下文是全局对象window; 2.  作为(对象)方法进行调用时,其上下文对象时拥有该方法的对象; 3.  作为构造器( ...

  5. JavaScript之小工具之日志log()[兼容]

    function log(){ try{ console.log.apply(console,arguments); }catch(e){ try{ opera.postError.apply(ope ...

  6. python - logging模块应用

    logging日志模块应用: import logging # logging.basicConfig( # #定义日志级别,共5个级别,默认级别为warning级别,所以debug和info级别不添 ...

  7. 【windows核心编程】远程线程DLL注入

    15.1 DLL注入 目前公开的DLL注入技巧共有以下几种: 1.注入表注入 2.ComRes注入 3.APC注入 4.消息钩子注入 5.远线程注入 6.依赖可信进程注入 7.劫持进程创建注入 8.输 ...

  8. python中的这些坑,早看早避免。

    python中的这些坑,早看早避免. 说一说python中遇到的坑,躲坑看这一篇就够了 传递参数时候不要使用列表 def foo(num,age=[]): age.append(num) print( ...

  9. Spring的Aspect切面类不能拦截Controller中的方法

    根本原因在于<aop:aspectj-autoproxy />这句话是在spring的配置文件内,还是在springmvc的配置文件内.如果是在spring的配置文件内,则@Control ...

  10. lvs持久连接及防火墙标记实现多端口绑定服务

    lvs持久连接及防火墙标记实现多端口绑定服务 LVS持久连接: PCC:将来自于同一个客户端发往VIP的所有请求统统定向至同一个RS: PPC:将来自于一个客户端发往某VIP的某端口的所有请求统统定向 ...