'Client
Private ActiveThreads() As Thread
Private ActiveThreadsState() As Integer
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
Private ActiveThreads() As Thread
Private ActiveThreadsState() As Integer
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
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 Listener As TcpListener
Private StopListener As Boolean
Private ActiveThreads As Integer Private ThreadIndex As Integer Public Sub New()
MyBase.New() Form1 = Me '该调用是 Win 窗体设计器所必需的。
InitializeComponent() Listener = New TcpListener(9105) Listener.Start() lstStatus.Items.Insert(lstStatus.Items.Count, "侦听器已启动")
Timer1.Enabled = True
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
Private components As System.ComponentModel.IContainer#Region " Windows 窗体设计器生成的代码 " 'Windows 窗体设计器所必需
Private WithEvents Label1 As System.Windows.Forms.Label
Private WithEvents txtMaxThreads As System.Windows.Forms.TextBox
Private WithEvents lstStatus As System.Windows.Forms.ListBox
Private WithEvents Timer1 As System.Windows.Forms.Timer Private WithEvents cmdStopListener 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.lstStatus = New System.Windows.Forms.ListBox()
Me.Label1 = New System.Windows.Forms.Label()
Me.txtMaxThreads = New System.Windows.Forms.TextBox()
Me.cmdStopListener = New System.Windows.Forms.Button()
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.lstStatus.Location = New System.Drawing.Point(6, 42)
Me.lstStatus.Size = New System.Drawing.Size(414, 199)
Me.lstStatus.TabIndex = 4
Me.Label1.Location = New System.Drawing.Point(12, 12)
Me.Label1.Size = New System.Drawing.Size(72, 16)
Me.Label1.TabIndex = 6
Me.Label1.Text = "最大线程数:"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight
Me.Label1.Visible = False
Me.txtMaxThreads.Location = New System.Drawing.Point(90, 12)
Me.txtMaxThreads.Size = New System.Drawing.Size(32, 20)
Me.txtMaxThreads.TabIndex = 5
Me.txtMaxThreads.Text = "5"
Me.txtMaxThreads.Visible = False
Me.cmdStopListener.Location = New System.Drawing.Point(324, 12)
Me.cmdStopListener.Size = New System.Drawing.Size(94, 24)
Me.cmdStopListener.TabIndex = 3
Me.cmdStopListener.Text = "停止侦听器"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(432, 253)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label1, Me.txtMaxThreads, Me.lstStatus, Me.cmdStopListener})
Me.Text = "侦听器" End Sub#End Region Protected Sub ProcessRequest() Dim CurThread As Thread
Dim CurSocket As Socket
Dim Buffer(100) As Byte
Dim Bytes As Integer
Dim Temp As String CurThread = System.Threading.Thread.CurrentThread()
CurSocket = Listener.AcceptSocket While Not StopListener
If CurSocket.Available > 0 Then
Bytes = CurSocket.Receive(Buffer, Buffer.Length, 0)
SyncLock CurThread
lstStatus.Items.Insert(lstStatus.Items.Count, System.Text.Encoding.Default.GetString(Buffer))
lstStatus.SelectedIndex = lstStatus.Items.Count - 1
End SyncLock
Exit While
End If
Application.DoEvents()
If Not CurSocket.Connected Then
StopListener = True
End If
End While '通过暂停两秒钟来模拟由侦听器执行的操作
System.Threading.Thread.CurrentThread().Sleep(2000) '格式化返回消息 - 这通常是
'服务器端处理的结果
Temp = "已收到: " & System.DateTime.Now
Buffer = System.Text.Encoding.Default.GetBytes(Temp.ToCharArray) '通过打开的套接字将结果发送回客户端应用程序,然后
'关闭该套接字
CurSocket.Send(Buffer, Buffer.Length, 0)
CurSocket.Close()
SyncLock CurThread
ActiveThreads -= 1 '处理完毕,线程总数减1
End SyncLock End Sub Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick Dim CurThreadStart As ThreadStart
Dim CurThread As Thread
Dim ThreadCount As Integer
Dim i As Integer If Not Listener.Pending() Then '没有挂起的请求则退出.不用处理.
Exit Sub
End If Timer1.Enabled = False If ActiveThreads > CInt(txtMaxThreads.Text) Then
Timer1.Enabled = True
Exit Sub
End If CurThreadStart = New ThreadStart(AddressOf ProcessRequest)
CurThread = New Thread(CurThreadStart) CurThread.Start()
SyncLock CurThread
ActiveThreads += 1 '新开线程,线程总数加1
End SyncLock Timer1.Enabled = True End Sub Protected Sub cmdStopListener_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdStopListener.Click
StopListener = True
Timer1.Stop()
Listener.Stop()
End Sub
Public Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Form1.Closing
Timer1.Stop()
If Not Listener Is Nothing Then
Listener.Stop()
End If
End Sub
End Class