实例介绍
【实例简介】
【实例截图】
【核心代码】
Option Explicit On
Option Strict On
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Threading
Imports System.Net.Sockets
Public Class Form1
Inherits System.Windows.Forms.Form
Private ActiveThreads() As Thread
Private ActiveThreadsState() As Integer
Public Sub New()
MyBase.New()
Form1 = Me
'该调用是 Win 窗体设计器所必需的。
InitializeComponent()
'TODO:在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If Disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(Disposing)
End Sub
#Region " Windows 窗体设计器生成的代码 "
'Windows 窗体设计器所必需
Private components As System.ComponentModel.Container
Private WithEvents lstOutput As System.Windows.Forms.ListBox
Private WithEvents cmdStop As System.Windows.Forms.Button
Private WithEvents Label1 As System.Windows.Forms.Label
Private WithEvents txtClientCount As System.Windows.Forms.TextBox
Private WithEvents cmdStart As System.Windows.Forms.Button
Dim WithEvents Form1 As System.Windows.Forms.Form
'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Me.txtClientCount = New System.Windows.Forms.TextBox()
Me.cmdStop = New System.Windows.Forms.Button()
Me.cmdStart = New System.Windows.Forms.Button()
Me.lstOutput = New System.Windows.Forms.ListBox()
Me.Label1 = New System.Windows.Forms.Label()
'@design Me.TrayHeight = 0
'@design Me.TrayLargeIcon = False
'@design Me.TrayAutoArrange = True
'@design Me.GridSize = New System.Drawing.Size(8, 8)
txtClientCount.Location = New System.Drawing.Point(160, 8)
txtClientCount.Text = "10"
txtClientCount.TabIndex = 2
txtClientCount.Size = New System.Drawing.Size(32, 20)
cmdStop.Location = New System.Drawing.Point(208, 8)
cmdStop.Size = New System.Drawing.Size(64, 24)
cmdStop.TabIndex = 6
cmdStop.Text = "停止"
cmdStart.Location = New System.Drawing.Point(8, 8)
cmdStart.Size = New System.Drawing.Size(64, 24)
cmdStart.TabIndex = 0
cmdStart.Text = "启动"
lstOutput.Location = New System.Drawing.Point(8, 48)
lstOutput.Size = New System.Drawing.Size(328, 225)
lstOutput.TabIndex = 7
Label1.Location = New System.Drawing.Point(80, 10)
Label1.Text = "客户端计数:"
Label1.Size = New System.Drawing.Size(72, 16)
Label1.TabIndex = 5
Label1.TextAlign = ContentAlignment.MiddleRight
Me.Text = "客户端"
Me.StartPosition = System.Windows.Forms.FormStartPosition.Manual
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(344, 285)
Me.Controls.Add(lstOutput)
Me.Controls.Add(cmdStop)
Me.Controls.Add(Label1)
Me.Controls.Add(txtClientCount)
Me.Controls.Add(cmdStart)
End Sub
#End Region
Protected Sub cmdStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdStop.Click
Dim i As Integer
For i = 0 To UBound(ActiveThreads)
ActiveThreads(i).Abort()
ActiveThreads(i) = Nothing
Next
End Sub
Protected Sub cmdStart_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdStart.Click
Dim i As Integer
Dim ClientCount As Integer
Dim ActiveThreadStart As ThreadStart
ClientCount = Int32.Parse(txtClientCount.Text)
ReDim ActiveThreads(ClientCount - 1)
ReDim ActiveThreadsState(ClientCount - 1)
For i = 0 To ClientCount - 1
'创建 ThreadStart 对象以传递 NewThread 的地址
ActiveThreadStart = New ThreadStart(AddressOf StartClient)
'清除任何现有线程(如果多次单击了“开始”按钮)
ActiveThreads(i) = Nothing
'创建 Thread 对象
ActiveThreads(i) = New Thread(ActiveThreadStart)
'启动线程将调用 ThreadStart 委托
ActiveThreads(i).Name = i.ToString
ActiveThreadsState(i) = System.Threading.ThreadState.Running
ActiveThreads(i).Start()
Next i
End Sub
Protected Sub StartClient()
Dim NewThread As Thread
Dim ThreadName As String
Dim Client As TcpClient
Dim Buffer() As Byte
Dim InBuff(100) As Byte
Dim Temp As String
NewThread = System.Threading.Thread.CurrentThread
ThreadName = NewThread.Name
While True
Client = New TcpClient()
Try
Client.Connect("localhost", 9105)
Catch e As Exception
SyncLock NewThread
lstOutput.Items.Insert(lstOutput.Items.Count, "与服务器的连接失败,返回代码是: " & e.Message)
lstOutput.SelectedIndex = lstOutput.Items.Count - 1
End SyncLock
Exit Sub
End Try
Temp = System.DateTime.Now & " 来自客户端 # 的消息" & ThreadName
Buffer = System.Text.Encoding.Default.GetBytes(Temp.ToCharArray)
Client.GetStream().Write(Buffer, 0, Buffer.Length)
While Not Client.GetStream.DataAvailable()
Application.DoEvents()
End While
If Client.GetStream.DataAvailable() Then
Client.GetStream().Read(InBuff, 0, InBuff.Length)
Temp = "客户端 #" & ThreadName & " " & System.Text.Encoding.Default.GetString(InBuff)
SyncLock NewThread
lstOutput.Items.Insert(lstOutput.Items.Count, Temp)
lstOutput.SelectedIndex = lstOutput.Items.Count - 1
End SyncLock
End If
Client.Close()
End While
End Sub
End Class
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论