?? talker.vb
字號:
Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Threading
Imports System.Net.Sockets
Imports System.Windows.Forms
Class Talker
Implements IDisposable
'實現接口
Public socket As socket
'定義Socket
Public reader As TextReader
'定義讀出流
Public writer As TextWriter
'定義寫入流
Public client As Boolean
'標識程序是客戶端還是服務器端
Public endPoint As IPEndPoint
'IP地址
Public prevSendText As String
'在此之前發出去的信息
Public prevReceiveText As String
'在此之前收到的信息
Public statusText As String
'連接狀態
Public statusObj As Status
'定義Status類型變量
Public Sub Dispose() Implements IDisposable.Dispose
GC.SuppressFinalize(Me)
If Not (reader Is Nothing) Then
'這部分代碼用來釋放資源,銷毀對象
reader.Close()
reader = Nothing
'釋放讀出流對象
End If
If Not (writer Is Nothing) Then
writer.Close()
'釋放寫入流對象
writer = Nothing
End If
If Not (socket Is Nothing) Then
socket.Close()
'釋放Socket對象
socket = Nothing
End If
End Sub
Public Sub New(ByVal endPoint As IPEndPoint, ByVal client As Boolean)
'構造函數
Me.endPoint = endPoint
Me.client = client
socket = Nothing
reader = Nothing
writer = Nothing
statusText = String.Empty
prevSendText = String.Empty
prevReceiveText = String.Empty
End Sub
Protected Overrides Sub Finalize()
'析構
Dispose()
MyBase.Finalize()
End Sub
Friend Event Notifications(ByVal notify As Notification, ByVal data As Object)
'定義【通知】事件,對notify進行處理
Public Enum Notification
'系統可能的通知
Initialized = 1
StatusChange
ReceivedAppend
ReceivedRefresh
EndNotify
ErrorNotify
End Enum
Public Enum Status
'狀態
偵聽
已正常連接
End Enum
Public Sub Start()
ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback _
(AddressOf EstablishSocket))
'將用戶項排隊到線程池,調用指定的委托,
'并指定在接受線程池服務時要傳遞給委托的對象()
End Sub
Private Sub EstablishSocket(ByVal state As Object)
Dim stream As NetworkStream
'定義流
stream = Nothing
'初始化
Try
If Not client Then
'如果不是客戶端,則進行偵聽
Dim listener As Socket
Try
listener = New Socket(AddressFamily.InterNetwork, _
SocketType.Stream, ProtocolType.Tcp)
'實例化Socket類
listener.Blocking = True
'指示Socket處于阻塞模式
listener.Bind(endPoint)
'綁定本地的IP地址
SetStatus(Status.偵聽)
'顯示狀態
listener.Listen(0)
'偵聽
socket = listener.Accept()
'處理傳入的連接請求
listener.Close()
'關閉Socket連接
stream = New NetworkStream(socket)
'實例化流類
reader = New StreamReader(stream)
'實例化讀出流類
writer = New StreamWriter(stream)
'實例化寫入流類
writer.Write("WINTALK .NET")
writer.Flush()
'清理緩沖區,將所有的緩沖數據寫入基礎設備
Catch e As SocketException
'如果已經存在了一個偵聽端,那么這個端口作為客戶端
If e.ErrorCode = 10048 Then
client = True
endPoint = New IPEndPoint _
(Dns.Resolve("127.0.0.1").AddressList(0), endPoint.Port)
'設置端口地址
Else
RaiseEvent Notifications _
(Notification.ErrorNotify, "初始化Socket時發生錯誤:" & ControlChars.CrLf & e.ToString())
'觸發事件
End If
End Try
End If
If client Then '以客戶端形式進行連接
Dim temp As New Socket(AddressFamily.InterNetwork, _
SocketType.Stream, ProtocolType.Tcp)
'實例化Socket類
temp.Blocking = True
'指示Socket處于阻塞模式
temp.Connect(endPoint)
socket = temp
socket.SetSocketOption(SocketOptionLevel.Socket, _
SocketOptionName.ReceiveTimeout, 5000)
socket.SetSocketOption(SocketOptionLevel.Socket, _
SocketOptionName.SendTimeout, 5000)
stream = New NetworkStream(socket)
reader = New StreamReader(stream)
writer = New StreamWriter(stream)
Dim handshake(11) As Char
'判斷是否連接上,通過發出的 WINTALK .NET 消息來判斷
Try
If Not (reader.Read(handshake, 0, 12) > 0 And New _
String(handshake) = "WINTALK .NET") Then
socket.Close()
socket = Nothing
Else
socket.SetSocketOption(SocketOptionLevel.Socket, _
SocketOptionName.ReceiveTimeout, 0)
socket.SetSocketOption(SocketOptionLevel.Socket, _
SocketOptionName.SendTimeout, 0)
End If
Catch
socket.Close()
socket = Nothing
End Try
End If
If Not (socket Is Nothing) Then
SetStatus(Status.已正常連接)
RaiseEvent Notifications(Notification.Initialized, Me)
'觸發事件進行初始化
ReceiveTalk() '開始收取信息
RaiseEvent Notifications(Notification.EndNotify, "遠程連接已經關閉")
Else
RaiseEvent Notifications(Notification.ErrorNotify, _
"建立Socket失敗,檢查端口設置是否正確")
End If
Catch e As IOException
Dim sockExcept As SocketException = _
CType(e.InnerException, SocketException)
If Not (sockExcept Is Nothing) And 10054 = sockExcept.ErrorCode Then
RaiseEvent Notifications(Notification.EndNotify, "遠程連接已經斷開")
Else
RaiseEvent Notifications(Notification.ErrorNotify, "Socket 錯誤" & _
ControlChars.CrLf & e.Message)
End If
Catch e As Exception
RaiseEvent Notifications(Notification.ErrorNotify, "Socket 錯誤" & _
ControlChars.CrLf & e.Message)
End Try
End Sub
Public Sub SendTalk(ByVal newText As String)
'向遠程機器發送信息
Dim send As String
If prevSendText.Length <= newText.Length And _
String.CompareOrdinal(newText, 0, prevSendText, 0, _
prevSendText.Length) = 0 Then
'判斷是否是附加的字符串
Dim append As [String] = newText.Substring(prevSendText.Length)
send = String.Format("A{0}:{1}", append.Length, append)
Else
send = String.Format("R{0}:{1}", newText.Length, newText)
'否則取代該字符串
End If '
writer.Write(send)
'發送字符串
writer.Flush()
'清理緩沖區
prevSendText = newText
'保存字符串為后面作比較
End Sub
Private Sub SetStatus(ByVal statusObj As Status)
Me.statusObj = statusObj
'獲取系統狀態
RaiseEvent Notifications(Notification.StatusChange, statusObj)
'設置狀態
End Sub
Private Sub ReceiveTalk()
'從遠程客戶端獲取信息
Dim commandBuffer(19) As Char
Dim oneBuffer(0) As Char
Dim readMode As Integer = 1
Dim counter As Integer = 0
Dim textObj As New StringBuilder()
While readMode <> 0
If reader.Read(oneBuffer, 0, 1) = 0 Then
readMode = 0
Else
Select Case readMode
Case 1
If counter = commandBuffer.Length Then
readMode = 0
End If
If oneBuffer(0) <> ":"c Then
commandBuffer(counter) = oneBuffer(0)
counter = counter + 1
Else
counter = Convert.ToInt32(New String(commandBuffer, 1, _
counter - 1))
If counter > 0 Then
readMode = 2
textObj.Length = 0
Else
If commandBuffer(0) = "R"c Then
counter = 0
prevReceiveText = String.Empty
RaiseEvent Notifications _
(Notification.ReceivedRefresh, prevReceiveText)
End If
End If
End If
Case 2
textObj.Append(oneBuffer(0))
counter = counter - 1
If counter = 0 Then
Select Case commandBuffer(0)
Case "R"c
prevReceiveText = textObj.ToString()
RaiseEvent Notifications _
(Notification.ReceivedRefresh, prevReceiveText)
Case Else
Dim newText As String
newText = textObj.ToString()
prevReceiveText += newText
RaiseEvent Notifications _
(Notification.ReceivedAppend, newText)
End Select
readMode = 1
End If
Case Else
readMode = 0
End Select
End If
End While
End Sub
End Class
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -