




已阅读5页,还剩30页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C#winform网络编程TCP程序帮助文档1、 界面的实现服务器界面客户端界面2、 界面控件解析以客户端界面为例,整个界面运用的控件,功能。 进度条的实现主要是StatusStrip(状态栏)控件下的ToolStripStatusLabel(标签) ProgressBar(进度条)控件 3、 类的调用解析System.Net命名控件为当前网络上使用的多种协议提供了简单的编程接口 下面对该命名控件进行类的介绍Dns类:从Internet域名系统(DNS)检索关于特定主机的信息IPAddress类:提供网际协议(IP)地址IPEndPoint类:包含应用程序连接到主机上的服务所需的主机和本地或远程端口信息WebClient类:提供向URI标识的任何本地、Intranet或Internet资源发送数据以及从这些资源接受数据的公共方法System.Net.Sockets命名控件主要提供制作Sockets网络应用程序的相关类Socket类:为网络通信提供了一套丰富的方法和属性,主要用于管理连接,实现Berkeley通信端套接字接口,同时他还定义了绑定,连接网络端口点及传输数据所需的各种方法,提供处理端点连接传输等细节所需要的功能。TcpClient类:用于在同步阻止模式下通过网络来连接,发送和接收流数据TcpListenner类:用于阻止同步模式下侦听和接受传入的连接请求UdpClient类:用于阻止同步模式下发送和接收无连接UDP数据报。(UDP是无连接传输协议)System.Threading命名控件用来创建线程和控制线程的System.IO命名控件提供了对系统文件的存取、创建、修改等封装类。4、 编写思路1、 服务器端(1) 创建一个TcpListener对象,然后调用该对象的Start方法在指定的端口进行监听。Private IPAddress localAddress;/IP地址Private const int port=51888;/端口Private TcpListener tcpListener;/监听套接字/初始化IPAddress listenIP=Dns.GetHostAddresses(“”);localAddress=listenIP0;/创建TcpListener对象,开启监听tcpListener=new TcpListener(localAddress,port);tcpListener.Start();/监听开始(2) 在单独的线程中,首先循环调用AcceptTcpClient方法接受客户端的连接请求,从该方法的返回结果中得到与客户端对应的TcpClient对象,并利用该对象的GetStream方法得到NetworkStream对象。然后再利用该对象得到其他使用更方便的对象,为进一步通信做准备/启动一个线程接收请求Thread threadAccept=new Thread(AcceptClientConnect);threadAccept.Start();/线程执行AcceptClientConnect方法请求private void AcceptClientConnect() while (true) try tcpClient = tcpListener.AcceptTcpClient();/接收客户端的请求 if (tcpClient != null) stsInfo.Invoke(shwStatusInfo, 接受了一个连接); networkStream = tcpClient.GetStream();/接收或者发送数据 br = new BinaryReader(networkStream); bw = new BinaryWriter(networkStream); catch.(3) 每得到一得到新的TcpClient对象,就创建一个与该客户对应的线程,在线程中与对应的客户进行通信Thread threadReceive = new Thread(ReceiveMessage);threadReceive.Start();其中,ReceiveMessage是接收消息的方法,当然,在得到TcpClient对象后,也可以分别创建两个线程,一恶搞用于接收信息,另一个用于发送信息。例如,定义发送信息的方法SendMessage后,可创建一个发送消息的线程: Thread threadSend = new Thread(new ParameterizedThreadStart(SendMessage); threadSend.Start(txtMsg.Text);(4) 根据传送信息的情况确定是否关闭与客户的连接if (br != null) br.Close(); if (bw != null) bw.Close(); if (tcpClient != null) tcpClient.Close(); tssInfo.Text = 连接断开;在连接关闭之前,要先关闭读写流br和bw。在停止服务后,服务器可以断开监听。tcpListener.Stop();/关闭侦听2、 客户端(1) 利用TcpClient的构造函数创建一个TcpClient对象 private TcpClient tcpClient;/为TCP网络服务提供客户端连接tcpClient=new TcpClient();(2) 使用Connect方法与服务器建立连接tcpClient.Connect(remoteHost.HostName,int.Parse(txtPort.Text);(3) 利用TcpClient对象的GetStream方法得到网络流,然后利用该网络流与服务器进行数据传输if(tcpClient!=null) stsInfo.Invoke(shwStatusInfo,连接成功); networkStream=tcpClient.GetStream(); br=new BinaryReader(networkStream); bw=new BinaryWriter(networkStream);(4) 创建一个线程监听指定的端口,循环接收并处理服务器发送过来的信息Thread threadReceive=new Thread(ReceiveMessage);threadReceive.Start();private void ReceiveMessage() for(int i=0;ireceiveCount;i+) try string rcvMsgStr=br.ReadString();/读取字符串 if(rcvMsgStr!=null) lstbxMsgView.Invoke(shwMsgforView,rcvMsgStr); catch break; (5) 完成工作后,向服务器发送关闭信息,并关闭与服务器的连接5、 操作步骤1、 服务器接受请求(tcpClient=tcpListener.AcceptTcpClient())服务器先点击“开始侦听”然后在点击客户端的“连接”2、 接收数据(string rcvMsgStr=br.ReadString()由于该项目是TCP同步的,每次客户端或者服务器发送信息后,必须点击“接收”按钮。否则消息是无法显示出来的3、 发送数据(bw.Write(state.ToString()6、 源代码服务器功能源代码using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Net;using System.Net.Sockets;using System.Threading;using System.IO;namespace SyncTcpServer public partial class frmSyncTcpServer : Form private IPAddress localAddress; private const int port = 51888; private TcpListener tcpListener;/侦听TCP客户端的连接 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 shwMsgforView; /显示状态 private delegate void ShwStatusInfoCallBack(string str); private ShwStatusInfoCallBack shwStatusInfo; /显示进度 private delegate void ShwProgressProcCallBack(int Progress); private ShwProgressProcCallBack shwProgressProc; /重置消息文本 private delegate void ResetMsgTxtCallBack(); private ResetMsgTxtCallBack resetMsgTxt; public frmSyncTcpServer() InitializeComponent(); /*-定义委托-*/ /显示消息 shwMsgforView = new ShwMsgforViewCallBack(ShwMsgforView); /显示状态 shwStatusInfo = new ShwStatusInfoCallBack(ShwStatusInfo); /显示进度 shwProgressProc = new ShwProgressProcCallBack(ShwProgressProc); /重置消息文本 resetMsgTxt = new ResetMsgTxtCallBack(ResetMsgTxt); IPAddress listenIP = Dns.GetHostAddresses(); localAddress = listenIP0; txtSendCount.Text = sendCount.ToString(); txtReceiveCount.Text = receiveCount.ToString(); tspbProc.Minimum = 0; /*定义回调函数 * 允许跨线程访问Windows窗体的控件 */ / / 显示消息 / / private void ShwMsgforView(string str) lstbxMsgView.Items.Add(str); lstbxMsgView.TopIndex = lstbxMsgView.Items.Count - 1; / / 显示状态 / / private void ShwStatusInfo(string str) tssInfo.Text = str;/状态栏中标签的赋值 / / 显示进度 / / private void ShwProgressProc(int Progress) tspbProc.Value = Progress;/进度条的显示 / / 重置消息文本 / private void ResetMsgTxt() txtMsg.Text = ; txtMsg.Focus(); #region 开始侦听事件的处理 / / 接收请求 / private void AcceptClientConnect() stsInfo.Invoke(shwStatusInfo, + localAddress + : + port + 侦听.); /间隔延时 DateTime nowtime = DateTime.Now; while (nowtime.AddSeconds(1) DateTime.Now) try stsInfo.Invoke(shwStatusInfo, 等待连接.); stsInfo.Invoke(shwProgressProc, 1); tcpClient = tcpListener.AcceptTcpClient();/接收客户端的请求 stsInfo.Invoke(shwProgressProc, 100); if (tcpClient != null) stsInfo.Invoke(shwStatusInfo, 接受了一个连接); networkStream = tcpClient.GetStream();/接收或者发送数据 br = new BinaryReader(networkStream); bw = new BinaryWriter(networkStream); catch stsInfo.Invoke(shwStatusInfo, 停止侦听); DateTime now = DateTime.Now; while (now.AddSeconds(1) DateTime.Now) stsInfo.Invoke(shwProgressProc, 0); stsInfo.Invoke(shwStatusInfo, 就绪); / / 开始侦听事件 / / / private void btnStart_Click(object sender, EventArgs e) tcpListener = new TcpListener(localAddress, port); tcpListener.Start(); tspbProc.Maximum = 100; tspbProc.Value = 0; Thread threadAccept = new Thread(AcceptClientConnect); threadAccept.Start(); #endregion #region 接收数据 / / 接收消息 / private void ReceiveMessage() stsInfo.Invoke(shwStatusInfo, 接收中.); for (int i = 0; i DateTime.Now) Thread threadAccept = new Thread(AcceptClientConnect); threadAccept.Start(); break; stsInfo.Invoke(shwStatusInfo, 接收了 + receiveCount + 条消息.); / / 接收事件 / / / private void btnReceive_Click(object sender, EventArgs e) receiveCount = int.Parse(txtReceiveCount.Text);/接收次数 tspbProc.Maximum = receiveCount; tspbProc.Value = 0; Thread threadReceive = new Thread(ReceiveMessage); threadReceive.Start(); #endregion #region 发送消息的处理 / / 发送消息 / / private void SendMessage(object state) stsInfo.Invoke(shwStatusInfo, 正在发送.); 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(); stsInfo.Invoke(shwStatusInfo, 连接断开!); stsInfo.Invoke(shwProgressProc, 0); DateTime now = DateTime.Now; while (now.AddSeconds(2) DateTime.Now) Thread threadAccept = new Thread(AcceptClientConnect); threadAccept.Start(); break; stsInfo.Invoke(shwStatusInfo, 完毕); DateTime nowtime = DateTime.Now; while (nowtime.AddSeconds(1) DateTime.Now) stsInfo.Invoke(shwProgressProc, 0); txtMsg.Invoke(resetMsgTxt, null); / / 发送事件 / / / private void btnSend_Click(object sender, EventArgs e) sendCount = int.Parse(txtSendCount.Text); tspbProc.Maximum = sendCount; tspbProc.Value = 0; Thread threadSend = new Thread(new ParameterizedThreadStart(SendMessage); threadSend.Start(txtMsg.Text); #endregion / / 断开事件 / / / private void btnDisconnect_Click(object sender, EventArgs e) if (br != null) br.Close(); if (bw != null) bw.Close(); if (tcpClient != null) tcpClient.Close(); tssInfo.Text = 连接断开; tspbProc.Value = 0; DateTime now = DateTime.Now; while (now.AddSeconds(2) DateTime.Now) Thread threadAccept = new Thread(AcceptClientConnect); threadAccept.Start(); / / 关闭侦听 / / / private void btnStop_Click(object sender, EventArgs e) tcpListener.Stop();/关闭侦听 / / 数据情况的处理 / / / private void btnClear_Click(object sender, EventArgs e) lstbxMsgView.Items.Clear(); 服务器界面namespace SyncTcpServer partial class frmSyncTcpServer / / 必需的设计器变量。 / private System.ComponentModel.IContainer components = null; / / 清理所有正在使用的资源。 / / 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) if (disposing & (components != null) components.Dispose(); base.Dispose(disposing); #region Windows 窗体设计器生成的代码 / / 设计器支持所需的方法 - 不要 / 使用代码编辑器修改此方法的内容。 / private void InitializeComponent() this.lstbxMsgView = new System.Windows.Forms.ListBox(); this.txtSendCount = new System.Windows.Forms.TextBox(); this.txtReceiveCount = new System.Windows.Forms.TextBox(); this.btnClear = new System.Windows.Forms.Button(); this.btnReceive = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.txtMsg = new System.Windows.Forms.TextBox(); this.btnSend = new System.Windows.Forms.Button(); this.btnStart = new System.Windows.Forms.Button(); this.btnStop = new System.Windows.Forms.Button(); this.btnDisconnect = new System.Windows.Forms.Button(); this.stsInfo = new System.Windows.Forms.StatusStrip(); this.tssInfo = new System.Windows.Forms.ToolStripStatusLabel(); this.tspbProc = new System.Windows.Forms.ToolStripProgressBar(); this.stsInfo.SuspendLayout(); this.SuspendLayout(); / / lstbxMsgView / this.lstbxMsgView.FormattingEnabled = true; this.lstbxMsgView.ItemHeight = 12; this.lstbxMsgView.Location = new System.Drawing.Point(12, 12); this.lstbxMsgView.Name = lstbxMsgView; this.lstbxMsgView.Size = new System.Drawing.Size(489, 124); this.lstbxMsgView.TabIndex = 0; / / txtSendCount / this.txtSendCount.Font = new System.Drawing.Font(宋体, 12F); this.txtSendCount.Location = new System.Drawing.Point(56, 152); this.txtSendCount.Name = txtSendCount; this.txtSendCount.Size = new System.Drawing.Size(53, 26); this.txtSendCount.TabIndex = 1; / / txtReceiveCount / this.txtReceiveCount.Font = new System.Drawing.Font(宋体, 12F); this.txtReceiveCount.Location = new System.Drawing.Point(169, 152); this.txtReceiveCount.Name = txtReceiveCount; this.txtReceiveCount.Size = new System.Drawing.Size(52, 26); this.txtReceiveCount.TabIndex = 2; / / btnClear / this.btnClear.Font = new System.Drawing.Font(宋体, 12F); this.btnClear.Location = new System.Drawing.Point(307, 152); this.btnClear.Name = btnClear; this.btnClear.Size = new System.Drawing.Size(66, 23); this.btnClear.TabIndex = 3; this.btnClear.Text = 清空; this.btnClear.UseVisualStyleBackColor = true; this.btnClear.Click += new System.EventHandler(this.btnClear_Click); / / btnReceive / this.btnReceive.Font
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025广东江门新会区会城街道今古洲社区公益性岗位招聘1人模拟试卷及参考答案详解1套
- 2025年中国地质调查局西安矿产资源调查中心招聘(26人)考前自测高频考点模拟试题及完整答案详解
- 2025年永康市属事业单位考试试卷
- 2025衡水冀州区招聘第二批社区工作者72名模拟试卷及答案详解(考点梳理)
- 2025江苏盐城选聘物业管理营商环境体验员考前自测高频考点模拟试题及答案详解(有一套)
- 2025内蒙古赤峰市喀喇沁旗锦山第三中学“绿色通道”引进教师第二次3人模拟试卷及一套参考答案详解
- 2025年浙江宁波市卫生健康委部分直属事业单位公开招聘高层次人才69人(第二批)考前自测高频考点模拟试题及1套参考答案详解
- 2025广东广州市公安局招聘辅警48人考前自测高频考点模拟试题及一套答案详解
- 2025江西上饶市信州区投资控股集团有限公司第一次招聘6人模拟试卷及答案详解一套
- 2025黑龙江哈尔滨工程大学后勤基建处前期采购办公室管理岗位招聘1人模拟试卷含答案详解
- 型钢混凝土结构钢筋施工
- 石群邱关源电路(第1至7单元)白底课件
- JJF 1338-2012相控阵超声探伤仪校准规范
- GB/T 40529-2021船舶与海洋技术起货绞车
- GB 31603-2015食品安全国家标准食品接触材料及制品生产通用卫生规范
- 关于公布2016年度中国电力优质工程奖评审结果的通知
- 港口集团绩效考核方案
- 送达地址确认书(诉讼类范本)
- 2023版北京协和医院重症医学科诊疗常规
- 三坐标测量基础知识(基础教育)
- 宜家战略分析(课堂PPT)
评论
0/150
提交评论