基本信息
源码名称:VB.NET源码,多线程侦听器
源码大小:0.01M
文件格式:.rar
开发语言:ASP
更新时间:2020-07-29
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 1 元×
微信扫码支付:1 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
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