知识大全 P2P的简单示例:VB.net版

Posted 消息

篇首语:人生何适不艰难,赖是胸中万斛宽。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 P2P的简单示例:VB.net版相关的知识,希望对你有一定的参考价值。

P2P的简单示例:VB.net版  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

这是用实现的一个简单的P P示例 利用了UDP打洞技术 分服务器端跟客户端 服务器端负责登陆记录用户的IP和端口及转发打洞消息 (相关技术在CSDN搜一下 有很多的 ) 原理到处都有 这里就没有贴出来 这里贴出了的代码 供初学者交流 也欢迎高手点评       服务器端在启动成功后 输入help可以查看到服务器相关命令       客户端在登陆成功后 输入help可以查看客户端相关命令 (登陆时用户名随便 )      以下是服务器端:      Imports System Net   Imports System Net Sockets   Imports System Text   Imports System Threading   Imports System Collections      Module myUDPServer      #Region 全局变量       Dim ServerSocket As New Socket(AddressFamily InterNeork SocketType Dgram ProtocolType Udp)    Dim ipep As IPEndPoint = New IPEndPoint(IPAddress Any )       Dim htUserList As New Hashtable 用来保存在线用户和用户的 IP和端口       Dim userName( ) As String    Dim userIPEP( ) As IPEndPoint    Dim userTime( ) As Integer       Dim timerDelegate As New TimerCallback(AddressOf onLineTimeOut)      #End Region      #Region 参数       以下是客户端到服务器端的消息开头    Const LOGININ As String = 请求登陆的消息|||消息形式: +自己的用户名    Const LOGINOUT As String = 请求登出的消息|||消息形式: +自己的用户名    Const GETULIST As String = 请求获得在线用户列表|||消息形式:    Const P PCONN As String = 请求P P连接的消息|||消息形式: +自己的用户名+|+对方的用户名    Const HOLDLINE As String = 保持连接 |||消息开式: +自己的用户名       以下是服务器到客户端的消息开头    Const HVUSER As String = 用户名已存在    Const GETUSER As String = 在线用户列表|||消息格式: +用户名+EP    Const MAKHOLD As String = 打洞命令|||消息格式: +IP    Const LOGINOK As String = 登陆成功    Const SERVCLS As String = 服务器关闭    Const MSGEND As String = 消息结束       以下是服务器端的命名    Const EXITPRO As String = EXIT 退出命令    Const SHOWULIST As String = SHOWUSER 显示在线用户    Const HELP As String = HELP 显示帮助      #End Region      #Region 方法       主函数 程序入口    Sub Main()       获得服务器的IP地址    Dim addressList As System Net IPAddress() = Dns GetHostByName(Dns GetHostName()) AddressList    Dim ServerIP As IPAddress = addressList( )       ServerSocket Bind(ipep)    Console WriteLine( 服务器正在启动 )    Console WriteLine( 服务器IP: & ServerIP ToString & 正在监听 & ipep Port ToString & 端口 )    Dim listenTH As New Thread(AddressOf listen)    listenTH Start() 启用监听的线程    Console WriteLine( 服务器启动成功 )       Dim timer As New Timer(timerDelegate Nothing )       Dim SVInput As String    While True    Console Write( Server> )    SVInput = Console ReadLine() ToUpper    Select Case SVInput    Case EXITPRO    listenTH Abort()    ServerSocket Close()    Exit Sub    Case SHOWULIST    showUser()    Case HELP    Console Write( ********************************* & Chr( ) & Chr( ) & exit:输出当前程序 & Chr( ) & Chr( ) & showuser:显示当前在线用户例表 & Chr( ) & Chr( ) & help:显示帮助 & Chr( ) & Chr( ) & ********************************* & Chr( ) & Chr( ))    Case Else    Console WriteLine( ********************************* & Chr( ) & Chr( ) & 笨瓜 你输入的不是有效的命令 & Chr( ) & Chr( ) & ********************************* )    End Select    End While          End Sub       打印在线用户    Sub showUser()    Dim hava As Boolean = False    If userName Length <> Then    Dim i As Integer    For i = To userName Length    If userName(i) <> Then    hava = True    Exit For    End If    Next    If hava = False Then    Console WriteLine( ********************************* & Chr( ) & Chr( ) & 当前没有用户在线 & Chr( ) & Chr( ) & ********************************* )    Exit Sub    End If    Console WriteLine( ********************************* )    For i = To userName Length    If userName(i) <> Then    Console WriteLine( 用户名: & userName(i) & 地址: & userIPEP(i) ToString)    End If    Next    Console WriteLine( ********************************* )    Else    Console WriteLine( ********************************* & Chr( ) & Chr( ) & 当前没有用户在线 & Chr( ) & Chr( ) & ********************************* )    End If    End Sub       服务器监听函数    Sub listen()       While True       Try    Dim recv As Integer =    Dim data As [Byte]() = New Byte( )    Dim sender As New IPEndPoint(IPAddress Any )    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 )    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 P PCONN    questP PConn(data recv)       Case HOLDLINE    holdOnLine(data recv)    End Select       Catch e As Exception    Console WriteLine(e ToString)    End Try    End While       End Sub       转发P P连接请求    Private Sub questP PConn(ByVal data() As Byte ByVal recv As Integer)       Dim recvStr As String = Encoding Unicode GetString(data recv )    Dim split() As String = recvStr Split( | )       Dim fromEP As IPEndPoint    Dim toEP As IPEndPoint    Dim i As Integer    For i = To userName Length    If userName(i) = split( ) Then    fromEP = userIPEP(i)    End If    If userName(i) = split( ) 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 = To userName Length    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       Dim Uname As String = Encoding Unicode GetString(data recvCount )       Dim Uinfobytes() As Byte       Dim i As Integer    Dim j As Integer       For i = To userName Length    If Uname = userName(i) Then    Return HVUSER    End If    Next       For i = To userName Length    If userName(i) = Then    userName(i) = Uname    userIPEP(i) = userEP    userTime(i) =    Console Write(Chr( ) & Chr( ) & ********************************* & Chr( ) & Chr( ) & Uname Trim & 上线了 & 用户地址: & userEP ToString & Chr( ) & Chr( ) & ********************************* & Chr( ) & Chr( ))    Console Write( Server> )       Uinfobytes = Encoding Unicode GetBytes(LOGININ & userName(i) & | & userIPEP(i) ToString)       For j = To userName Length    If userName(j) <> And userName(j) <> Uname Then    ServerSocket SendTo(Uinfobytes userIPEP(j))    End If    Next    Return LOGINOK    End If    Next       Dim userCount As Integer = userName Length       ReDim Preserve userName(userCount)    ReDim Preserve userIPEP(userCount)    ReDim Preserve userTime(userCount)       userName(userName Length ) = Uname    userIPEP(userIPEP Length ) = userEP    userTime(userTime Length ) =       Console Write(Chr( ) & Chr( ) & ********************************* & Chr( ) & Chr( ) & Uname Trim & 上线了 & 用户地址: & userEP ToString & Chr( ) & Chr( ) & ********************************* & Chr( ) & Chr( ))    Console Write( Server> )       Uinfobytes = Encoding Unicode GetBytes(LOGININ & userName(userName Length ) & | & userIPEP(userName Length ) ToString)       For j = To userName Length    If userName(j) <> And userName(j) <> Uname Then    ServerSocket SendTo(Uinfobytes userIPEP(j))    End If    Next    Return LOGINOK       End Function       用户登出    Private Sub userloginout(ByVal data As Byte() ByVal recvCount As Integer)       Dim i As Integer    Dim Uname As String = Encoding Unicode GetString(data recvCount )       For i = To userName Length       If Uname = userName(i) Then       Dim loginOutMsg As String = LOGINOUT & userName(i)          userName(i) =    userIPEP(i) = Nothing    userTime(i) =       Dim j As Integer    For j = To userName Length    If userName(j) <> Then       sendMsg(loginOutMsg userIPEP(j))       End If    Next       Console WriteLine(Chr( ) & Chr( ) & ********************************* )    Console WriteLine( 用户 & Uname & 下线了 )    Console WriteLine( ********************************* )    Console Write( Server> )       Exit For       End If       Next       End Sub       保持用户在线的过程    Private Sub holdOnLine(ByVal data As Byte() ByVal recvCount As Integer)       Dim Uname As String = Encoding Unicode GetString(data recvCount )       Dim i As Integer       For i = To userName Length       If Uname = userName(i) Then       userTime(i) =    Exit For       End If       Next       End Sub       用户超时退出    Private Sub onLineTimeOut(ByVal state As [Object])       Dim i As Integer       For i = To userName Length       If userTime(i) > Then       userTime(i) =       If userTime(i) <= Then       Dim loginoutmsg As String = LOGINOUT & userName(i)       Console WriteLine(Chr( ) & Chr( ) & ********************************* )    Console WriteLine( 用户 & userName(i) & 下线了 )    Console WriteLine( ********************************* )    Console Write( Server> )       userName(i) =    userIPEP(i) = Nothing       Dim ULoginOutbytes() As Byte = Encoding Unicode GetBytes(loginoutmsg)       Dim j As Integer    For j = To userName Length       If userName(j) <> Then    If userIPEP(j) Is Nothing Then    Else    ServerSocket SendTo(ULoginOutbytes userIPEP(j))    End If    End If       Next       End If       End If       Next       End Sub       发送消息的函数    Sub sendMsg(ByVal msg As String ByVal remoteEP As IPEndPoint)    Dim sendBytes As [Byte]() = Encoding Unicode GetBytes(msg)    Try       ServerSocket SendTo(sendBytes remoteEP)       Catch e As Exception    Console WriteLine(e ToString())    End Try    End Sub      #End Region      End Module      以下是客户端:      Imports System Net   Imports System Net Sockets   Imports System Text   Imports System Threading         Module Module      #Region 参数       以下是客户端到服务器端的消息开头    Const LOGININ As String = 请求登陆的消息|||消息形式: +自己的用户名    Const LOGINOUT As String = 请求登出的消息|||消息形式: +自己的用户名    Const GETULIST As String = 请求获得在线用户列表|||消息形式: +自己的用户名    Const P PCONN As String = 请求P P连接的消息|||消息形式: +自己的用户名+对方的用户名    Const HOLDLINE As String = 保持连接 |||消息开式: +自己的用户名       以下是服务器到客户端的消息开头    Const HVUSER As String = 用户名已存在    Const GETUSER As String = 在线用户列表|||消息格式: +用户名+EP    Const MAKHOLD As String = 打洞命令|||消息格式: +IP    Const LOGINOK As String = 登陆成功    Const SERVCLS As String = 服务器关闭    Const MSGEND As String = 消息结束       以下是客户端到客户端的消息开头    Const HOLDOK As String = 打洞成功    Const CHATMSG As String = 聊天消息    Const CHTMSGEND As String = 聊天消息发送成功       以下是客户端的命名    Const EXITPRO As String = EXIT 退出命令    Const SHOWULIST As String = SHOWUSER 显示在线用户    Const HELP As String = HELP 显示帮助    Const SEND As String = SEND 发送消息      #End Region      #Region 全局全量       Delegate Sub myMethodDelegate(ByRef myInData As Byte()) 登陆时用的事件       Dim MaxTry As Integer =    Dim msgSendEnd As Boolean = False 消息是否发送成功 若发送成功 则会返回结束消息    Dim ThListen As New Thread(AddressOf listen) 监听的线程    Dim ClientSocket As New Socket(AddressFamily InterNeork SocketType Dgram ProtocolType Udp) 客户端套节字的定义    Dim username As String 当前用户名    Dim ServerEP As IPEndPoint 服务器的IPEP    Dim holdBytes As [Byte]() = Encoding Unicode GetBytes(HOLDLINE & username) 和服务器保持连接连接时用到的byte数组    Dim OLUserName() As String    Dim OLUserEP() As IPEndPoint    Dim getUrecCount As Integer    Dim testHold As Boolean = False    Dim testChat As Boolean = False       Private receiveDone As ManualResetEvent 在登陆时用来阻塞线程 等待收到数据    Private sendDone As ManualResetEvent 用来阴塞发送消息的线程 等待收到回送的确认消息    Private getUDone As ManualResetEvent 用来阻塞请求好友名单的线程 等待接收好友名单    Private holdDone As ManualResetEvent 用来阻塞打洞时的线程    Private chatDone As ManualResetEvent 用来阻塞发送聊天消息时的线程       Dim timerDelegate As New TimerCallback(AddressOf holdonline) 为保持在线状态弄得      #End Region      #Region 方法       主函数 程序入口    Sub Main()    Dim InputIP As String    Dim InputOK As Boolean = False          判断输入的IP 并且保存服务器的IPEP    While InputOK <> True    Console Write( 请输入服务器IP: )    InputIP = Console ReadLine()    Try    ServerEP = New IPEndPoint(IPAddress Parse(InputIP) )    InputOK = True    Catch    Console WriteLine( 你输入的服务器IP不正确 请重新输入 )    InputOK = False    End Try    End While       Dim bool As Boolean = False       判断用户是否登陆成功    While bool <> True       Dim LoginOK As Boolean = Login()    If LoginOK = True Then    bool = True    Else    Console Write( 是否重试:输入Y重试 输入任意值退出程序: )    Dim tempYN As String = Console ReadLine ToUpper    If tempYN = Y Then    bool = False    Else    Exit Sub    End If    End If       End While       Console WriteLine( 用户名: & username)    holdBytes = Encoding Unicode GetBytes(HOLDLINE & username)    登陆成功后 用一个timer 每隔 秒向服务器发送消息 保持在线状态跟在主机注册的端口    Dim timer As New Timer(timerDelegate Nothing )       请求在线名单    Console WriteLine( 正在获取在线名单 请稍后 )    Dim getUbool As Boolean = False    While getUbool <> True    getUbool = getU()    If getUbool = False Then    Console Write( 是否重试:输入Y重试 输入任意值退出程序: )    Dim tempYN As String = Console ReadLine ToUpper    If tempYN = Y Then    bool = False    Else    Exit Sub    End If    End If    End While       ThListen Start()       用来处理客户端的一些命令    Dim SVInput As String    While True    Console Write( Client> )    SVInput = Console ReadLine() ToUpper    Select Case SVInput    Case EXITPRO    exitApp()    ThListen Abort()    ClientSocket Close()    Exit Sub    Case SHOWULIST    Console WriteLine( ********************************* )    showUserList()    Console WriteLine( ********************************* )    Case HELP    Console Write( ********************************* & Chr( ) & Chr( ) & exit:输出当前程序 & Chr( ) & Chr( ) & showuser:显示当前在线用户例表 & Chr( ) & Chr( ) & send:发送消息 格式:send 用户名 消息 & Chr( ) & Chr( ) & help:显示帮助 & Chr( ) & Chr( ) & ********************************* & Chr( ) & Chr( ))    Case Else    If SVInput Substring( ) = SEND Then    Dim split() As String = SVInput Split( )    If split Length = Then    sendChatMsg(split( ) split( ))    Else    Console WriteLine( ********************************* & Chr( ) & Chr( ) & 你输入的命令格式不正确 send命令格式为:send 用户名 你的消息 & Chr( ) & Chr( ) & ********************************* )    End If    Else    Console WriteLine( ********************************* & Chr( ) & Chr( ) & 笨瓜 你输入的不是有效的命令 & Chr( ) & Chr( ) & ********************************* )    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 > Or userBytes Length < Then    Console WriteLine( 用户名不得小于 个字节 且不得大于 个字节 )    Console Write( 请重新输入你的用户名: )    Else    userOK = True    End If    End While       向服务器发送客户消息    ClientSocket SendTo(userBytes ServerEP)       Dim data As [Byte]() = New Byte( )       Dim Str As String = Encoding Unicode GetString(data )       异面的接收服务器回送的消息    Dim DGrecv As New myMethodDelegate(AddressOf recvLogin)    DGrecv BeginInvoke(data Nothing Nothing)       等待服务器回送消息的时长为 秒 否则为服务器超时    receiveDone WaitOne( True)       Dim recvStr As String = Encoding Unicode GetString(data )       If recvStr = Str Then    Console WriteLine( 服务器超时 登陆失败!! )    Return False    End If       If Encoding Unicode GetString(data ) = LOGINOK Then    Console WriteLine( 登陆成功!! )    Return True    ElseIf Encoding Unicode GetString(data ) = 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( )    Dim Str As String = Encoding Unicode GetString(data )       Dim GUrecv As New myMethodDelegate(AddressOf recvGetU)    GUrecv BeginInvoke(data Nothing Nothing)       getUDone WaitOne( True)       Dim recvStr As String = Encoding Unicode GetString(data )       If recvStr = Str Then    Console WriteLine( 服务器超时 或取好友名单失败!! )    Return False    End If       If Encoding Unicode GetString(data ) = 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 reccount )       Dim splitStr() As String = Nothing       splitStr = Ustr Split( | )       Dim IPEPSplit() As String = Nothing       Dim i As Integer =       Dim k As Integer    For k = To splitStr Length Step    ReDim Preserve OLUserName(i)    ReDim Preserve OLUserEP(i)       OLUserName(i) = splitStr(k)    IPEPSplit = splitStr(k + ) Split( : )    OLUserEP(i) = New IPEndPoint(IPAddress Parse(IPEPSplit( )) IPEPSplit( ))       IPEPSplit = Nothing    i +=    Next       End Sub       显示在线用户    Private Sub showUserList()    Dim i As Integer    For i = To OLUserName Length    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 = 收到的字节数    Dim data As [Byte]() = New Byte( ) 缓冲区大小    Dim sender As New IPEndPoint(IPAddress Any )    Dim tempRemoteEP As EndPoint = CType(sender EndPoint)    recv = ClientSocket ReceiveFrom(data tempRemoteEP)       Dim msgHead As String = Encoding Unicode GetString(data ) 获得消息头的内容    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( ) & Chr( ) & 收到打洞消息 )    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 = To OLUserName Length    If remoteUser = OLUserName(i) Then    remoteUEP = OLUserEP(i)    Exit For    End If    If i = OLUserName Length 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(P PCONN & username & | & remoteUser)       chatDone = New ManualResetEvent(False)    ClientSocket SendTo(msgbytes remoteUEP)    chatDone WaitOne( 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( True)    If testHold = True Then    Exit While    Else    Console WriteLine( 打洞超时 发送消息失败 )    Console Write( 是否重试 按Y重试 按任意值结束发送: )    Dim YorN As String = Console ReadLine() ToUpper    If YorN = Y Then    testHold = False    Else    Exit Sub    End If    End If    End While       While testChat <> True    Console WriteLine( 打洞成功 正在准备发送 )    chatDone = New ManualResetEvent(False)    ClientSocket SendTo(msgbytes remoteUEP)    chatDone WaitOne( True)    If testChat = True Then    Console WriteLine( 消息发送成功!! )    Exit While    Else    Console WriteLine( 发送超时 发送消息失败 )    Console Write( 是否重试 按Y重试 按任意值结束发送: )    Dim YorN As String = Console ReadLine() ToUpper    If YorN = Y Then    testChat = False    Else    Exit Sub    End If    End If    End While    testHold = False    testChat = False    End Sub       处理聊天消息    Private Sub showChatMsg(ByVal indata() As Byte ByVal recvcount As Integer)    Dim msgStr As String = Encoding Unicode GetString(indata recvcount )    Dim splitStr() As String = msgStr Split( | )    Dim fromUname As String = splitStr( )    Dim msg As String = splitStr( )    Console WriteLine(Chr( ) & Chr( ) & 收到来自 & fromUname & 的消息: & msg)    Console Write( Client> )    Dim i As Integer    For i = To OLUserName Length    If OLUserName(i) = fromUname Then    Exit For    End If    Next    Dim tempbytes() As Byte = Encoding Unicode GetBytes(CHTMSGEND)    ClientSocket SendTo(tempbytes OLUserEP(i))    End Sub       处理打洞函数    Private Sub makeHold(ByVal indata() As Byte ByVal recvcount As Integer)    Dim makholdstr As String = Encoding Unicode GetString(indata recvcount)    Dim ipepstr() As String = makholdstr Split( : )    Dim holdEP As IPEndPoint = New IPEndPoint(IPAddress Parse(ipepstr( )) ipepstr( ))       Dim holdbytes() As Byte = Encoding Unicode GetBytes(HOLDOK & username)    ClientSocket SendTo(holdbytes holdEP)    Console WriteLine( 回送打洞消息 )    End Sub       处理用户上线的函数    Private Sub addOnLine(ByVal inData() As Byte ByVal recvCount As Integer)    Dim inStr As String = Encoding Unicode GetString(inData recvCount )    Dim userinfo() As String = inStr Split( | )    Dim strUserEP() As String = userinfo( ) Split( : )       Dim i As Integer    For i = To OLUserName Length    If OLUserName(i) = Then    OLUserName(i) = userinfo( )    OLUserEP(i) = New IPEndPoint(IPAddress Parse(strUserEP( )) strUserEP( ))    Console WriteLine(Chr( ) & Chr( ) & 用户 & OLUserName(i) & 上线了 用户地址: & OLUserEP(i) ToString)    Console Write( Client> )    Exit Sub    End If    Next       ReDim Preserve OLUserName(i + )    ReDim Preserve OLUserEP(i + )       OLUserName(i + ) = userinfo( )    OLUserEP(i + ) = New IPEndPoint(IPAddress Parse(strUserEP( )) strUserEP( ))       Console WriteLine(Chr( ) & Chr( ) & 用户 & OLUserName(i + ) & 上线了 用户地址: & OLUserEP(i + ) ToString)    Console Write( Client> )       End Sub       处理用户下线的函数    Private Sub removeOnLine(ByVal inData() As Byte ByVal recvCount As Integer)    Dim offUname As String = Encoding Unicode GetString(inData recvCount )       Dim i As Integer    For i = To OLUserName Length    If OLUserName(i) = offUname Then    OLUserName(i) =    OLUserEP(i) = Nothing    Console WriteLine(Chr( ) & Chr( ) & 用户 & offUname & 下线了 )    Console Write( Client> )    Exit Sub    End If    Next    End Sub       发送消息的函数    Public Function sendmsg(ByVal msg As String ByVal sendToIPEP As IPEndPoint) As String       Dim sendBytes As [Byte]() = Encoding Unicode GetBytes(msg)       判断发送的字节数是否超过了服务器缓冲区大小    If sendBytes Length > Then    Return W输入的字数太多    End If       判断消息是否发送成功    While msgSendEnd = False       sendDone = New ManualResetEvent(False)       Try       ClientSocket SendTo(sendBytes sendToIPEP)       sendDone WaitOne( True) 阻塞线程 秒       If msgSendEnd = False Then    Console WriteLine( 消息发送超时 )    Else    Exit While    End If       Catch e As Exception       Console WriteLine( 发送消息失败 & e ToString)    Exit Function       End Try       Console Write( 是否重试?按Y重试 按任意键退出: )    Dim userInput As String = Console ReadLine ToUpper       If userInput = Y Then    Else    msgSendEnd = False    Exit Function    End If       End While       msgSendEnd = False       End Function       用保持在线状态的函数    Private Sub holdonline(ByVal state As [Object])    ClientSocket SendTo(holdBytes ServerEP)    End Sub      #End Region      End Module cha138/Article/program/net/201311/13617

