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

📄 form1.vb

📁 TCP SOCKET 通讯服务器端源头代码
💻 VB
字号:
Imports System.Net
Imports System.Net.Sockets

Imports System.Threading
Imports System.Text
Imports System.Xml.Serialization



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 Button1 As System.Windows.Forms.Button
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents StatusBar1 As System.Windows.Forms.StatusBar
    Friend WithEvents StatusBarPanel1 As System.Windows.Forms.StatusBarPanel
    Friend WithEvents Button3 As System.Windows.Forms.Button
    Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        Me.StatusBar1 = New System.Windows.Forms.StatusBar()
        Me.StatusBarPanel1 = New System.Windows.Forms.StatusBarPanel()
        Me.Button3 = New System.Windows.Forms.Button()
        Me.RichTextBox1 = New System.Windows.Forms.RichTextBox()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        CType(Me.StatusBarPanel1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(32, 80)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(80, 24)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "开始监听"
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(120, 32)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(120, 21)
        Me.TextBox1.TabIndex = 1
        Me.TextBox1.Text = "6688"
        '
        'StatusBar1
        '
        Me.StatusBar1.Location = New System.Drawing.Point(0, 251)
        Me.StatusBar1.Name = "StatusBar1"
        Me.StatusBar1.Panels.AddRange(New System.Windows.Forms.StatusBarPanel() {Me.StatusBarPanel1})
        Me.StatusBar1.ShowPanels = True
        Me.StatusBar1.Size = New System.Drawing.Size(292, 22)
        Me.StatusBar1.TabIndex = 2
        Me.StatusBar1.Text = "StatusBar1"
        '
        'StatusBarPanel1
        '
        Me.StatusBarPanel1.Text = "StatusBarPanel1"
        Me.StatusBarPanel1.Width = 200
        '
        'Button3
        '
        Me.Button3.Location = New System.Drawing.Point(152, 80)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(88, 24)
        Me.Button3.TabIndex = 4
        Me.Button3.Text = "停止监听"
        '
        'RichTextBox1
        '
        Me.RichTextBox1.Location = New System.Drawing.Point(24, 152)
        Me.RichTextBox1.Name = "RichTextBox1"
        Me.RichTextBox1.Size = New System.Drawing.Size(240, 72)
        Me.RichTextBox1.TabIndex = 5
        Me.RichTextBox1.Text = "RichTextBox1"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(40, 32)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(64, 16)
        Me.Label1.TabIndex = 8
        Me.Label1.Text = "监听端口"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(24, 128)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(72, 16)
        Me.Label2.TabIndex = 9
        Me.Label2.Text = "查询记录"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label2, Me.Label1, Me.RichTextBox1, Me.Button3, Me.StatusBar1, Me.TextBox1, Me.Button1})
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.StatusBarPanel1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Dim Listener As TcpListener
    Dim port As Integer
    Dim sock As Socket
    Dim Cnn As New ADODB.Connection()
    


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Cnn.Provider = "Microsoft.Jet.OLEDB.4.0"
        Cnn.Open(Application.StartupPath & "\奖金数据库.mdb")
        RichTextBox1.Text = ""
    End Sub


    '开始监听
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

      

        Try
            port = TextBox1.Text

            Try
                Listener = New TcpListener(port)
                Listener.Start()
                StatusBarPanel1.Text = "开始监听....."

                Dim Thread As New Thread(AddressOf target)
                Thread.Start()

            Catch ee As Exception

                MsgBox(ee.Message)

            End Try

        Catch
            MsgBox("您输入的格式不对,请输入正确的格式")
        End Try

    End Sub


    Sub target()

        Dim Reco As New ADODB.Recordset()
        Dim Tname As String
       
        Tname = "奖金"
        Reco.Open(Tname, Cnn, 3, 3)

        Dim str As String
        Dim msg As Byte()
        'Dim str As String = TextBox2.Text + Chr(13) + Chr(10)

        
        sock = Listener.AcceptSocket
        If sock.Connected Then
            StatusBarPanel1.Text = "已与客户建立了连接"

            While True
                Dim bb(64) As Byte
                Try
                    '把数据从连接的Soket接收到接收缓冲区的特定位置
                    Dim i = sock.Receive(bb, bb.Length, 0)

                    Dim ss As String = Encoding.UTF8.GetString(bb)


                    '使用UTF8的原因是因为要传送中文,帮助是用的ASCII,无法传送中文
                    'Dim ss As String = Encoding.ASCII.GetString(bb)

                    RichTextBox1.AppendText(ss)

                    If ss <> "" Then

                        Dim Msql As String = ss

                        ss = ""
                        Try
                            Try
                                Reco.Close()
                            Catch
                            End Try
                            Tname = "select * from 奖金 where " & Msql
                            Reco.Open(Tname, Cnn, 3, 3)

                            Reco.MoveFirst()
                            While Not Reco.EOF

                                str = ""

                                If VarType(Reco.Fields(0).Value) > 1 Then
                                    str = str + CStr(Reco.Fields(0).Value) + "   "
                                Else
                                    str = str + "   "
                                End If

                                If VarType(Reco.Fields(2).Value) > 1 Then
                                    str = str + CStr(Reco.Fields(2).Value) + "   "
                                Else
                                    str = str + "   "
                                End If

                                If VarType(Reco.Fields(3).Value) > 1 Then
                                    str = str + CStr(Reco.Fields(3).Value) + "   "
                                Else
                                    str = str + "   "
                                End If

                                If VarType(Reco.Fields(4).Value) > 1 Then
                                    str = str + CStr(Reco.Fields(4).Value) + Chr(13) + Chr(10)
                                Else
                                    str = str + Chr(13) + Chr(10)
                                End If

                                msg = Encoding.UTF8.GetBytes(str)
                                sock.Send(msg, msg.Length, 0)

                                Reco.MoveNext()

                            End While
                        Catch
                            msg = Encoding.UTF8.GetBytes("条件错误")
                            sock.Send(msg, msg.Length, 0)
                        End Try
                    End If

                Catch
                End Try

            End While
        End If

    End Sub


    '停止监听
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Try
            Listener.Stop()
            StatusBarPanel1.Text = "停止监听....."

        Catch

            MsgBox("监听还未开始。关闭无效")

        End Try

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub


End Class


⌨️ 快捷键说明

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