此主题相关图片如下:360截图20160817170718191.jpg
Public socket As MyClient
Public Class MyClient
Private mainfrm As Object
Public receiveThread As System.Threading.Thread
Public nStream As System.Net.Sockets.NetworkStream
Public tcp As System.Net.Sockets.TcpClient
Public Sub New(ByVal ip As String, ByVal port As Integer, ByVal frm As Object,ByVal Code As String)
mainfrm = frm
Dim server As System.Net.IPAddress = System.Net.IPAddress.Parse(ip)
tcp = New System.Net.Sockets.TcpClient()
Try
MsgBox("测试1")
tcp.Connect(ip, port)
MsgBox("测试2")
Vars("conState")=False
Catch generatedExceptionName As Exception
MsgBox("无法连接服务器,请重试")
End Try
Dim stream() As Byte = Encoding.ASCII.GetBytes(code.Replace(" ", ""))
tcp.Client.Send(stream)
receiveThread = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf ReceiveMsg))
receiveThread.IsBackground=True
receiveThread.Start()
End Sub
\'\'\' <summary>
\'\'\' 接受消息的线程执行体
\'\'\' </summary>
Private Sub ReceiveMsg()
While True
Dim packet As Byte() = New Byte(2) {}
nStream.Read(packet, 0, packet.Length)
Dim _cmd As String = String.Concat(packet(0), packet(1))
MessageBox.show("关闭系统")
Select Case _cmd
\'11状态,关闭客户端
Case "11"
mainfrm.Close()
If receiveThread IsNot Nothing Then
receiveThread.Abort()
End If
nStream.Close()
End Select
End While
End Sub
End Class