相关参考

知识大全 vb.net实现sqlite数据库编程示例

   在项目开发中充分体会到了一个精简数据存储模块的重要性及实用性在综合了三年开发经验的基础上向各位推介一下sqlite数据库希望更多的程序猿支持开源精神 &n

知识大全 简单实用jquery版三级联动select示例

本文主要为大家介绍下通过jquery实现三级联动select这里用到的json文件只是事例根据情况添加或编写感兴趣的朋友可以参考下哈希望对大家有所帮助   和js部分复制代码代码如下:<

知识大全 vb.net graphic 简单的画图程序

  [vb]  PublicClassForm  DimDrawStateAsBoolean  DimPreXAsSingle  DimPreYAsSingle  DimePAsNewPen(Colo

知识大全 页面右下角弹出提示框示例代码js版

右下角弹出提示框想必大家不会陌生吧本文简单的为大家实现一个具体代码如下有需求的朋友可以参考下 复制代码代码如下:cha138/Article/program/Java/JSP/20

知识大全 VB.NET多线程开发实例

同Java一样简单!VB.NET多线程开发实例  以下文字资料是由(历史新知网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  VBNET

知识大全 消息队列在VB.NET数据库开发中的应用

消息队列在VB.NET数据库开发中的应用  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 我们先简单

知识大全 c#(或vb.net)程序改进,可以提高性能

  使用值类型的ToString方法  在连接字符串时经常使用+号直接将数字添加到字符串中这种方法虽然简单也可以得到正确结果但是由于涉及到不同的数据类型数字需要通过装箱操作转化为引用类型才可以添加到字

知识大全 P2P通俗的说、用自己语言简单的说、P2P是做什么的

P2P通俗的说、用自己语言简单的说、P2P是做什么的?是什么来的?百度勿扰  以下文字资料是由(本站网www.cha138.com)小编为大家搜集整理后发布的内容,让我们

知识大全 从VB 6迁移到VB.NET的进展至今仍旧缓慢

从VB6迁移到VB.NET的进展至今仍旧缓慢  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!在发布V

知识大全 VB.NET中声音的播放

VB.NET中声音的播放  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 由VB升级为NET后有些人