*****************" & Chr(10) & Chr(13)) Console.Write("Server>") Uinfobytes = Encoding.Unicode.GetBytes(LOGININ & userName(userName.Length - 1) & "|" & userIPEP(userName.Length - 1).ToString) For j = 1 To userName.Length - 1 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, 4, recvCount - 4) For i = 1 To userName.Length - 1 If Uname = userName(i) Then Dim loginOutMsg As String = LOGINOUT & userName(i) userName(i) = "" userIPEP(i) = Nothing userTime(i) = 0
Dim j As Integer For j = 1 To userName.Length - 1 If userName(j) <> "" Then sendMsg(loginOutMsg, userIPEP(j)) End If Next Console.WriteLine(Chr(10) & Chr(13) & "*********************************") 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, 4, recvCount - 4) Dim i As Integer For i = 1 To userName.Length - 1 If Uname = userName(i) Then userTime(i) = 60 Exit For End If Next End Sub ''''用户超时退出 Private Sub onLineTimeOut(ByVal state As [Object]) Dim i As Integer For i = 1 To userName.Length - 1 If userTime(i) > 0 Then userTime(i) -= 5 If userTime(i) <= 0 Then Dim loginoutmsg As String = LOGINOUT & userName(i) Console.WriteLine(Chr(10) & Chr(13) & "*********************************") 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 = 1 To userName.Length - 1 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 Module1
#Region "参数" ''''以下是客户端到服务器端的消息开头 Const LOGININ As String = "10" ''''请求登陆的消息|||消息形式:10+自己的用户名 Const LOGINOUT As String = "11" ''''请求登出的消息|||消息形式:11+自己的用户名 Const GETULIST As String = "12" ''''请求获得在线用户列表|||消息形式:12+自己的用户名 Const P2PCONN As String = "13" ''''请求P2P连接的消息|||消息形式:13+自己的用户名+对方的用户名 Const HOLDLINE As String = "14" ''''保持连接.|||消息开式:14+自己的用户名 ''''以下是服务器到客户端的消息开头 Const HVUSER As String = "20" ''''用户名已存在 Const 上一页 [1] [2] [3] [4] [5] [6] [7] [8] 下一页 |