实例介绍
【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel.Channels;
using System.ServiceModel;
using Contract;
using System.IO;
using System.Threading;
using SocketServiceContract;
namespace Client
{
public partial class MainForm : Form
{
public string ServiceIP = "192.168.0.96";
public MainForm()
{
InitializeComponent();
btnStart.Enabled = false;
btnSendMess.Enabled = false;
}
DuplexChannelFactory<IContract> duplexChannelFactory = null;
public DuplexChannelFactory<IContract> DuplexChannelFactory
{
get
{
if (duplexChannelFactory == null)
{
AddressHeaderCollection headers = new System.ServiceModel.Channels.AddressHeaderCollection();
NetTcpBinding tcpBinding = new NetTcpBinding();
tcpBinding.Security.Mode = SecurityMode.None;
tcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
tcpBinding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
tcpBinding.ReliableSession.Enabled = true;
EndpointAddress tcpAddress = new EndpointAddress("net.tcp://" ServiceIP ":9999/HelloWCFService");
duplexChannelFactory = new DuplexChannelFactory<IContract>(
new InstanceContext(
new CallBack((DownFile)delegate(CustomFileInfo currentDownFile)
{
using (FileStream fs = new FileStream("WCF" currentDownFile.FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
{
fs.Position = currentDownFile.CurrentOffset;
fs.Write(currentDownFile.CurrentData, 0, currentDownFile.CurrentLen);
fs.Flush();
fs.Close();
fs.Dispose();
}
long downSize = currentDownFile.CurrentOffset currentDownFile.CurrentLen;
int offset = (int)(downSize * 100 / currentDownFile.FileLength);
if (this.IsDisposed) return;
this.Invoke((MethodInvoker)delegate()
{
this.pbWCFDown.Value = offset;
this.lbWCFDownPos.Text = offset "%";
if (downSize > 1024 * 1024 * 1024)
{
float posLen = downSize / (1024f * 1024f * 1024f);
this.lbWCFDown.Text = posLen.ToString("f1") "G";
}
else if (downSize > 1024 * 1024)
{
float posLen = downSize / (1024f * 1024f);
this.lbWCFDown.Text = posLen.ToString("f1") "M";
}
else if (downSize > 1024)
{
float posLen = downSize / 1024f;
this.lbWCFDown.Text = posLen.ToString("f1") "K";
}
else
{ this.lbWCFDown.Text = downSize.ToString("f1") "B"; }
});
}
)), tcpBinding, tcpAddress);
duplexChannelFactory.Closing = delegate
{
helloWCF = null;
duplexChannelFactory = null;
};
}
return duplexChannelFactory;
}
}
IContract helloWCF = null;
public IContract HelloWCF
{
get
{
if (helloWCF == null)
{
helloWCF = DuplexChannelFactory.CreateChannel();
}
return helloWCF;
}
}
IContract helloAsyWCF = null;
public IContract HelloAsyWCF
{
get
{
if (helloAsyWCF == null)
{
helloAsyWCF = DuplexChannelFactory.CreateChannel();
}
return helloAsyWCF;
}
}
SynchroSocketClient synchroClient = new SynchroSocketClient();
AsynchronousSocketClient asynchronousClient = null;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
synchroClient.Revice = new ReviceBufferHandle(client_Revice);
}
void client_Revice(object sender, ReviceBuffer e)
{
SocketFileInfo fileInfo = SocketFileInfo.DeSerialize(e.ReviceByte);
using (FileStream fs = new FileStream("Socket" fileInfo.FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
{
fs.Position = fileInfo.CurrentOffset;
fs.Write(fileInfo.CurrentByte, 0, fileInfo.CurrentLen);
fs.Flush();
fs.Close();
fs.Dispose();
}
long downSize = fileInfo.CurrentOffset fileInfo.CurrentLen;
int offset = (int)(downSize * 100 / fileInfo.FileLength);
if (this.IsDisposed) return;
this.Invoke((MethodInvoker)delegate()
{
this.pbSocketDown.Value = offset;
this.lbSocketDownPos.Text = offset "%";
if (downSize > 1024 * 1024 * 1024)
{
float posLen = downSize / (1024f * 1024f * 1024f);
this.lbSocketDown.Text = posLen.ToString("f1") "G";
}
else if (downSize > 1024 * 1024)
{
float posLen = downSize / (1024f * 1024f);
this.lbSocketDown.Text = posLen.ToString("f1") "M";
}
else if (downSize > 1024)
{
float posLen = downSize / 1024f;
this.lbSocketDown.Text = posLen.ToString("f1") "K";
}
else
{ this.lbSocketDown.Text = downSize.ToString("f1") "B"; }
});
}
private void btnConnection_Click(object sender, EventArgs e)
{
asynchronousClient = new AsynchronousSocketClient(AsynchronousData);
synchroClient.Connect(ServiceIP, 7777);
asynchronousClient.Connect(ServiceIP, 7777);
HelloWCF.Connection();
HelloAsyWCF.Connection();
btnStart.Enabled = true;
btnSendMess.Enabled = true;
}
private void btnStart_Click(object sender, EventArgs e)
{
Thread downFile = new Thread((ThreadStart)delegate()
{
asynchronousClient.DownFile(txtServerFileName.Text);
});
Thread th = new Thread((ThreadStart)delegate()
{
DownFile("WCF同步" txtServerFileName.Text, txtServerFileName.Text);
});
string cmd = "GetFile " txtServerFileName.Text;
byte[] byt = Encoding.ASCII.GetBytes(cmd);
synchroClient.SendMess(byt);
downFile.Start();
HelloWCF.BeginDownFile(txtServerFileName.Text, null, null);
th.Start();
}
private void btnSendMess_Click(object sender, EventArgs e)
{
synchroClient.SendMess("消息抓包测试");
HelloWCF.SayHello("消息抓包测试");
}
public void AsynchronousData(long downSize, int offset)
{
this.Invoke((MethodInvoker)delegate()
{
this.pbSocketAsyDown.Value = offset;
this.lbSocketAsyDownPos.Text = offset "%";
if (downSize > 1024 * 1024 * 1024)
{
float posLen = downSize / (1024f * 1024f * 1024f);
this.lbSocketDownAsy.Text = posLen.ToString("f1") "G";
}
else if (downSize > 1024 * 1024)
{
float posLen = downSize / (1024f * 1024f);
this.lbSocketDownAsy.Text = posLen.ToString("f1") "M";
}
else if (downSize > 1024)
{
float posLen = downSize / 1024f;
this.lbSocketDownAsy.Text = posLen.ToString("f1") "K";
}
else
{ this.lbSocketDownAsy.Text = downSize.ToString("f1") "B"; }
});
}
private void DownFile(string LocalFileName, string ServerPath)
{
long filelenth = HelloAsyWCF.GetFileLenth(ServerPath);
if (filelenth == 0)
{
throw new Exception("服务器请求文件失败");
}
using (FileStream fs = new FileStream(LocalFileName, FileMode.Create))
{
int length = 0;
long CurrentLength = 0;
byte[] bts = HelloAsyWCF.ReadFileBtyes(ServerPath, 0, out length);
while (bts != null && length > 0)
{
CurrentLength = length;
fs.Write(bts, 0, length);
bts = HelloAsyWCF.ReadFileBtyes(ServerPath, CurrentLength, out length);
int Pos = Convert.ToInt32(CurrentLength * 100 / filelenth);//当前进度
long downSize = fs.Length;
int offset = (int)(downSize * 100 / filelenth);
if (this.IsDisposed) return;
this.Invoke((MethodInvoker)delegate()
{
this.pbDown.Value = offset;
this.lbDownPos.Text = offset "%";
if (downSize > 1024 * 1024 * 1024)
{
float posLen = downSize / (1024f * 1024f * 1024f);
this.lbDown.Text = posLen.ToString("f1") "G";
}
else if (downSize > 1024 * 1024)
{
float posLen = downSize / (1024f * 1024f);
this.lbDown.Text = posLen.ToString("f1") "M";
}
else if (downSize > 1024)
{
float posLen = downSize / 1024f;
this.lbDown.Text = posLen.ToString("f1") "K";
}
else
{ this.lbDown.Text = downSize.ToString("f1") "B"; }
});
}
fs.Close();
fs.Dispose();
}
}
}
}
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论