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

📄 form1.vb

📁 编程之道VB.NETt程序设计入门-589M.zip
💻 VB
字号:
Imports System.IO
Imports System.Threading
Imports System.Net
Imports System.Net.Sockets

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加任何初始化

    End Sub

    '窗体重写处置以清理组件列表。
    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

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意:以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents btnStart As System.Windows.Forms.Button
    Friend WithEvents lstClient As System.Windows.Forms.ListBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.btnStart = New System.Windows.Forms.Button()
        Me.lstClient = New System.Windows.Forms.ListBox()
        Me.SuspendLayout()
        '
        'btnStart
        '
        Me.btnStart.Location = New System.Drawing.Point(128, 232)
        Me.btnStart.Name = "btnStart"
        Me.btnStart.Size = New System.Drawing.Size(56, 24)
        Me.btnStart.TabIndex = 1
        Me.btnStart.Text = "启动"
        '
        'lstClient
        '
        Me.lstClient.ItemHeight = 12
        Me.lstClient.Name = "lstClient"
        Me.lstClient.Size = New System.Drawing.Size(296, 220)
        Me.lstClient.TabIndex = 3
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lstClient, Me.btnStart})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Dim serverThread As Thread
    Dim tcpListener As tcpListener
    Dim netStream As NetworkStream
    Dim rdStream As StreamReader
    Dim wtStream As StreamWriter
    Dim clientSocket As Socket


    Private Sub Listen()
        Dim clientMsg As String
        tcpListener = New TcpListener(1111)
        Try
            tcpListener.Start()
            clientSocket = tcpListener.AcceptSocket()

            If clientSocket.Connected Then
                lstClient.Items.Add("new user")
                netStream = New NetworkStream(clientSocket)
                rdStream = New StreamReader(netStream)
                wtStream = New StreamWriter(netStream)
            End If

            While (True)
                clientMsg = rdStream.ReadLine()
                If clientMsg = "Exit" Then
                    Exit Sub
                End If
                lstClient.Items.Add(clientMsg)
            End While
        Catch ex As SocketException
            MessageBox.Show("无法打开端口")
        Catch ex As IOException
            '流被关闭
        Catch ex As ThreadAbortException
            '线程正常中止
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub EndListen()
        If (Not netStream Is Nothing) Then
            rdStream.Close()
            wtStream.Close()
            netStream.Close()
            clientSocket.Shutdown(SocketShutdown.Both)
            clientSocket.Close()
        End If

        If (Not serverThread Is Nothing) Then
            serverThread.Abort()
            tcpListener.Stop()
        End If
    End Sub

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        Try
            serverThread = New Thread(AddressOf Listen)
            serverThread.Start()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        EndListen()
    End Sub
End Class

⌨️ 快捷键说明

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