⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listener.vb

📁 功能源码多线程 TCIIP 侦听器,vb.net,可供学习和交流
💻 VB
字号:
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
        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
        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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -