基本信息
源码名称:tcp 二进制通讯实例源码下载
源码大小:0.33M
文件格式:.rar
开发语言:C#
更新时间:2014-06-18
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using StriveEngine.Tcp.Passive; using StriveEngine.BinaryDemoCore; namespace StriveEngine.BinaryDemo { public partial class Form1 : Form { private ITcpPassiveEngine tcpPassiveEngine; public Form1() { InitializeComponent(); this.comboBox1.SelectedIndex = 0; } private void button3_Click(object sender, EventArgs e) { try { //初始化并启动客户端引擎(TCP、文本协议) this.tcpPassiveEngine = NetworkEngineFactory.CreateStreamTcpPassivEngine(this.textBox_IP.Text, int.Parse(this.textBox_port.Text), new StreamContractHelper()); this.tcpPassiveEngine.MessageReceived = new CbDelegate<System.Net.IPEndPoint, byte[]>(tcpPassiveEngine_MessageReceived); this.tcpPassiveEngine.AutoReconnect = true;//启动掉线自动重连 this.tcpPassiveEngine.ConnectionInterrupted = new CbDelegate(tcpPassiveEngine_ConnectionInterrupted); this.tcpPassiveEngine.ConnectionRebuildSucceed = new CbDelegate(tcpPassiveEngine_ConnectionRebuildSucceed); this.tcpPassiveEngine.Initialize(); this.textBox_IP.ReadOnly = true; this.textBox_port.ReadOnly = true; this.button3.Enabled = false; this.button1.Enabled = true; MessageBox.Show("连接成功!"); } catch (Exception ee) { MessageBox.Show(ee.Message); } } private void button1_Click(object sender, EventArgs e) { this.label_result.Text = "-"; int msgType = this.comboBox1.SelectedIndex == 0 ? MessageType.Add : MessageType.Multiple; //请求消息体 RequestContract contract = new RequestContract(int.Parse(this.textBox1.Text), int.Parse(this.textBox2.Text)); byte[] bBody = SerializeHelper.SerializeObject(contract); //消息头 MessageHead head = new MessageHead(bBody.Length,msgType) ; byte[] bHead = head.ToStream(); //构建请求消息 byte[] reqMessage = new byte[bHead.Length bBody.Length]; Buffer.BlockCopy(bHead, 0, reqMessage, 0, bHead.Length); Buffer.BlockCopy(bBody, 0, reqMessage, bHead.Length, bBody.Length); //发送请求消息 this.tcpPassiveEngine.PostMessageToServer(reqMessage); } void tcpPassiveEngine_ConnectionRebuildSucceed() { if (this.InvokeRequired) { this.BeginInvoke(new CbDelegate(this.tcpPassiveEngine_ConnectionInterrupted)); } else { this.button1.Enabled = true; MessageBox.Show("重连成功。"); } } void tcpPassiveEngine_ConnectionInterrupted() { if (this.InvokeRequired) { this.BeginInvoke(new CbDelegate(this.tcpPassiveEngine_ConnectionInterrupted)); } else { this.button1.Enabled = false; MessageBox.Show("您已经掉线。"); } } void tcpPassiveEngine_MessageReceived(System.Net.IPEndPoint serverIPE, byte[] bMsg) { //获取消息类型 int msgType = BitConverter.ToInt32(bMsg, 4);//消息类型是 从offset=4处开始 的一个整数 if (msgType != MessageType.Result) { return; } //解析消息体 ResponseContract response = (ResponseContract)SerializeHelper.DeserializeBytes(bMsg, MessageHead.HeadLength, bMsg.Length - MessageHead.HeadLength); string result = string.Format("{0}与{1}{2}的答案是 {3}" ,response.Number1,response.Number2,response.OperationType,response.Result); this.ShowResult(result); } private void ShowResult(string result) { if (this.InvokeRequired) { this.BeginInvoke(new CbDelegate<string>(this.ShowResult), result); } else { this.label_result.Text = result; } } } }