服務器端在啟動成功後
客戶端在登陸成功後
以下是服務器端:
Imports System
Imports System
Imports System
Imports System
Imports System
Module myUDPServer
#Region
Dim ServerSocket As New Socket(AddressFamily
Dim ipep As IPEndPoint = New IPEndPoint(IPAddress
Dim htUserList As New Hashtable
Dim userName(
Dim userIPEP(
Dim userTime(
Dim timerDelegate As New TimerCallback(AddressOf onLineTimeOut)
#End Region
#Region
Const LOGININ As String =
Const LOGINOUT As String =
Const GETULIST As String =
Const P
Const HOLDLINE As String =
Const HVUSER As String =
Const GETUSER As String =
Const MAKHOLD As String =
Const LOGINOK As String =
Const SERVCLS As String =
Const MSGEND As String =
Const EXITPRO As String =
Const SHOWULIST As String =
Const HELP As String =
#End Region
#Region
Sub Main()
Dim addressList As System
Dim ServerIP As IPAddress = addressList(
ServerSocket
Console
Console
Dim listenTH As New Thread(AddressOf listen)
listenTH
Console
Dim timer As New Timer(timerDelegate
Dim SVInput As String
While True
Console
SVInput = Console
Select Case SVInput
Case EXITPRO
listenTH
ServerSocket
Exit Sub
Case SHOWULIST
showUser()
Case HELP
Console
Case Else
Console
End Select
End While
End Sub
Sub showUser()
Dim hava As Boolean = False
If userName
Dim i As Integer
For i =
If userName(i) <>
hava = True
Exit For
End If
Next
If hava = False Then
Console
Exit Sub
End If
Console
For i =
If userName(i) <>
Console
End If
Next
Console
Else
Console
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
Dim tempRemoteEP As EndPoint = CType(sender
recv = ServerSocket
Dim msgHead As String = Encoding
Select Case msgHead
Case LOGININ
Dim LoginThing As String = userLogin(data
If LoginThing = HVUSER Then
sendMsg(HVUSER
ElseIf LoginThing = LOGINOK Then
sendMsg(LOGINOK
End If
Case LOGINOUT
userloginout(data
Case GETULIST
Dim userinfo As String = getUserList()
sendMsg(userinfo
Case P
questP
Case HOLDLINE
holdOnLine(data
End Select
Catch e As Exception
End Try
End While
End Sub
Private Sub questP
Dim recvStr As String = Encoding
Dim split() As String = recvStr
Dim fromEP As IPEndPoint
Dim toEP As IPEndPoint
Dim i As Integer
For i =
If userName(i) = split(
fromEP = userIPEP(i)
End If
If userName(i) = split(
toEP = userIPEP(i)
End If
Next
Dim holdbytes() As Byte = Encoding
ServerSocket
End Sub
Private Function getUserList() As String
Dim userInfo As String = GETUSER
Dim i As Integer
For i =
If userName(i) <>
userInfo += userName(i) &
End If
Next
Return userInfo
End Function
Private Function userLogin(ByVal data As Byte()
Dim Uname As String = Encoding
Dim Uinfobytes() As Byte
Dim i As Integer
Dim j As Integer
For i =
If Uname = userName(i) Then
Return HVUSER
End If
Next
For i =
If userName(i) =
userName(i) = Uname
userIPEP(i) = userEP
userTime(i) =
Console
Console
Uinfobytes = Encoding
For j =
If userName(j) <>
ServerSocket
End If
Next
Return LOGINOK
End If
Next
Dim userCount As Integer = userName
ReDim Preserve userName(userCount)
ReDim Preserve userIPEP(userCount)
ReDim Preserve userTime(userCount)
userName(userName
userIPEP(userIPEP
userTime(userTime
Console
Console
Uinfobytes = Encoding
For j =
If userName(j) <>
ServerSocket
End If
Next
Return LOGINOK
End Function
Private Sub userloginout(ByVal data As Byte()
Dim i As Integer
Dim Uname As String = Encoding
For i =
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 =
If userName(j) <>
sendMsg(loginOutMsg
End If
Next
Console
Console
Console
Console
Exit For
End If
Next
End Sub
Private Sub holdOnLine(ByVal data As Byte()
Dim Uname As String = Encoding
Dim i As Integer
For i =
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 =
If userTime(i) >
userTime(i)
If userTime(i) <=
Dim loginoutmsg As String = LOGINOUT & userName(i)
Console
Console
Console
Console
userName(i) =
userIPEP(i) = Nothing
Dim ULoginOutbytes() As Byte = Encoding
Dim j As Integer
For j =
If userName(j) <>
If userIPEP(j) Is Nothing Then
Else
ServerSocket
End If
End If
Next
End If
End If
Next
End Sub
Sub sendMsg(ByVal msg As String
Dim sendBytes As [Byte]() = Encoding
Try
ServerSocket
Catch e As Exception
Console
End Try
End Sub
#End Region
End Module
以下是客戶端:
Imports System
Imports System
Imports System
Imports System
Module Module
#Region
Const LOGININ As String =
Const LOGINOUT As String =
Const GETULIST As String =
Const P
Const HOLDLINE As String =
Const HVUSER As String =
Const GETUSER As String =
Const MAKHOLD As String =
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 =
Const SHOWULIST As String =
Const HELP As String =
Const SEND As String =
#End Region
#Region
Delegate Sub myMethodDelegate(ByRef myInData As Byte())
Dim msgSendEnd As Boolean = False
Dim ThListen As New Thread(AddressOf listen)
Dim ClientSocket As New Socket(AddressFamily
Dim username As String
Dim ServerEP As IPEndPoint
Dim holdBytes As [Byte]() = Encoding
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
While InputOK <> True
Console
InputIP = Console
Try
ServerEP = New IPEndPoint(IPAddress
InputOK = True
Catch
Console
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
Dim tempYN As String = Console
If tempYN =
bool = False
Else
Exit Sub
End If
End If
End While
Console
holdBytes = Encoding
Dim timer As New Timer(timerDelegate
Console
Dim getUbool As Boolean = False
While getUbool <> True
getUbool = getU()
If getUbool = False Then
Console
Dim tempYN As String = Console
If tempYN =
bool = False
Else
Exit Sub
End If
End If
End While
ThListen
Dim SVInput As String
While True
Console
SVInput = Console
Select Case SVInput
Case EXITPRO
exitApp()
ThListen
ClientSocket
Exit Sub
Case SHOWULIST
Console
showUserList()
Console
Case HELP
Console
Case Else
If SVInput
Dim split() As String = SVInput
If split
sendChatMsg(split(
Else
Console
End If
Else
Console
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
While (userOK <> True)
username = Console
userBytes = Encoding
If userBytes
Console
Console
Else
userOK = True
End If
End While
ClientSocket
Dim data As [Byte]() = New Byte(
Dim comStr As String = Encoding
Dim DGrecv As New myMethodDelegate(AddressOf recvLogin)
DGrecv
receiveDone
Dim recvStr As String = Encoding
If recvStr = comStr Then
Console
Return False
End If
If Encoding
Console
Return True
ElseIf Encoding
Console
Return False
Else
Console
Return False
End If
End Function
Private Sub exitApp()
Dim loginOutStr As String = LOGINOUT & username
Dim sendBytes As [Byte]() = Encoding
ClientSocket
End Sub
Private Function getU() As Boolean
getUDone = New ManualResetEvent(False)
Dim getUbytes As Byte() = Encoding
ClientSocket
Dim data As [Byte]() = New Byte(
Dim comStr As String = Encoding
Dim GUrecv As New myMethodDelegate(AddressOf recvGetU)
GUrecv
getUDone
Dim recvStr As String = Encoding
If recvStr = comStr Then
Console
Return False
End If
If Encoding
getUserList(data
Console
showUserList()
Return True
Else
Console
Return False
End If
End Function
Sub recvLogin(ByRef inData As Byte())
ClientSocket
receiveDone
End Sub
Sub recvGetU(ByRef inData As Byte())
getUrecCount = ClientSocket
getUDone
End Sub
Private Sub getUserList(ByVal userInfobytes() As Byte
Dim ustr As String = Encoding
Dim splitStr() As String = Nothing
splitStr = Ustr
Dim IPEPSplit() As String = Nothing
Dim i As Integer =
Dim k As Integer
For k =
ReDim Preserve OLUserName(i)
ReDim Preserve OLUserEP(i)
OLUserName(i) = splitStr(k)
IPEPSplit = splitStr(k +
OLUserEP(i) = New IPEndPoint(IPAddress
IPEPSplit = Nothing
i +=
Next
End Sub
Private Sub showUserList()
Dim i As Integer
For i =
If OLUserName(i) <>
Console
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
Dim tempRemoteEP As EndPoint = CType(sender
recv = ClientSocket
Dim msgHead As String = Encoding
Select Case msgHead
Case MSGEND
msgSendEnd = True
sendDone
Case LOGININ
addOnLine(data
Case LOGINOUT
removeOnLine(data
Case MSGEND
msgSendEnd = True
sendDone
Case MAKHOLD
Console
makeHold(data
Console
Case CHATMSG
showChatMsg(data
Case HOLDOK
testHold = True
holdDone
Case CHTMSGEND
testChat = True
chatDone
End Select
Catch
End Try
End While
End Sub
Private Sub sendChatMsg(ByVal remoteUser As String
If remoteUser = username Then
Console
Exit Sub
End If
Dim i As Integer
Dim remoteUEP As IPEndPoint
For i =
If remoteUser = OLUserName(i) Then
remoteUEP = OLUserEP(i)
Exit For
End If
If i = OLUserName
Console
Exit Sub
End If
Next
Dim msgbytes() As Byte = Encoding
Dim holdbytes() As Byte = Encoding
chatDone = New ManualResetEvent(False)
ClientSocket
chatDone
If testChat = True Then
testChat = False
Exit Sub
End If
testHold = False
While testHold <> True
Console
holdDone = New ManualResetEvent(False)
ClientSocket
ClientSocket
holdDone
If testHold = True Then
Exit While
Else
Console
Console
Dim YorN As String = Console
If YorN =
testHold = False
Else
Exit Sub
End If
End If
End While
While testChat <> True
Console
chatDone = New ManualResetEvent(False)
ClientSocket
chatDone
If testChat = True Then
Console
Exit While
Else
Console
Console
Dim YorN As String = Console
If YorN =
testChat = False
Else
Exit Sub
End If
End If
End While
testHold = False
testChat = False
End Sub
Private Sub showChatMsg(ByVal indata() As Byte
Dim msgStr As String = Encoding
Dim splitStr() As String = msgStr
Dim fromUname As String = splitStr(
Dim msg As String = splitStr(
Console
Console
Dim i As Integer
For i =
If OLUserName(i) = fromUname Then
Exit For
End If
Next
Dim tempbytes() As Byte = Encoding
ClientSocket
End Sub
Private Sub makeHold(ByVal indata() As Byte
Dim makholdstr As String = Encoding
Dim ipepstr() As String = makholdstr
Dim holdEP As IPEndPoint = New IPEndPoint(IPAddress
Dim holdbytes() As Byte = Encoding
ClientSocket
Console
End Sub
Private Sub addOnLine(ByVal inData() As Byte
Dim inStr As String = Encoding
Dim userinfo() As String = inStr
Dim strUserEP() As String = userinfo(
Dim i As Integer
For i =
If OLUserName(i) =
OLUserName(i) = userinfo(
OLUserEP(i) = New IPEndPoint(IPAddress
Console
Console
Exit Sub
End If
Next
ReDim Preserve OLUserName(i +
ReDim Preserve OLUserEP(i +
OLUserName(i +
OLUserEP(i +
Console
Console
End Sub
Private Sub removeOnLine(ByVal inData() As Byte
Dim offUname As String = Encoding
Dim i As Integer
For i =
If OLUserName(i) = offUname Then
OLUserName(i) =
OLUserEP(i) = Nothing
Console
Console
Exit Sub
End If
Next
End Sub
Public Function sendmsg(ByVal msg As String
Dim sendBytes As [Byte]() = Encoding
If sendBytes
Return
End If
While msgSendEnd = False
sendDone = New ManualResetEvent(False)
Try
ClientSocket
sendDone
If msgSendEnd = False Then
Console
Else
Exit While
End If
Catch e As Exception
Console
Exit Function
End Try
Console
Dim userInput As String = Console
If userInput =
Else
msgSendEnd = False
Exit Function
End If
End While
msgSendEnd = False
End Function
Private Sub holdonline(ByVal state As [Object])
ClientSocket
End Sub
#End Region
End Module
From:http://tw.wingwit.com/Article/program/net/201311/13617.html