VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > VB.net教程 >
  • 用vb.net开发的简易服务器

在这里介绍程序主要实现的功能以及流程。
程序在启动后会开启1个线程,1个定时器,一个线程用来接受来自客户端的连接,定时器用来检测当前有多少个客户连接,当有一个客户连接是,会开启一个线程用来与客户传送消息。为了简单起见,服务器发送消息为广播发送。

全局变量:

  Dim listen_thread As Thread'监听线程
    Dim listener As Socket '套接字
    Public allDone As New ManualResetEvent(False)
    Dim Thread_listen As Thread

    Dim So As New ArrayList'已连接客户的集合
    Dim thread_lock As Object = New Object()'用于线程锁
    Dim Time As Timers.Time'定时器

   Public Class StateObject
        ' Client  socket.
        Public workSocket As Socket = Nothing
        ' Size of receive buffer.
        Public Const BufferSize As Integer = 1024
        ' Receive buffer.
        Public buffer(BufferSize) As Byte
        ' Received data string.
        Public sb As New StringBuilder
        Public size As Int32
    End Class 'StateObject
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

接受连接的线程过程;

    Public Sub listen_handle()
        TextBox3.AppendText(vbNewLine & "服务器启动")
        TextBox3.ScrollToCaret()
        While 1

            allDone.Reset()
            Try
                listener.BeginAccept(New AsyncCallback(AddressOf Accept_callback), listener)
            Catch e As Exception
                Exit Sub
            End Try
            allDone.WaitOne()
        End While
    End Sub
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

处理连接的线程过程:


 Public Sub Accept_callback(ar As IAsyncResult)
        allDone.Set() '让线程开始

        Dim waitDone As New ManualResetEvent(False)
        Dim socketi As Socket

        Try
            socketi = listener.EndAccept(ar)
        Catch ie As Exception
            Exit Sub
        End Try
        '显示出来
        TextBox3.AppendText(vbNewLine & "加入一个" & socketi.RemoteEndPoint.ToString)
        TextBox3.ScrollToCaret()
        Dim State As New StateObject()
        State.workSocket = socketi
        SyncLock thread_lock '/加入集合
            So.Add(socketi)
        End SyncLock
        State.size = 0
        Dim err As Int32 = 0
        While 1
            Try
                err = socketi.Receive(State.buffer)
            Catch e As Exception'接受异常说明客户端终止
                TextBox3.AppendText(vbNewLine & "客户端终止")
                TextBox3.ScrollToCaret()
                socketi.Close()
                SyncLock thread_lock
                    So.Remove(socketi)
                End SyncLock
                Exit Sub
            End Try
            If err > 0 Then '接收到消息并回调
                State.sb.Clear()
                State.sb.Append(Encoding.ASCII.GetString(State.buffer, 0, err))
                Dim str As String
                str = State.sb.ToString()
                TextBox1.AppendText(vbNewLine & str)
                TextBox1.ScrollToCaret()
            End If
        End While
    End Sub
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

定时器过程:

    Public Sub Time_hande()
        Dim str As String = ""
        SyncLock thread_lock '
            For Each member As Socket In So
                str = str & member.RemoteEndPoint.ToString() & vbNewLine
            Next
        End SyncLock
        TextBox6.Text = str
    End Sub
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

界面: 这里写图片描述

github:https://github.com/fengasdf/WindowsApplication4/blob/master/WindowsApplication4/Form1.vb


相关教程