C# TCP Socket无法读取医疗设备ACK报文问题求助
C# TCP Socket无法读取医疗设备ACK报文问题
- Wireshark可捕获到医疗设备发送的ACK报文,但C# Socket代码无法读取该报文
- 通信流程需等待ACK才能继续,当前因接收超时触发错误:无法建立连接。连接方在一段时间后未正确响应,或连接的主机未响应。
- 除Read方法外其余代码运行正常,可正常发送ACK,但无法接收设备返回的ACK
- 补充测试:使用COM端口数据模拟器发送的ACK能被正常读取,仅医疗设备的ACK无法读取
- 曾查阅相关技术资料但未找到可行方案,有人建议使用PcapDotNet但不知如何实现
相关代码
using System; using System.Text; using System.Net.Sockets; namespace myCom { public partial class SocketLan { private Socket oClientSocket; public SocketLan(string cIp, int nPort) { try { this.oClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); if (this.Connect(cIp, nPort)) { this.oClientSocket.SendTimeout = 5000; this.oClientSocket.ReceiveTimeout = 5000; } } catch { this.oClientSocket.Close(); } } public bool Connect(string cIp, int nPort) { bool lOk; IAsyncResult ar; ar = this.oClientSocket.BeginConnect(cIp, nPort, null, null); try { if (!ar.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(5), false)) { throw new TimeoutException(); } this.oClientSocket.EndConnect(ar); lOk = true; } catch { lOk = false; } return lOk; } public string Read(int nRead = 1024) { string cReturnData = ""; byte[] InStream; int nByte; if (this.oClientSocket.Connected) { InStream = new byte[nRead]; try { nByte = this.oClientSocket.Receive(InStream, 0, nRead, SocketFlags.None);//Here it goes wrong if (nByte > 0) { cReturnData = Encoding.ASCII.GetString(InStream, 0, nByte); } } catch (Exception ex) { cReturnData = ""; string cError = ex.Message;//This is the error! return cReturnData; } } return cReturnData; } } }
内容的提问来源于stack exchange,提问作者Infoservice
相关产品推荐
相关产品推荐

