日期:2011-08-03 浏览次数:20476 次
服务器端在启动成功后,输入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.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
Dim ipep As IPEndPoint = New IPEndPoint(IPAddress.Any, 11000)
Dim htUserList As New Hashtable '用来保存在线用户和用户的"IP和端口"
Dim userName(0) As String
Dim userIPEP(0) As IPEndPoint
Dim userTime(0) As Integer
Dim timerDelegate As New TimerCallback(AddressOf onLineTimeOut)
#End Region
#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 GETUSER As String = "21" '在线用户列表|||消息格式:21+用户名+EP
Const MAKHOLD As String = "22" '打洞命令|||消息格式:22+IP
Const LOGINOK As String = "23" '登陆成功
Const SERVCLS As String = "24" '服务器关闭
Const MSGEND As String = "25" '消息结束
'以下是服务器端的命名
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(0)
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, 0, 5000)
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(10) & Chr(13) & "exit:输出当前程序" & Chr(10) & Chr(13) & "showuser:显示当前在线用户例表" & Chr(10) & Chr(13) & "help:显示帮助" & Chr(10) & Chr(13) & "*********************************" & Chr(10) & Chr(13))
Case Else
Console.WriteLine("*********************************" & Chr(10) & Chr(13) & "笨瓜,你输入的不是有效的命令." & Chr(10) & Chr(13) & "*********************************")
End Select
End While
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(