VB.NET端口监听实例

Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
Imports System.Threading
Imports System.Net.Sockets

Public Class Form1
    Inherits System.WinForms.Form
   
    Private oListener As TCPListener
    Private bStopListener As Boolean
    Private ActiveThreads As Integer
    Private ThreadIndex As Integer
   
    Public Sub New()
        MyBase.New()
        Form1 = Me
        'This call is required by the Win Form Designer.
        InitializeComponent()
        oListener = New TCPListener(9105)
        oListener.Start()
        lstStatus.InsertItem(lstStatus.Items.Count, "Listener Started")
        Timer1.Enabled = True
    End Sub
    'Form overrides dispose to clean up the component list.
    Public Overrides Sub Dispose()
        MyBase.Dispose()
        components.Dispose()
    End Sub
   
#Region " Windows Form Designer generated code "
   
    'Required by the Windows Form Designer
    Private components As System.ComponentModel.Container
    Private WithEvents Label1 As System.WinForms.Label
    Private WithEvents txtMaxThreads As System.WinForms.TextBox
    Private WithEvents lstStatus As System.WinForms.ListBox
    Private WithEvents Timer1 As System.WinForms.Timer
   
    Private WithEvents cmdStopListener As System.WinForms.Button
   
   
   
   
    Dim WithEvents Form1 As System.WinForms.Form
   
    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer. 
    'Do not modify it using the code editor.
    Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container()
        Me.lstStatus = New System.WinForms.ListBox()
        Me.Timer1 = New System.WinForms.Timer(components)
        Me.Label1 = New System.WinForms.Label()
        Me.cmdStopListener = New System.WinForms.Button()
        Me.txtMaxThreads = New System.WinForms.TextBox()
       
        '@design Me.TrayHeight = 90
        '@design Me.TrayLargeIcon = False
        '@design Me.TrayAutoArrange = True
        lstStatus.Location = New System.Drawing.Point(6, 42)
        lstStatus.Size = New System.Drawing.Size(414, 199)
        lstStatus.TabIndex = 4
       
        '@design Timer1.SetLocation(New System.Drawing.Point(7, 7))
       
        Label1.Location = New System.Drawing.Point(12, 12)
        Label1.Text = "Max Threads:"
        Label1.Size = New System.Drawing.Size(72, 16)
        Label1.TabIndex = 6
        Label1.TextAlign = System.WinForms.HorizontalAlignment.Right
        Label1.Visible = False
       
        cmdStopListener.Location = New System.Drawing.Point(324, 12)
        cmdStopListener.Size = New System.Drawing.Size(94, 24)
        cmdStopListener.TabIndex = 3
        cmdStopListener.Text = "Stop Listener"
       
        txtMaxThreads.Location = New System.Drawing.Point(90, 12)
        txtMaxThreads.Text = "5"
        txtMaxThreads.TabIndex = 5
        txtMaxThreads.Size = New System.Drawing.Size(32, 20)
        txtMaxThreads.Visible = False
        Me.Text = "Listener"
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(432, 253)
       
        Me.Controls.Add(Label1)
        Me.Controls.Add(txtMaxThreads)
        Me.Controls.Add(lstStatus)
        Me.Controls.Add(cmdStopListener)
    End Sub
   
#End Region
    Protected Sub ProcessRequest()
        Dim oThread As Thread
        Dim oSocket As Socket
        Dim Buffer(100) As Byte
        Dim bytes As Integer
        Dim Temp As String
        oThread = System.Threading.Thread.CurrentThread()
        oSocket = olistener.Accept
        While Not bStopListener
            If oSocket.Available > 0 Then
                bytes = oSocket.Receive(Buffer, Buffer.Length, 0)
                SyncLock oThread
                    lstStatus.InsertItem(lstStatus.Items.Count, System.Text.Encoding.ASCII.GetString(Buffer))
                    lstStatus.SelectedIndex = lstStatus.Items.Count - 1
                End SyncLock
                Exit While
            End If
            Application.DoEvents()
            If Not oSocket.Connected Then
                bStopListener = True
            End If
        End While
        'Simulate work being performed by listener by pausing for 2 seconds
        System.Threading.Thread.CurrentThread().Sleep(2000)
        'Format the return message - this will normally be the results of the
        'server side processing
        Temp = "Recieved: " & system.Date.Now
        Buffer = System.Text.Encoding.ASCII.GetBytes(Temp.ToCharArray)
        'Send the results back to the client application via the open socket and
        'close the socket
        oSocket.Send(Buffer, Buffer.Length, 0)
        oSocket.Close()
        SyncLock oThread
            ActiveThreads -= 1
        End SyncLock
    End Sub
   
    Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim oThreadStart As ThreadStart
        Dim oThread As Thread
        Dim ThreadCount As Integer
        Dim i As Integer
        If Not oListener.Pending() Then
            Exit Sub
        End If
        Timer1.Enabled = False
        If ActiveThreads > CInt(txtMaxThreads.Text) Then
            Timer1.Enabled = True
            Exit Sub
        End If
        oThreadStart = New ThreadStart(AddressOf ProcessRequest)
        oThread = New Thread(oThreadStart)
        oThread.Start()
        SyncLock oThread
            ActiveThreads += 1
        End SyncLock
        Timer1.Enabled = True
    End Sub
   
    Protected Sub cmdStopListener_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        bStopListener = True
        oListener.Stop()
    End Sub
   
    Public Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Form1.Closing
        If Not oListener Is Nothing Then
            oListener.Stop()
        End If
    End Sub
End Class
淘软网(http://www.taoruan.net/)是一个专业的软件、源码交易平台,不论你是企业或个人都可以发布、买卖产品.
为什么选择淘软?