WCF客户端从服务器下载数据
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客户端从服务器下载数据的更多相关文章
- java技术用ssh从linux服务器下载数据
通常需要从linux服务器获取数据文件,而通常能有的访问方式只有ssh,所以就可以用ssh进行数据下载. java连接远程主机的方式有多种,这里和大家分享的是通过ssh方式连接远程主机,使用的是jsc ...
- ICE学习第四步-----客户端请求服务器返回数据
这次我们来做一个例子,流程很简单:客户端向服务器发送一条指令,服务端接收到这条指令之后,向客户端发送数据库中查询到的数据,最终显示在DataGridView上. 根据上一篇文章介绍的Slice语法,我 ...
- C语言Socket-单工通信(客户端向服务器发送数据)
服务端(server) #include <stdio.h> #include <winsock2.h> #pragma comment(lib,"ws2_32.li ...
- tcp客户端从服务器下载文本文件
代码讲解: server import socket def send_file_client(new_client_socket, new_client_addr): # 接收客户端需要下载的文件名 ...
- WCF客户端和服务器时间不一致,导致通道建立失败的问题)
本文转载:http://www.cnblogs.com/bcbr/articles/2288374.html 最近,经常有客户反应,前天还用的好好的系统,今天就不能用了. 考虑到系统近来没有做过改动和 ...
- WCF客户端获取服务器返回数据报错
错误信息:An error occurred while receiving the HTTP response to http://127.0.0.1/SIHIS/Infection/PubExec ...
- nodejs的cs模式聊天客户端和服务器实现
学习完nodejs的基础后,自然要写点东西练练手,以下是一个基于nodejs的cs模式的聊天软件代码: net模块是nodejs的网络编程必定用到的一个模块,对socket通信进行了封装 实现的功能: ...
- 加密解密(2)*客户端,服务器,CA(Certificate Authority),公钥,私钥,证书,签名,验证
加密解密(2)*客户端,服务器,CA(Certificate Authority),公钥,私钥,证书,签名,验证 各角色比喻 客户端:通常为请求方,要验证服务器的身份. 服务器:通常为响应方,有时也要 ...
- WCF客户端调用服务器端错误:"服务器已拒绝客户端凭据"。
WCF客户端和服务器端不在同一台机器上时,客户端调用服务器端会报如下错误:"服务器已拒绝客户端凭据". 解决办法:在服务端配置文件与客户端配置文件中加入下面红色部分
随机推荐
- Java编程思想 学习笔记9
九.接口 接口和内部类为我们提供了一种将接口与实现分离的更加结构化的方法. 1.抽象类和抽象方法 抽象类是普通的类与接口之间的一种中庸之道.创建抽象类是希望通过这个通用接口操纵一系列类. Java提 ...
- 01-VS充当IIS的配置步骤
一. 背景 在实际开发中,经常会遇到需要在线调试,比如:第三方支付的回调.App接口借助PostMan工具测试,需要在代码上直接加断点,来进行调试,VS默认是不支持这种方式,需要手动配置一下,才能达到 ...
- HTTP协议学习笔记---HTTP持久连接和如何正确地关闭HTTP连接
一,持久连接 什么是持久连接?对于HTTP协议而言,它是基于请求响应模型,Client向Server发请求时,先建立一条HTTP连接,Server给Client响应数据后,连接关闭. 当Client发 ...
- The android command is deprecated
新版的SDK tools中的android命令已经不支持 android create project,用起来很不顺手. The "android" command is depr ...
- 《深入理解java虚拟机》第二章 Java内存区域与内存溢出异常
第二章 Java内存区域与内存溢出异常 2.2 运行时数据区域
- Linux - openssl 加密
openssl rand 15 -base64 # 口令生成 openssl sha1 filename # 哈希算法校验文件 openssl md5 filename # MD5校验文件 opens ...
- mysql 查询优化 ~explain解读之select_type的解读
一 简介:今天咱们来聊聊explain的select_type 二 类型 (1)SIMPLE 简单的SELECT语句(不包括UNION操作或子查询操作) (2)PRIMARY/UNION PRIMAR ...
- python - 发送邮件(email模块(内置))
发送邮件 import smtplib from email.mime.text import MIMEText #邮箱件内容 HTML = """ 发送邮件测试2,加密 ...
- Android WebView常见问题及解决方案汇总【很全很实用】
http://www.cnblogs.com/olartan/p/5713013.html
- swift计算 switch case
var year = var month = var day = ; let daysOfFeb = year % == && year% != || year % == ?: var ...