数媒梁伟伦1400170151实验_第1页
数媒梁伟伦1400170151实验_第2页
数媒梁伟伦1400170151实验_第3页
数媒梁伟伦1400170151实验_第4页
数媒梁伟伦1400170151实验_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选优质文档-倾情为你奉上精选优质文档-倾情为你奉上专心-专注-专业专心-专注-专业精选优质文档-倾情为你奉上专心-专注-专业贵州大学实验报告学院:计算机科学与技术学院 专业:数字媒体技术 班级:141姓名梁伟伦学号实验组实验时间2017.04.19指导教师蔡丽成绩实验项目名称实验五 TCP同步编程实验目的通过本实验掌握C#中TCP同步编程的方法,了解其区别与适用场合。实验要求熟悉Visual Studio开发环境,掌握C#的TCP同步编程。实验环境Visual Studio开发环境实验步骤1. 设计程序界面。2. 实现程序功能。实验内容实现简单的基于同步TCP的通信程序,要求使用C#的TCP

2、同步方法。实验数据实验代码:public frmSyncTcpServer() InitializeComponent(); /显示消息 shwMsgforViewCallBack = new ShwMsgforViewCallBack(ShwMsgforView); /显示状态 shwStatusInfoCallBack = new ShwStatusInfoCallBack(ShwStatusInfo); /显示进度 shwProgressProcCallBack = new ShwProgressProcCallBack(ShwProgressProc); /重置消息文本 resetMs

3、gTxtCallBack = new ResetMsgTxtCallBack(ResetMsgTxt); IPAddress listenIp = Dns.GetHostAddresses(); localAddress = listenIp0; tbxSendCount.Text = sendCount.ToString(); tbxReceiveCount.Text = receiveCount.ToString(); toolStripProgressProc.Minimum = 0; private void AcceptClientConnect() statusStripInfo.

4、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(); /

5、同步操作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(shwStatus

6、InfoCallBack, 停止侦听.); /间歇延时 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 Binar

7、yReader 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 ShwStatusInfoCallB

8、ack shwStatusInfoCallBack; /显示进度 private delegate void ShwProgressProcCallBack(int progress); private ShwProgressProcCallBack shwProgressProcCallBack; /重置消息文本 private delegate void ResetMsgTxtCallBack(); private ResetMsgTxtCallBack resetMsgTxtCallBack; public frmSyncTcpClient() InitializeComponent()

9、; /*定义委托*/ /显示消息 shwMsgforViewCallBack= new ShwMsgforViewCallBack(ShwMsgforView); /显示状态 shwStatusInfoCallBack = new ShwStatusInfoCallBack(ShwStatusInfo); /显示进度 shwProgressProcCallBack=new ShwProgressProcCallBack(ShwProgressProc); /重置消息文本 resetMsgTxtCallBack = new ResetMsgTxtCallBack(ResetMsgTxt); /*

10、定义委托*/ 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 ShwMsgforVi

11、ew(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() t

12、bxMsg.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 st

13、atusStripInfo.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(shwProgres

14、sProcCallBack, 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

15、 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) receiveCou

16、nt = 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 re

17、ceiveCount; 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.C

18、lose(); 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); toolStri

19、pProgressProc.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, 连接断开!); stat

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论