




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
贵州大学实验报告学院:计算机科学与技术学院 专业:数字媒体技术 班级:141姓名梁伟伦学号1400170151实验组实验时间2017.04.19指导教师蔡丽成绩实验项目名称实验五 TCP同步编程实验目的通过本实验掌握C#中TCP同步编程的方法,了解其区别与适用场合。实验要求熟悉Visual Studio开发环境,掌握C#的TCP同步编程。实验环境Visual Studio开发环境实验步骤1. 设计程序界面。2. 实现程序功能。实验内容实现简单的基于同步TCP的通信程序,要求使用C#的TCP同步方法。实验数据实验代码:public frmSyncTcpServer() InitializeComponent(); /显示消息 shwMsgforViewCallBack = new ShwMsgforViewCallBack(ShwMsgforView); /显示状态 shwStatusInfoCallBack = new ShwStatusInfoCallBack(ShwStatusInfo); /显示进度 shwProgressProcCallBack = new ShwProgressProcCallBack(ShwProgressProc); /重置消息文本 resetMsgTxtCallBack = new ResetMsgTxtCallBack(ResetMsgTxt); IPAddress listenIp = Dns.GetHostAddresses(); localAddress = listenIp0; tbxSendCount.Text = sendCount.ToString(); tbxReceiveCount.Text = receiveCount.ToString(); toolStripProgressProc.Minimum = 0; private void AcceptClientConnect() statusStripInfo.Invoke(shwStatusInfoCallBack, + localAddress + : + port + 侦听.); /间歇延时 DateTime nowtime = DateTime.Now; while (nowtime.AddSeconds(1) DateTime.Now) try statusStripInfo.Invoke(shwStatusInfoCallBack, 等待连接.); statusStripInfo.Invoke(shwProgressProcCallBack, 1); tcpClient = tcpListener.AcceptTcpClient(); /同步操作1 /附加操作1 statusStripInfo.Invoke(shwProgressProcCallBack, 100); if (tcpClient != null) statusStripInfo.Invoke(shwStatusInfoCallBack, 接受了一个连接.); networkStream = tcpClient.GetStream(); br = new BinaryReader(networkStream); bw = new BinaryWriter(networkStream); catch statusStripInfo.Invoke(shwStatusInfoCallBack, 停止侦听.); /间歇延时 DateTime now = DateTime.Now; while (now.AddSeconds(1) DateTime.Now) statusStripInfo.Invoke(shwProgressProcCallBack, 0); statusStripInfo.Invoke(shwStatusInfoCallBack, 就绪); C# SyncTcpClient 核心代码:private TcpClient tcpClient; private NetworkStream networkStream; private BinaryReader br; private BinaryWriter bw; private int sendCount = 1; private int receiveCount = 10; /显示消息 private delegate void ShwMsgforViewCallBack(string str); private ShwMsgforViewCallBack shwMsgforViewCallBack; /显示状态 private delegate void ShwStatusInfoCallBack(string str); private ShwStatusInfoCallBack shwStatusInfoCallBack; /显示进度 private delegate void ShwProgressProcCallBack(int progress); private ShwProgressProcCallBack shwProgressProcCallBack; /重置消息文本 private delegate void ResetMsgTxtCallBack(); private ResetMsgTxtCallBack resetMsgTxtCallBack; public frmSyncTcpClient() InitializeComponent(); /*定义委托*/ /显示消息 shwMsgforViewCallBack= new ShwMsgforViewCallBack(ShwMsgforView); /显示状态 shwStatusInfoCallBack = new ShwStatusInfoCallBack(ShwStatusInfo); /显示进度 shwProgressProcCallBack=new ShwProgressProcCallBack(ShwProgressProc); /重置消息文本 resetMsgTxtCallBack = new ResetMsgTxtCallBack(ResetMsgTxt); /*定义委托*/ IPAddress serverIp = Dns.GetHostAddresses(); tbxSrvIp.Text = serverIp0.ToString(); tbxSrvIp.SelectAll(); tbxPort.Text = 51888; tbxSendCount.Text = sendCount.ToString(); tbxReceiveCount.Text = receiveCount.ToString(); toolStripProgressProc.Minimum = 0; /*定义回调函数*/ /显示消息 private void ShwMsgforView(string str) lstbxMsgView.Items.Add(str); lstbxMsgView.TopIndex = lstbxMsgView.Items.Count - 1; /显示状态 private void ShwStatusInfo(string str) toolStripStatusInfo.Text = str; /显示进度 private void ShwProgressProc(int progress) toolStripProgressProc.Value = progress; /重置消息文本 private void ResetMsgTxt() tbxMsg.Text = ; tbxMsg.Focus(); /*定义回调函数*/ private void btnConnect_Click(object sender, EventArgs e) toolStripProgressProc.Maximum = 100; toolStripProgressProc.Value = 0; /通过一个线程发起请求 Thread threadConnect = new Thread(ConnectoServer); threadConnect.Start(); /发起连接请求 private void ConnectoServer() try statusStripInfo.Invoke(shwStatusInfoCallBack, 正在连接.); IPHostEntry remoteHost = Dns.GetHostEntry(tbxSrvIp.Text); tcpClient = new TcpClient(); statusStripInfo.Invoke(shwProgressProcCallBack, 1); tcpClient.Connect(remoteHost.HostName, int.Parse(tbxPort.Text); /非同步 /非同步操作 statusStripInfo.Invoke(shwProgressProcCallBack, 100); /间歇延时 DateTime nowtime = DateTime.Now; while (nowtime.AddSeconds(1) DateTime.Now) if (tcpClient != null) statusStripInfo.Invoke(shwStatusInfoCallBack, 连接成功!); networkStream = tcpClient.GetStream(); br = new BinaryReader(networkStream); bw = new BinaryWriter(networkStream); catch statusStripInfo.Invoke(shwStatusInfoCallBack, 连接失败!); /间歇延时 DateTime now = DateTime.Now; while (now.AddSeconds(1) DateTime.Now) statusStripInfo.Invoke(shwProgressProcCallBack, 0); statusStripInfo.Invoke(shwStatusInfoCallBack, 就绪); private void btnReceive_Click(object sender, EventArgs e) receiveCount = int.Parse(tbxReceiveCount.Text); toolStripProgressProc.Maximum = receiveCount; toolStripProgressProc.Value = 0; Thread threadReceive = new Thread(ReceiveMessage); threadReceive.Start(); /接收消息 private void ReceiveMessage() statusStripInfo.Invoke(shwStatusInfoCallBack, 接收中.); for (int i = 0; i receiveCount; i+) try string rcvMsgStr = br.ReadString(); /同步操作1 /附加操作1 statusStripInfo.Invoke(shwProgressProcCallBack, i + 1); if (rcvMsgStr != null) lstbxMsgView.Invoke(shwMsgforViewCallBack, rcvMsgStr); catch if (br != null) br.Close(); if (bw != null) bw.Close(); if (tcpClient != null) tcpClient.Close(); statusStripInfo.Invoke(shwStatusInfoCallBack, 连接断开!); statusStripInfo.Invoke(shwProgressProcCallBack, 0); break; statusStripInfo.Invoke(shwStatusInfoCallBack, 接收了 + receiveCount + 条消息.); private void btnSend_Click(object sender, EventArgs e) sendCount = int.Parse(tbxSendCount.Text); toolStripProgressProc.Maximum = sendCount; toolStripProgressProc.Value = 0; Thread threadSend = new Thread(new ParameterizedThreadStart(SendMessage); threadSend.Start(tbxMsg.Text); /发送消息 private void SendMessage(object state) statusStripInfo.Invoke(shwStatusInfoCallBack, 正在发送.); for (int i = 0; i DateTime.Now) bw.Flush(); catch if (br != null) br.Close(); if (bw != null) bw.Close(); if (tcpClient != null) tcpClient.Close(); statusStripInfo.Invoke(shwStatusInfoCallBack, 连接断开!); statusStri
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 内网安全培训思路课件
- 内经选读病之形能课件
- 内科中西医结合课件
- 单例模式与自适应算法的结合研究-洞察及研究
- 统编版一年级上册语文园地五 公开课一等奖创新教学设计
- 2025年秋部编版语文四上口语交际 讲历史人物故事(公开课一等奖创新教案+)
- 七下第四单元作业设计(表格式)
- 创业青年培训安全协议课件
- 文库发布:化合价课件
- 创业基础理论课件
- 《无人机飞行控制技术》全套教学课件
- 环境反应工程导论课件
- 超声诊断在肱骨外上髁炎(网球肘)中的应用
- 舆论导向培训课件
- 腮腺脓肿护理查房
- 保管员技师考试题及答案
- 消防自动灭火系统课件
- (2025.06.12)领导干部任前应知应会党内法规和法律知识考试题库(2025年度)
- 关于数据安全管理制度
- 2025年安徽省农业职业技能大赛(水生物病害防治员)备赛试题库(含答案)
- 华中师范大学第─附属中学2025届高三下五月高考模拟英语试卷
评论
0/150
提交评论