打印本文 打印本文 关闭窗口 关闭窗口
P2P的简单示例:VB.net版
作者:武汉SEO闵涛  文章来源:敏韬网  点击数5365  更新时间:2009/4/23 19:00:28  文章录入:mintao  责任编辑:mintao
& Chr(10) & Chr(13) & "*********************************" & Chr(10) & Chr(13))
                Case Else
                    If SVInput.Substring(0, 4) = "SEND" Then
                        Dim split() As String = SVInput.Split(" ")
                        If split.Length = 3 Then
                            sendChatMsg(split(1), split(2))
                        Else
                            Console.WriteLine("*********************************" & Chr(10) & Chr(13) & "你输入的命令格式不正确.send命令格式为:send 用户名 你的消息" & Chr(10) & Chr(13) & "*********************************")
                        End If
                    Else
                        Console.WriteLine("*********************************" & Chr(10) & Chr(13) & "笨瓜,你输入的不是有效的命令." & Chr(10) & Chr(13) & "*********************************")
                    End If
            End Select
        End While

    End Sub

    ''''登陆函数
    Private Function Login() As Boolean

        receiveDone = New ManualResetEvent(False)
        Dim userBytes As [Byte]()

        Dim userOK As Boolean = False

        Console.Write("请输入你的用户名:")

        ''''判断用户名是否符合格式
        While (userOK <> True)
            username = Console.ReadLine.ToUpper
            userBytes = Encoding.Unicode.GetBytes(LOGININ & username)

            If userBytes.Length > 24 Or userBytes.Length < 10 Then
                Console.WriteLine("用户名不得小于6个字节,且不得大于20个字节.")
                Console.Write("请重新输入你的用户名:")
            Else
                userOK = True
            End If
        End While

        ''''向服务器发送客户消息
        ClientSocket.SendTo(userBytes, ServerEP)

        Dim data As [Byte]() = New Byte(1024) {}

        Dim comStr As String = Encoding.Unicode.GetString(data, 0, 4)

        ''''异面的接收服务器回送的消息
        Dim DGrecv As New myMethodDelegate(AddressOf recvLogin)
        DGrecv.BeginInvoke(data, Nothing, Nothing)

        ''''等待服务器回送消息的时长为10秒,否则为服务器超时
        receiveDone.WaitOne(30000, True)

        Dim recvStr As String = Encoding.Unicode.GetString(data, 0, 4)

        If recvStr = comStr Then
            Console.WriteLine("服务器超时.登陆失败!!")
            Return False
        End If

        If Encoding.Unicode.GetString(data, 0, 4) = LOGINOK Then
            Console.WriteLine("登陆成功!!")
            Return True
        ElseIf Encoding.Unicode.GetString(data, 0, 4) = HVUSER Then
            Console.WriteLine("用户名重复.登陆失败!!")
            Return False
        Else
            Console.WriteLine("服务器未知错误,登陆失败!!")
            Return False
        End If

    End Function

    ''''登出函数
    Private Sub exitApp()

        Dim loginOutStr As String = LOGINOUT & username
        Dim sendBytes As [Byte]() = Encoding.Unicode.GetBytes(loginOutStr)
        ClientSocket.SendTo(sendBytes, ServerEP)

    End Sub

    ''''请求好友列表的函数
    Private Function getU() As Boolean

        getUDone = New ManualResetEvent(False)
        Dim getUbytes As Byte() = Encoding.Unicode.GetBytes(GETULIST)
        ClientSocket.SendTo(getUbytes, ServerEP)

        Dim data As [Byte]() = New Byte(4056) {}
        Dim comStr As String = Encoding.Unicode.GetString(data, 0, 4)

        Dim GUrecv As New myMethodDelegate(AddressOf recvGetU)
        GUrecv.BeginInvoke(data, Nothing, Nothing)

        getUDone.WaitOne(30000, True)

        Dim recvStr As String = Encoding.Unicode.GetString(data, 0, 4)

        If recvStr = comStr Then
            Console.WriteLine("服务器超时.或取好友名单失败!!")
            Return False
        End If

        If Encoding.Unicode.GetString(data, 0, 4) = GETUSER Then
            getUserList(data, getUrecCount)
            Console.WriteLine("获取在线名单成功!!")
            showUserList()
            Return True
        Else
            Console.WriteLine("服务器未知错误,获取在线名单失败!!")
            Return False
        End If

    End Function

    ''''登陆时用来异步的接收服务器发送的消息
    Sub recvLogin(ByRef inData As Byte())

        ClientSocket.Receive(inData)
        receiveDone.Set()

    End Sub

    ''''请求好友名单时用来异步接收服务器发送的消息
    Sub recvGetU(ByRef inData As Byte())

        getUrecCount = ClientSocket.Receive(inData)
        getUDone.Set()

    End Sub

    ''''处理收到的在线用户信息
    Private Sub getUserList(ByVal userInfobytes() As Byte, ByVal reccount As Integer)

        Dim ustr As String = Encoding.Unicode.GetString(userInfobytes, 4, reccount - 4)

        Dim splitStr() As String = Nothing

        splitStr = Ustr.Split("|")

        Dim IPEPSplit() As String = Nothing

        Dim i As Integer = 0

        Dim k As Integer
        For k = 0 To splitStr.Length - 2 Step

上一页  [1] [2] [3] [4] [5] [6] [7] [8]  下一页

打印本文 打印本文 关闭窗口 关闭窗口