接收TCP响应 [英] Receiving TCP Response
问题描述
Hi Folks,
我正致力于转换我们使用的工具,该工具使用TCP从VB6到VS2005 +与一台设备进行通信。它曾经使用winsock.ocx,而VS我使用套接字。我已经能够成功地将命令发送到设备,但我无法得到回应。我是TCP通信的新手...
这是旧代码(VB 6)
公共sender1作为新套接字(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp)< br> Dim data As String
sender1.GetData data
(Mid $(data,i2 + 1,1)
这是我将它转换为(不成功!)
Dim data As String
frmMain.sender1.Receive(data)
(Mid $(data,i2 + 1,1)
当我收到回复时,我得到一个关于收到null的例外data ...如果我将数据更改为"Dim data as String""",则会出现System.InvalidCastException
Message ="无法将类型为'System.String'的对象强制转换为类型'System.Collections.Generic.IList`1 [System.ArraySegment`1 [System.Byte]]'。"。
我也尝试将"数据"更改为字节数组,但问题是那就是(Mid $(数据,i2 + 1,1),要求"数据"是一个字符串。
有没有人有任何建议?
我会使用字节数组。 Mid
需要字符串参数,所以我会改用Array.CopyTo。
你可以在VB论坛中找到更多帮助,因为这实际上更像是一种VB语言问题。
-Steve
Hi Folks,
Im working on converting a tool we use that communicates with a piece of equipment using TCP from VB6 to VS2005+. It used to use the winsock.ocx, and with VS I'm using sockets. I have been able to sucessfully trasmit a command to the equipment, but I am not able to get a response. I am fairly new to TCP communications...
This is the old code (VB 6)
Public sender1 As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim data As String
sender1.GetData data
(Mid$(data, i2 + 1, 1)
This is what I converted it to (unsucessfully!)
Dim data As String
frmMain.sender1.Receive(data)
(Mid$(data, i2 + 1, 1)
When I get a response, I get an exception about recieving null data... and if I change the data to "Dim data as String """, I get System.InvalidCastException occurred
Message="Unable to cast object of type 'System.String' to type 'System.Collections.Generic.IList`1[System.ArraySegment`1[System.Byte]]'.".
I have also tried changing "data" to a byte array, but the problem with that is (Mid$(data, i2 + 1, 1), requires "data" to be a string.
Does anyone have any suggestions?
I would use a byte array. Mid
requires string arguments, so I would use Array.CopyTo instead.
You may find more help in a VB forum, since this is actually more of a VB language question.
-Steve
这篇关于接收TCP响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!