VBNET中TCP通讯代码(客户端和接受端集成在一个窗口中).doc_第1页
VBNET中TCP通讯代码(客户端和接受端集成在一个窗口中).doc_第2页
VBNET中TCP通讯代码(客户端和接受端集成在一个窗口中).doc_第3页
VBNET中TCP通讯代码(客户端和接受端集成在一个窗口中).doc_第4页
VBNET中TCP通讯代码(客户端和接受端集成在一个窗口中).doc_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

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

文档简介

VB.NET中TCP通讯代码(客户端和接受端集成在一个窗口中) _Partial Class Form1 Inherits System.Windows.Forms.Form Form overrides dispose to clean up the component list. _ Private Sub InitializeComponent() Me.Label1 = New System.Windows.Forms.Label Me.Label2 = New System.Windows.Forms.Label Me.TextBox1 = New System.Windows.Forms.TextBox Me.TextBox2 = New System.Windows.Forms.TextBox Me.BtnLink = New System.Windows.Forms.Button Me.BtnSend = New System.Windows.Forms.Button Me.LabService = New System.Windows.Forms.Label Me.ListBoxClientContext = New System.Windows.Forms.ListBox Me.StartService = New System.Windows.Forms.Button Me.StatusStrip1 = New System.Windows.Forms.StatusStrip Me.ToolStripLable1 = New System.Windows.Forms.ToolStripStatusLabel Me.ToolStripSeviceStatus = New System.Windows.Forms.ToolStripStatusLabel Me.StatusStrip1.SuspendLayout() Me.SuspendLayout() Label1 Me.Label1.AutoSize = True Me.Label1.Location = New System.Drawing.Point(25, 22) Me.Label1.Name = Label1 Me.Label1.Size = New System.Drawing.Size(77, 12) Me.Label1.TabIndex = 0 Me.Label1.Text = 服务器地址: Label2 Me.Label2.AutoSize = True Me.Label2.Location = New System.Drawing.Point(25, 69) Me.Label2.Name = Label2 Me.Label2.Size = New System.Drawing.Size(41, 12) Me.Label2.TabIndex = 1 Me.Label2.Text = 信息: TextBox1 Me.TextBox1.Location = New System.Drawing.Point(115, 19) Me.TextBox1.Name = TextBox1 Me.TextBox1.Size = New System.Drawing.Size(149, 21) Me.TextBox1.TabIndex = 2 TextBox2 Me.TextBox2.Location = New System.Drawing.Point(115, 69) Me.TextBox2.Name = TextBox2 Me.TextBox2.Size = New System.Drawing.Size(149, 21) Me.TextBox2.TabIndex = 3 BtnLink Me.BtnLink.Location = New System.Drawing.Point(27, 130) Me.BtnLink.Name = BtnLink Me.BtnLink.Size = New System.Drawing.Size(75, 23) Me.BtnLink.TabIndex = 4 Me.BtnLink.Text = 连接 Me.BtnLink.UseVisualStyleBackColor = True BtnSend Me.BtnSend.Location = New System.Drawing.Point(177, 129) Me.BtnSend.Name = BtnSend Me.BtnSend.Size = New System.Drawing.Size(75, 23) Me.BtnSend.TabIndex = 5 Me.BtnSend.Text = 发送 Me.BtnSend.UseVisualStyleBackColor = True LabService Me.LabService.AutoSize = True Me.LabService.Location = New System.Drawing.Point(25, 162) Me.LabService.Name = LabService Me.LabService.Size = New System.Drawing.Size(77, 12) Me.LabService.TabIndex = 0 Me.LabService.Text = 服务尚未启动 ListBoxClientContext Me.ListBoxClientContext.FormattingEnabled = True Me.ListBoxClientContext.ItemHeight = 12 Me.ListBoxClientContext.Location = New System.Drawing.Point(27, 182) Me.ListBoxClientContext.Name = ListBoxClientContext Me.ListBoxClientContext.Size = New System.Drawing.Size(245, 148) Me.ListBoxClientContext.TabIndex = 1 StartService Me.StartService.Location = New System.Drawing.Point(27, 351) Me.StartService.Name = StartService Me.StartService.Size = New System.Drawing.Size(75, 23) Me.StartService.TabIndex = 2 Me.StartService.Text = 启动服务 Me.StartService.UseVisualStyleBackColor = True StatusStrip1 Me.StatusStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() Me.ToolStripLable1, Me.ToolStripSeviceStatus) Me.StatusStrip1.Location = New System.Drawing.Point(0, 399) Me.StatusStrip1.Name = StatusStrip1 Me.StatusStrip1.Size = New System.Drawing.Size(299, 22) Me.StatusStrip1.TabIndex = 6 Me.StatusStrip1.Text = StatusStrip1 ToolStripLable1 Me.ToolStripLable1.Name = ToolStripLable1 Me.ToolStripLable1.Size = New System.Drawing.Size(55, 17) Me.ToolStripLable1.Text = 无连接! ToolStripSeviceStatus Me.ToolStripSeviceStatus.Name = ToolStripSeviceStatus Me.ToolStripSeviceStatus.Size = New System.Drawing.Size(91, 17) Me.ToolStripSeviceStatus.Text = 服务器无连接! Form1 Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(299, 421) Me.Controls.Add(Me.StatusStrip1) Me.Controls.Add(Me.BtnSend) Me.Controls.Add(Me.BtnLink) Me.Controls.Add(Me.TextBox2) Me.Controls.Add(Me.TextBox1) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.StartService) Me.Controls.Add(Me.ListBoxClientContext) Me.Controls.Add(Me.LabService) Me.Name = Form1 Me.Text = 客户端 Me.StatusStrip1.ResumeLayout(False) Me.StatusStrip1.PerformLayout() Me.ResumeLayout(False) Me.PerformLayout() End Sub Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents TextBox2 As System.Windows.Forms.TextBox Friend WithEvents BtnLink As System.Windows.Forms.Button Friend WithEvents BtnSend As System.Windows.Forms.Button Friend WithEvents LabService As System.Windows.Forms.Label Friend WithEvents ListBoxClientContext As System.Windows.Forms.ListBox Friend WithEvents StartService As System.Windows.Forms.Button Friend WithEvents StatusStrip1 As System.Windows.Forms.StatusStrip Friend WithEvents ToolStripLable1 As System.Windows.Forms.ToolStripStatusLabel Friend WithEvents ToolStripSeviceStatus As System.Windows.Forms.ToolStripStatusLabelEnd Class2、其次是类,即其控件所对应的方法,在这里注意的是,里面因为需要侦听端口号,所以需要新增一个线程,但是控件是不能跨线程使用的,需要用委托,代码如下:Imports System.Net.Sockets使用到TcpListen类Imports System.IO使用到StreamWriter类Imports System.Net使用IPAddress类、IPHostEntry类等Imports System.ThreadingPublic Class Form1 Client 端变量定义 Private swWriter As StreamWriter 用以向网络基础数据流传送数据 Private Client_nsStream As NetworkStream 创建发送数据的网络基础数据流 Private tcpClient As System.Net.Sockets.TcpClient 通过它实现向远程主机提出TCP连接申请 Private tcpConnect As Boolean = False 定义标识符,用以表示TCP连接是否建立 Server端变量定义 Private iPort As Integer = 11000 定义侦听端口号 Private thThreadRead As Thread 创建线程,用以侦听端口号,接收信息 Private tlTcpListen As TcpListener 侦听端口号 Private blistener As Boolean = True 设定标示位,判断侦听状态 Private Service_nsStream As NetworkStream 创建接收的基本数据流 Private srRead As StreamReader 从网络基础数据流中读取数据 Private tcClient As TcpClient 用于确认客户端TCP连接请求 Private Sub Listen() Try tlTcpListen = New TcpListener(iPort) 以8000端口号来初始化TcpListener实例 tlTcpListen.Start() 开始监听 Me.ToolStripSeviceStatus.Text = 正在监听. tcClient = tlTcpListen.AcceptTcpClient() 通过TCP连接请求 Service_nsStream = tcClient.GetStream() 获取用以发送、接收数据的网络基础数据流 srRead = New StreamReader(Service_nsStream) 以得到的网络基础数据流来初始化StreamReader实例 Me.ToolStripSeviceStatus.Text = 已经建立TCP连接! 循环侦听 While blistener Dim sMessage As String = srRead.ReadLine() 从网络基础数据流中读取一行数据 If (sMessage = STOP) Then Me.StartService.Enabled = True Me.StartService.BeginInvoke(New System.EventHandler(AddressOf StartServiceUpdateUI) tlTcpListen.Stop() 关闭侦听 Service_nsStream.Close() srRead.Close() 释放资源 Me.ToolStripSeviceStatus.Text = 无连接! Me.LabService.BeginInvoke(New System.EventHandler(AddressOf LabServiceUpdateUI) LabService.Text = 服务尚未启动! thThreadRead.Abort() 中止线程 Me.StartService.Enabled = True Return Else Try 判断是否为断开TCP连接控制码 Dim sTime As String = DateTime.Now.ToShortTimeString() 获取接收数据时的时间 ListBoxClientContext.BeginInvoke(New System.EventHandler(AddressOf ListBoxUpdateUI), sTime + + sMessage) ListBoxClientContext.Items.Add(sTime + + sMessage) Catch ex As Exception MessageBox.Show(ex.ToString() End Try End If End While Catch ex As System.Security.SecurityException MessageBox.Show(侦听失败!, 错误) End Try End Sub Private Sub StartServiceUpdateUI(ByVal sender As System.Object, ByVal e As System.EventArgs) Me.StartService.Enabled = True End Sub Private Sub LabServiceUpdateUI(ByVal sender As System.Object, ByVal e As System.EventArgs) Me.LabService.Text = 服务尚未启动! End Sub Private Sub ListBoxUpdateUI(ByVal sender As System.Object, ByVal e As System.EventArgs) ListBoxClientContext.Items.Add(sender.ToString() End Sub Private Sub StartService_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartService.Click thThreadRead = New Thread(New ThreadStart(AddressOf Listen) 以Listen过程来初始化线程实例 thThreadRead.Start() 启动线程 StartService.Enabled = False LabService.Text = 服务已经启动! LabService.ForeColor = Color.Red End Sub Private Sub BtnLink_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLink.Click Dim ipRemote As IPAddress Dim sHostName As String Dim tcpClient As TCPClient Try ipRemote = IPAddress.Parse(T

温馨提示

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

评论

0/150

提交评论