打印本文 打印本文 关闭窗口 关闭窗口
P2P的简单示例:VB.net版
作者:武汉SEO闵涛  文章来源:敏韬网  点击数5363  更新时间:2009/4/23 19:00:28  文章录入:mintao  责任编辑:mintao
2
            ReDim Preserve OLUserName(i)
            ReDim Preserve OLUserEP(i)

            OLUserName(i) = splitStr(k)
            IPEPSplit = splitStr(k + 1).Split(":")
            OLUserEP(i) = New IPEndPoint(IPAddress.Parse(IPEPSplit(0)), IPEPSplit(1))

            IPEPSplit = Nothing
            i += 1
        Next

    End Sub

    ''''显示在线用户
    Private Sub showUserList()
        Dim i As Integer
        For i = 0 To OLUserName.Length - 1
            If OLUserName(i) <> "" Then
                Console.WriteLine("用户名:" & OLUserName(i) & " 用户IP:" & OLUserEP(i).ToString)
            End If
        Next
    End Sub

    ''''客户程序监听的函数
    Sub listen()

        While True

            Try
                Dim recv As Integer = 0 ''''收到的字节数
                Dim data As [Byte]() = New Byte(1024) {}  ''''缓冲区大小
                Dim sender As New IPEndPoint(IPAddress.Any, 0)
                Dim tempRemoteEP As EndPoint = CType(sender, EndPoint)
                recv = ClientSocket.ReceiveFrom(data, tempRemoteEP)

                Dim msgHead As String = Encoding.Unicode.GetString(data, 0, 4) ''''获得消息头的内容
                Select Case msgHead
                    Case MSGEND
                        msgSendEnd = True
                        sendDone.Set()
                    Case LOGININ
                        addOnLine(data, recv)
                    Case LOGINOUT
                        removeOnLine(data, recv)
                    Case MSGEND
                        msgSendEnd = True
                        sendDone.Set()
                    Case MAKHOLD
                        Console.WriteLine(Chr(10) & Chr(13) & "收到打洞消息.")
                        makeHold(data, recv)
                        Console.Write("Client>")
                    Case CHATMSG
                        showChatMsg(data, recv)
                    Case HOLDOK
                        testHold = True
                        holdDone.Set()
                    Case CHTMSGEND
                        testChat = True
                        chatDone.Set()
                End Select

            Catch
            End Try

        End While
    End Sub

    ''''发送聊天消息
    Private Sub sendChatMsg(ByVal remoteUser As String, ByVal chatMsgStr As String)

        If remoteUser = username Then
            Console.WriteLine("猪头,你想干什么!!!")
            Exit Sub
        End If

        Dim i As Integer

        Dim remoteUEP As IPEndPoint
        For i = 0 To OLUserName.Length - 1
            If remoteUser = OLUserName(i) Then
                remoteUEP = OLUserEP(i)
                Exit For
            End If
            If i = OLUserName.Length - 1 Then
                Console.WriteLine("找不到你想发送的用户.")
                Exit Sub
            End If
        Next

        Dim msgbytes() As Byte = Encoding.Unicode.GetBytes(CHATMSG & username & "|" & chatMsgStr)
        Dim holdbytes() As Byte = Encoding.Unicode.GetBytes(P2PCONN & username & "|" & remoteUser)

        chatDone = New ManualResetEvent(False)
        ClientSocket.SendTo(msgbytes, remoteUEP)
        chatDone.WaitOne(10000, True)
        If testChat = True Then
            testChat = False
            Exit Sub
        End If

        testHold = False
        While testHold <> True
            Console.WriteLine("打洞ing.....")
            holdDone = New ManualResetEvent(False)
            ClientSocket.SendTo(holdbytes, remoteUEP)
            ClientSocket.SendTo(holdbytes, ServerEP)
            holdDone.WaitOne(10000, True)
            If testHold = True Then
                Exit While
            Else
                Cons

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

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