Exit Sub End If End If
End While
Console.WriteLine("用户名:" & username) holdBytes = Encoding.Unicode.GetBytes(HOLDLINE & username) '登陆成功后.用一个timer,每隔50秒向服务器发送消息,保持在线状态跟在主机注册的端口 Dim timer As New Timer(timerDelegate, Nothing, 10000, 50000)
'请求在线名单 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(10) & Chr(13) & "exit:输出当前程序" & Chr(10) & Chr(13) & "showuser:显示当前在线用户例表" & Chr(10) & Chr(13) & "send:发送消息.格式:send 用户名 消息" & Chr(10) & Chr(13) & "help:显示帮助" & Chr(10) & Chr(13) & "*********************************" & Chr(10) & Chr(13)) Case Else If SVInput.Substring(0, 4) = "SEND" Then Dim split() As String = SVInput.Split(" ") If split.Length = 3 Then sendChatMsg(split(1), split(2)) Else Console.WriteLine("*********************************" & Chr(10) & Chr(13) & "你输入的命令格式不正确.send命令格式为:send 用户名 你的消息" & Chr(10) & Chr(13) & "*********************************") End If Else Console.WriteLine("*********************************" & Chr(10) & Chr(13) & "笨瓜,你输入的不是有效的命令." & Chr(10) & Chr(13) & "*********************************") 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 > 24 Or userBytes.Length < 10 Then Console.WriteLine("用户名不得小于6个字节,且不得大于20个字节.") Console.Write("请重新输入你的用户名:") Else userOK = True End If End While
'向服务器发送客户消息 ClientSocket.SendTo(userBytes, ServerEP)
Dim data As [Byte]() = New Byte(1024) {}
Dim comStr As String = Encoding.Unicode.GetString(data, 0, 4)
'异面的接收服务器回送的消息 Dim DGrecv As New myMethodDelegate(AddressOf recvLogin) DGrecv.BeginInvoke(data, Nothing, Nothing)
'等待服务器回送消息的时长为10秒,否则为服务器超时 receiveDone.WaitOne(30000, True)
Dim recvStr As String = Encoding.Unicode.GetString(data, 0, 4) |