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客户端和服务器端不在同一台机器上时,客户端调用服务器端会报如下错误:"服务器已拒绝客户端凭据". 解决办法:在服务端配置文件与客户端配置文件中加入下面红色部分
随机推荐
- Redis模块学习笔记(一)RediSearch简单使用
说明:安装的Redis服务器必须为 4.0 以上版本,通过info命令查看 > INFO redis_version: 一.安装 RediSearch git clone https://git ...
- MySQL - 日常操作一 增删改查
mysql 源码安装 创建 mysql 账户 组 groupadd mysql useradd mysql -g mysql -M -s /bin/false 解压缩源码安装 .tar.gz cd ...
- apache - http
apahce 添加模块编译 httpd # so模块用来提供DSO支持的apache核心模块 # 如果编译中包含任何DSO模块,则mod_so会被自动包含进核心. # 如果希望核心能够装载DSO, ...
- Linux - 文件ACL权限控制
getfacl 1.test # 查看文件ACL权限 setfacl -R -m u:xuesong:rw- 1.test # 对文件增加用户的读写权限 -R 递归
- JavaScript之调试工具之断言assert
1.单点断言 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <ti ...
- resolution will not be reattempted until the update interval of repository-group has elapsed or updates are forced
Failed to execute goal on project safetan-web: Could not resolve dependencies for project com.safeta ...
- 【Eclipse】Eclipse中打开cmd窗口和terminal窗口
在IDEA的时候可以直接使用terminal打开类似于cmd窗口的功能,于是想着在eclipse也使用类似的功能. 1.Eclipse打开类似于cmd窗口的功能.(DOS) 1.window——> ...
- ppt 制作圆角三角形
制作圆角三角形: PART 01 :插入三角形与三个等大的圆形: PART 02 :利用[任意多边形]和[合并形状-剪除]获得缺三角: (先选中大三角形,然后再选中任意多边形,"格式&quo ...
- Linux下的换行符\n\r以及txt和word文档的使用
Linux doc WINDOWS下记事本编写的文档和LINUX下VIM或者GEDIT等编写的文档的不同! 例如WINDOWS下编写的SH脚本,放到LINUX下执行可能会出错. 解决方法: 原因是:W ...
- C++学习2--坦克大战编写-前置知识
基础班学习的这一个多月里的前三周讲解基础的语法,最后一周需要做坦克大战的项目巩固提高自己掌握的语法知识.这个系列博文主要是为了把学习过程中的知识点总结并记录下来: 开发语言与开发工具:C++,VS20 ...