|
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] 下一页 [C语言系列]NET 中C#的switch语句的语法 [系统软件]托拽Explore中的文件到VB.net的窗口 [系统软件]Boost库在XP+Visual C++.net中的安装 [常用软件]新配色面板:Paint.Net3.0RC1官方下载 [常用软件]用内建的“Net Meeting”聊天 [VB.NET程序]Henry的VB.NET之旅(三)—共享成员 [VB.NET程序]Henry的VB.NET之旅(二)—构造与析构 [VB.NET程序]Henry的VB.NET之旅(一)—失踪的窗体 [VB.NET程序]在托盘上显示Balloon Tooltip(VB.NET) [VB.NET程序]Henry手记-VB.NET中动态加载Treeview节点(二)
|