Imports System.Net.Sockets '使用到TcpListen类 Imports System.IO '使用到StreamWriter类 Imports System.Net '使用IPAddress类、IPHostEntry类等 Imports System.Threading Public 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(TextBox1.Text) Catch MessageBox.Show("输入的IP地址不合法!", "错误提示!") Return '判断给定的IP地址的合法性 End Try Try
tcpClient = New Sockets.TcpClient(TextBox1.Text, 11000) '对远程主机的8000端口提出TCP连接申请 Client_nsStream = tcpClient.GetStream() '通过申请,并获取传送数据的网络基础数据流 swWriter = New StreamWriter(Client_nsStream) '使用获取的网络基础数据流来初始化StreamWriter实例 BtnLink.Enabled = False BtnSend.Enabled = True tcpConnect = True Me.ToolStripLable1.Text = "已经连接!" Catch ex As Exception MessageBox.Show("无法和远程主机11000端口建立连接!", "错误提示!") Return End Try End Sub Private Sub BtnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSend.Click If (TextBox2.Text <> "") Then swWriter.WriteLine(TextBox2.Text) If TextBox2.Text = "STOP" Then BtnLink.Enabled = True swWriter.Flush() '刷新当前数据流中的数据 Client_nsStream.Close() swWriter.Close() '清除资源 ( ) Else '刷新当前数据流中的数据 swWriter.Flush() End If TextBox2.Text = "" Else MessageBox.Show("发送信息不能为空!", "错误提示!") End If End Sub End Class