P2P的简单示例(VB.net版) 一__教程 |
|
日期:2007-5-20 0:37:34 人气:71 [大 中 小] |
|
|
|
End Sub
'打印在线用户 Sub showUser() Dim hava As Boolean = False If userName.Length <> 0 Then Dim i As Integer For i = 1 To userName.Length - 1 If userName(i) <> "" Then hava = True Exit For End If Next If hava = False Then Console.WriteLine("*********************************" & Chr(10) & Chr(13) & "当前没有用户在线" & Chr(10) & Chr(13) & "*********************************") Exit Sub End If Console.WriteLine("*********************************") For i = 1 To userName.Length - 1 If userName(i) <> "" Then Console.WriteLine("用户名:" & userName(i) & " 地址:" & userIPEP(i).ToString) End If Next Console.WriteLine("*********************************") Else Console.WriteLine("*********************************" & Chr(10) & Chr(13) & "当前没有用户在线" & Chr(10) & Chr(13) & "*********************************") End If 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 = ServerSocket.ReceiveFrom(data, tempRemoteEP)
'Console.WriteLine(Encoding.Unicode.GetString(data))
Dim msgHead As String = Encoding.Unicode.GetString(data, 0, 4) Select Case msgHead Case LOGININ Dim LoginThing As String = userLogin(data, tempRemoteEP, recv) If LoginThing = HVUSER Then sendMsg(HVUSER, tempRemoteEP) ElseIf LoginThing = LOGINOK Then sendMsg(LOGINOK, tempRemoteEP)
End If
Case LOGINOUT userloginout(data, recv)
Case GETULIST Dim userinfo As String = getUserList() sendMsg(userinfo, tempRemoteEP)
Case P2PCONN questP2PConn(data, recv)
Case HOLDLINE holdOnLine(data, recv) End Select
Catch e As Exception 'Console.WriteLine(e.ToString) End Try End While
End Sub
'转发P2P连接请求 Private Sub questP2PConn(ByVal data() As Byte, ByVal recv As Integer)
Dim recvStr As String = Encoding.Unicode.GetString(data, 4, recv - 4) Dim split() As String = recvStr.Split("|")
Dim fromEP As IPEndPoint Dim toEP As IPEndPoint Dim i As Integer For i = 1 To userName.Length - 1 If userName(i) = split(0) Then fromEP = userIPEP(i) End If If userName(i) = split(1) Then toEP = userIPEP(i) End If Next Dim holdbytes() As Byte = Encoding.Unicode.GetBytes(MAKHOLD & fromEP.ToString) ServerSocket.SendTo(holdbytes, toEP) End Sub
'函数.返回所有在线用户.其格式:用户名+|+用户IPEP+| Private Function getUserList() As String Dim userInfo As String = GETUSER Dim i As Integer For i = 1 To userName.Length - 1 If userName(i) <> "" Then userInfo += userName(i) & "|" & userIPEP(i).ToString & "|" End If Next Return userInfo End Function
'用户登陆,直接返回登陆是否成功的值 Private Function userLogin(ByVal data As Byte(), ByVal userEP As IPEndPoint, ByVal recvCount As Integer) As String |
|
出处:本站原创 作者:佚名 |
|
|