以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  [求助]链接tcp提示错误  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=89232)

--  作者:新航程小何
--  发布时间:2016/8/17 17:09:00
--  [求助]链接tcp提示错误
各位老师,我想实现链接tcp的功能,写了如下代码,出现如附件截图错误

图片点击可在新窗口打开查看此主题相关图片如下: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

--  作者:大红袍
--  发布时间:2016/8/17 17:14:00
--  
我拷贝进去没问题。你调用了吗?怎么调用的?
--  作者:新航程小何
--  发布时间:2016/8/17 17:31:00
--  
\'socket = New MyClient("192.168.0.111", 4000, basemainform,"00FEFE")
--  作者:大红袍
--  发布时间:2016/8/17 17:38:00
--  

 提出什么提示了?是否连接成功?


--  作者:新航程小何
--  发布时间:2016/8/17 17:47:00
--  
提示连接成功,之后就出现了截图的问题
--  作者:大红袍
--  发布时间:2016/8/17 17:51:00
--  
 服务器端发上来
--  作者:新航程小何
--  发布时间:2016/8/17 17:55:00
--  
服务端我没有,用c#写的功能是没有没问题,现在转成vb就出现如下问题
--  作者:大红袍
--  发布时间:2016/8/17 18:20:00
--  

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
    nStream = tcp.GetStream()
    receiveThread = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf ReceiveMsg))
    receiveThread.IsBackground=True
    receiveThread.Start()
   
    Dim stream() As Byte = Encoding.ASCII.GetBytes(code.Replace(" ", ""))
    tcp.Client.Send(stream)
   
Catch generatedExceptionName As Exception
    MsgBox("无法连接服务器,请重试")
End Try
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))
    msgbox(_cmd)
    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