VB.net 通信的简单例子(新手)
我想做个服务端和客户端通信的例子。在服务端和客户端都能够判断对方是否已断了服务。
根据网上的例子,我简单套用了下,但总发觉运行时会有好多问题。
比如1.我将服务端打开,直接关闭,就会报“一个封锁操作被对 WSACancelBlockingCall 的调用中断。”的错误。
2.还有不能判断对方是否已断了服务。
3.当然还有其它潜在的问题。哪位朋友能否帮下忙,帮我修改下。谢了!!
我的代码如下
服务端
Dim listener As Socket
'与客户端会话的Socket
Dim mySocket As Socket
'服务端的运行状态
Dim IsRun As Boolean = False
'start监听接收数据线程
Dim myThread As Thread
'detail 监听接收数据
Dim myThreadService As Thread
'detail client Ip
Dim hashIP As New Hashtable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myThread = New Thread(AddressOf Listen)
myThread.Start()
End Sub
Public Sub Listen()
Dim localEndPoint As New IPEndPoint(Net.IPAddress.Parse("127.0.0.1"), 11000)
'初始化socket
listener = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
'绑定端口
listener.Bind(localEndPoint)
'开始监听
listener.Listen(10)
BeginInvoke(New EventHandler(AddressOf AddInfo), "服务端已启动,正在等待连接......")
Try
While True
mySocket = listener.Accept()
If Not hashIP.Contains(DirectCast(mySocket.RemoteEndPoint, System.Net.IPEndPoint).Address().ToString _
+ DirectCast(mySocket.RemoteEndPoint, System.Net.IPEndPoint).Port().ToString) Then
hashIP.Add(DirectCast(mySocket.RemoteEndPoint, System.Net.IPEndPoint).Address().ToString _
+ DirectCast(mySocket.RemoteEndPoint, System.Net.IPEndPoint).Port().ToString, mySocket)
End If
myThreadService = New Thread(AddressOf service)
myThreadService.Start(mySocket)
End While
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Sub service(objMySocket As Object)
mySocket = objMySocket
Dim bytes() As Byte = New [Byte](1024) {}
Dim data As String = String.Empty
Dim tokens() As String
Dim bytesRec As Integer
While True
Try
bytesRec = mySocket.Receive(bytes)
Catch ex As Exception
'这是为了知道客户端断了的时候,在这里抓错误(不知道这么做是否正确合理)
BeginInvoke(New EventHandler(AddressOf AddInfo), "client cut")
If hashIP.Contains(DirectCast(mySocket.RemoteEndPoint, System.Net.IPEndPoint).Address().ToString _
+ DirectCast(mySocket.RemoteEndPoint, System.Net.IPEndPoint).Port().ToString) Then
hashIP.Remove(DirectCast(mySocket.RemoteEndPoint, System.Net.IPEndPoint).Address().ToString _
+ DirectCast(mySocket.RemoteEndPoint, System.Net.IPEndPoint).Port().ToString)
End If
mySocket.Shutdown(SocketShutdown.Both)
mySocket.Close()
Exit Sub
End Try
data = Encoding.UTF8.GetString(bytes, 0, bytesRec)
'这代码主要是因为客户端发送消息后,我把服务端画面关了时,还会走这段WHILE循环,不然会报“未将对象引用设置到对象的实例。”的错误。
'不知道这么做是否正确合理
If (data.Equals("")) Then
Exit Sub
End If
tokens = data.Trim.Split("|")
Select Case tokens(0) '分析接收到的数据,可自己定义更多一些
Case "Chat"
BeginInvoke(New EventHandler(AddressOf AddInfo), tokens(1)) 'Invoke保证线程安全
Case "Exit"
BeginInvoke(New EventHandler(AddressOf AddInfo), tokens(1)) 'Invoke保证线程安全
hashIP.Remove(DirectCast(mySocket.RemoteEndPoint, System.Net.IPEndPoint).Address().ToString _
+ DirectCast(mySocket.RemoteEndPoint, System.Net.IPEndPoint).Port().ToString)
mySocket.Shutdown(SocketShutdown.Both)
mySocket.Close()
Exit Sub
End Select
End While
End Sub
'发送信息
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
Try
Dim msg As Byte() = Encoding.UTF8.GetBytes("Chat|" + TextBox1.Text)
mySocket.Send(msg)
ListBox1.Items.Add("本地信息 from Server test: " + TextBox1.Text)
TextBox1.Text = ""
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Sub AddInfo(ByVal sender As System.Object, ByVal e As System.EventArgs)
ListBox1.Items.Add("远程信息 from Client: " + sender.ToString)
End Sub
'关闭窗口时发关退出信息并清理资源
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If hashIP.Count > 0 Then
Dim msg As Byte() = Encoding.UTF8.GetBytes("Exit|服务端退出: " + Me.Handle.ToString)
Dim bytesSent As Integer = mySocket.Send(msg)
End If
listener.Close()
mySocket = Nothing
myThread.Abort()
End Sub
客户端
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim remoteEP As New IPEndPoint(Net.IPAddress.Parse("127.0.0.1"), 11000)
cliSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Try
cliSocket.Connect(remoteEP)
ListBox1.Items.Add("连接服务端成功!")
IsConnected = True
myThread = New Thread(AddressOf ReciveMsg)
myThread.Start()
Catch ex As Exception
ListBox1.Items.Add(ex.Message)
End Try
End Sub
Sub ReciveMsg()
Dim bytes() As Byte = New Byte(1024) {}
Dim data As String = String.Empty
Dim tokens() As String
While True
Dim bytesRec As Integer
Try
bytesRec = cliSocket.Receive(bytes)
Catch ex As Exception
'这是为了知道服务端断了的时候,在这里抓错误(不知道这么做是否正确合理)
IsConnected = False
BeginInvoke(New EventHandler(AddressOf AddInfo), "server cut") 'Invoke保证线程安全
Exit Sub
End Try
data = Encoding.UTF8.GetString(bytes, 0, bytesRec)
tokens = data.Trim.Split("|")
Select Case tokens(0) '分析接收到的数据,可自己定义更多一些
Case "Exit"
IsConnected = False
BeginInvoke(New EventHandler(AddressOf AddInfo), tokens(1)) 'Invoke保证线程安全
cliSocket.Shutdown(SocketShutdown.Both)
cliSocket.Close()
Exit Sub
Case "Chat"
BeginInvoke(New EventHandler(AddressOf AddInfo), tokens(1)) 'Invoke保证线程安全
End Select
End While
End Sub
'发送信息
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim msg As Byte() = Encoding.UTF8.GetBytes("Chat|" + TextBox1.Text)
Dim bytesSent As Integer
bytesSent = cliSocket.Send(msg)
ListBox1.Items.Add("本地信息 from Client Test: " + TextBox1.Text)
TextBox1.Text = ""
End Sub
Sub AddInfo(ByVal sender As System.Object, ByVal e As System.EventArgs)
ListBox1.Items.Add("远程信息 from Server: " + sender.ToString)
End Sub
'关闭窗口时发关退出信息并清理资源
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If IsConnected Then
Dim msg As Byte() = Encoding.UTF8.GetBytes("Exit|客户端退出: " + Me.Handle.ToString)
Dim bytesSent As Integer = cliSocket.Send(msg)
cliSocket.Close()
myThread.Abort()
End If
End Sub
[解决办法]
呵呵,楼主以上问题,本人也遇到过,后来直接基于别人写的类上面修改,最后解决了问题。
详情请参考:http://blog.csdn.net/lym11023/article/details/15502909