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客户端和服务器端不在同一台机器上时,客户端调用服务器端会报如下错误:"服务器已拒绝客户端凭据". 解决办法:在服务端配置文件与客户端配置文件中加入下面红色部分
随机推荐
- JMS的P2P和PUB/SUB区别和应用场景介绍【转】
完全引用自: https://my.oschina.net/alexgaoyh/blog/338890 解释得非常棒. 1.P2P模型 在P2P模型中,有下列概念: 消息队列(Queue). 发送者 ...
- Study 2 —— 格式化输出
打印人物信息的两种方法第一种: Name = input('Input your name: ') Age = input('Input your age: ') Job = input('Input ...
- 将文本(lrc,txt)文件转换成UTF-8格式
UTF-8是UNICODE的一种变长字符编码又称万国码,由Ken Thompson于1992年创建.现在已经标准化为RFC 3629.UTF-8用1到6个字节编码UNICODE字符.用在网页上可以同一 ...
- json 不能 dumps Decimal 解决办法
class DecimalEncoder(json.JSONEncoder): def default(self, o): if isinstance(o, decimal.Decimal): ret ...
- Postfix 邮件服务 - postfix服务
postfix 邮件服务 也是基于sendmail (需要关闭或卸载sendmail)postfix 是一个电子邮件服务器,它为了改良sendmail邮件服务器而产生的,并且它的配置文件比sendma ...
- elasticsearch 单机多实例
elasticsearch 配置单机器多实例 host: - - path data: /opt/elasticsearch/data/node1 /opt/elasticsearch/data/no ...
- Wannafly挑战赛21 E 未来城市规划
传送门 题目中给的信息很难直接维护,但是可以考虑一条边对答案的贡献 在以\(x\)为根的子树里,如果一条边\(i\)的权值为\(w_i\),这条边深度更深的端点为\(to_i\),那么这条边对这个子树 ...
- [JSOI2008]球形空间产生器 (高斯消元)
[JSOI2008]球形空间产生器 \(solution:\) 非常明显的一道高斯消元.给了你n+1个球上的位置,我们知道球上任何一点到球心的距离是相等,所以我们 可以利用这一个性质.我们用n+1个球 ...
- Pyqt5自定义浏览器
from PyQt5.QtWebChannel import QWebChannel from PyQt5.QtWebEngineWidgets import QWebEngineView from ...
- vue学习之用 Vue.js + Vue Router 创建单页应用的几个步骤
通过vue学习一:新建或打开vue项目,创建好项目后,接下来的操作为: src目录重新规划——>新建几个页面——>配置这几个页面的路由——>给根实例注入路由配置 src目录重整 在项 ...