📄 winsocket.vb.svn-base
字号:
'### Visual Basic.NET and Direct X9 Legend of MiR Project ###'
'### Mir Unleashed Client WinSock Module ###'
'### http://www.lomcn.co.uk ###' '### Credits to TrueADM and DeathWish ###'
'Hanles the WinSock for the client.
Imports System.Net
Imports System.Net.Sockets
Imports System.Text.ASCIIEncoding
Imports System.Text
Public Class SocketClient
Public Event onConnect(ByVal sNow As String)
Public Event onError(ByVal sDescription As String, ByVal iNumber As Integer, ByVal sNow As String)
Public Event onDataArrival(ByVal sData() As Byte, ByVal iTotalBytes As Integer, ByVal sNow As String)
Public Event onDisconnect(ByVal sNow As String)
Public Event onSendComplete(ByVal iDataSize As Integer, ByVal sNow As String)
Public Event onSendBegin(ByVal iDataSize As Integer, ByVal sNow As String)
Public Event onConnecting(ByVal sNow As String)
Public Event onTimeOut(ByVal sNow As String)
Public iBuffer As Integer = 10240, sBuffer(10240) As Byte
Public iPort As Integer, sHost As String
Public dDate As Date
Public Check1 As Integer
Public tcpClient As Socket
Private ConnectingTime As Long
Private TimeOutThread As New System.Threading.Thread(AddressOf TimeOut)
Private DisconnectThread As New System.Threading.Thread(AddressOf DisconnectCheck)
Public Sub Connect()
Dim IP As IPAddress
IP = IP.Parse(sHost)
Try
tcpClient = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
tcpClient.BeginConnect(New IPEndPoint(IP, iPort), AddressOf sockConnected, tcpClient)
RaiseEvent onConnecting(dDate.Now)
ConnectingTime = DateTime.Now.Ticks
If TimeOutThread.ThreadState = 8 Then TimeOutThread.Start()
Catch
RaiseEvent onError(Err.Description, Err.Number, dDate.Now)
Exit Sub
End Try
End Sub
Public Sub SendData(ByVal sData As String)
Try
tcpClient.BeginSend(ASCII.GetBytes(sData), 0, ASCII.GetBytes(sData).Length, 0, AddressOf sockSendEnd, tcpClient)
RaiseEvent onSendBegin(ASCII.GetBytes(sData).GetLength(0), dDate.Now)
Catch
RaiseEvent onError(Err.Description, Err.Number, dDate.Now)
Exit Sub
End Try
End Sub
Public Sub SendData(ByVal bData() As Byte)
Try
tcpClient.BeginSend(bData, 0, bData.Length, 0, AddressOf sockSendEnd, tcpClient)
RaiseEvent onSendBegin(bData.GetLength(0), dDate.Now)
Catch
RaiseEvent onError(Err.Description, Err.Number, dDate.Now)
Exit Sub
End Try
End Sub
Public Sub Disconnect()
Try
tcpClient.Shutdown(SocketShutdown.Both)
Catch
End Try
tcpClient.Close()
End Sub
Public Property RemoteHost() As String
Get
Try
Return sHost
Catch
RaiseEvent onError(Err.Description, Err.Number, dDate.Now)
Exit Property
End Try
End Get
Set(ByVal sRemoteHost As String)
Try
sHost = sRemoteHost
Catch
RaiseEvent onError(Err.Description, Err.Number, dDate.Now)
Exit Property
End Try
End Set
End Property
Public Property RemotePort() As Integer
Get
Try
Return iPort
Catch
RaiseEvent onError(Err.Description, Err.Number, dDate.Now)
Exit Property
End Try
End Get
Set(ByVal iRemotePort As Integer)
Try
iPort = iRemotePort
Catch
RaiseEvent onError(Err.Description, Err.Number, dDate.Now)
Exit Property
End Try
End Set
End Property
Public Function ConnectedHost() As String
Dim sIpEndPoint As IPEndPoint = tcpClient.RemoteEndPoint
Dim sIpSplit() As String = Split(sIpEndPoint.ToString, ":")
Try
Return sIpSplit(0)
Catch
RaiseEvent onError(Err.Description, Err.Number, dDate.Now)
Exit Function
End Try
End Function
Public Function ConnectedPort() As Integer
Dim sIpEndPoint As IPEndPoint = tcpClient.RemoteEndPoint
Dim sIpSplit() As String = Split(sIpEndPoint.ToString, ":")
Try
Return Int32.Parse(sIpSplit(1))
Catch
RaiseEvent onError(Err.Description, Err.Number, dDate.Now)
Exit Function
End Try
End Function
Public Function Connected() As Boolean
Try
Return tcpClient.Connected
Catch
RaiseEvent onError(Err.Description, Err.Number, dDate.Now)
Exit Function
End Try
End Function
Private Sub sockConnected(ByVal ar As IAsyncResult)
Try
If tcpClient.Connected = False Then RaiseEvent onError("Connection refused.", Err.Number, dDate.Now) : Exit Sub
tcpClient.BeginReceive(sBuffer, 0, iBuffer, 0, AddressOf sockDataArrival, tcpClient)
'DisconnectThread.IsBackground = True
If DisconnectThread.ThreadState = 8 Then DisconnectThread.Start()
RaiseEvent onConnect(dDate.Now)
Catch
RaiseEvent onError(Err.Description, Err.Number, dDate.Now)
Exit Sub
End Try
End Sub
Private Sub sockDataArrival(ByVal ar As IAsyncResult)
Dim bytesRead As Integer
Try
bytesRead = tcpClient.EndReceive(ar)
Catch
Exit Sub
End Try
'Try
Dim Data() As Byte = sBuffer
If bytesRead = 0 Then
tcpClient.Shutdown(SocketShutdown.Both)
tcpClient.Close()
RaiseEvent onDisconnect(dDate.Now)
Exit Sub
End If
'ReDim sBuffer(32767)
ReDim sBuffer(iBuffer)
tcpClient.BeginReceive(sBuffer, 0, iBuffer, 0, AddressOf sockDataArrival, tcpClient)
'SyncLock Data
Do While Running = True
If PacketBusy = False Then
PacketBusy = True
Check1 = Check1 + 1
If Check1 >= 2 Then
Beep()
End If
RaiseEvent onDataArrival(Data, bytesRead, dDate.Now)
Check1 = Check1 - 1
PacketBusy = False
Exit Sub
'Exit the loop
End If
Loop
'End SyncLock
'Catch
'RaiseEvent onError(Err.Description, Err.Number, dDate.Now)
'Exit Sub
'End Try
End Sub
Private Sub sockSendEnd(ByVal ar As IAsyncResult)
Try
Dim tcpClient As Socket = CType(ar.AsyncState, Socket)
Dim bytesSent As Integer = tcpClient.EndSend(ar)
RaiseEvent onSendComplete(bytesSent, dDate.Now)
Catch
RaiseEvent onError(Err.Description, Err.Number, dDate.Now)
Exit Sub
End Try
End Sub
Private Sub TimeOut()
Do While Running
If tcpClient.Connected Then
TimeOutThread.Abort()
Else
If ConnectingTime + (4000 * 10000) < DateTime.Now.Ticks Then
RaiseEvent onTimeOut(dDate.Now)
TimeOutThread.Abort()
End If
End If
TimeOutThread.Sleep(1000)
Loop
End Sub
Private Sub DisconnectCheck()
Do While Running
If Not tcpClient.Connected Then
RaiseEvent onDisconnect(Date.Now)
DisconnectThread.Abort()
End If
DisconnectThread.Sleep(1000)
Loop
End Sub
Public Sub SplitDataSend(ByVal Packet As String)
RaiseEvent onDataArrival(System.Text.Encoding.ASCII.GetBytes(Packet), 0, "")
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -