gpt4 book ai didi

vb.net - 在可以发送确认/响应之前关闭 TCP 监听器连接

转载 作者:可可西里 更新时间:2023-11-01 02:42:06 27 4
gpt4 key购买 nike

我正在编写一个需要接收消息并发回确认的 TCP 监听器(服务器)。很基本的东西。那里确实有几十个示例,包括 MSDN,我从中复制了大部分代码。我可以毫无问题地接收消息。当我尝试发回响应时,问题就来了。发送客户端(Corepoint HL7引擎)报如下错误:

The connection was closed before a response was received

我已经使用我自己的 TCP 发送测试应用程序(使用从 MSDN 复制的代码编写)测试了我的服务并且它有效。但是当我收到来自 Corepoint 的消息时,响应不会返回。

下面是我的代码。有谁知道为什么 NetworkStream.Write 方法实际上没有发送数据(或者为什么客户端没有收到数据)?我已经尝试了我在其他帖子中发现的与我的问题类似的所有想法,但没有任何效果。是我做错了什么,还是 Corepoint 配置有问题?

Sub Main()
listenThread.Start()
End Sub

Private serverSocket As TcpListener
Dim listenThread As New Thread(New ThreadStart(AddressOf ListenForClients))

Private Sub ListenForClients()
Dim port As Int32 = '(pick a port #)
Dim localIP As IPAddress = 'enter your IP

serverSocket = New TcpListener(localIP, port)
serverSocket.Start()

While True 'blocks until a client has connected to the server
Dim client As TcpClient

If serverSocket.Pending Then
client = serverSocket.AcceptTcpClient
'tried these 2 settings with no effect
'client.NoDelay = True
client.Client.NoDelay = True

ProcessIncomingMessageSocketTCPClient(client) 'I was doing this in a separate thread but temporarily kept it on this thread to eliminate threading as the possible cause (but no luck)
client.Close()
Else
Threading.Thread.Sleep(1000) 'wait 1 second and poll again
End If
End While
End Sub

Private Sub ProcessIncomingMessageSocketTCPClient(ByRef objClient As TcpClient)

Dim strMessageText As String
Dim clientStream As NetworkStream
Dim msgBuffer(4096) As Byte
Dim numberOfBytesRead As Integer
Dim strChunk As String
Dim strCompleteMessage As New Text.StringBuilder
Dim sendBytes As Byte()

clientStream = objClient.GetStream()

Do
numberOfBytesRead = clientStream.Read(msgBuffer, 0, msgBuffer.Length)
strChunk = Encoding.ASCII.GetString(msgBuffer, 0, numberOfBytesRead)
strCompleteMessage.AppendFormat("{0}", strChunk)
Loop While clientStream.DataAvailable
strMessageText = strCompleteMessage.ToString

sendBytes = Encoding.ASCII.GetBytes("I received a message from you")
clientStream.Write(sendBytes, 0, sendBytes.Length)
objClient.Close() 'tried it with and without this line

End Sub

最佳答案

事实证明,我的代码没有任何问题。 TCP 过去和现在都正常工作。这个应用程序是一个 HL7 监听器,我缺少环绕我的 ACK 的 MLP。我一添加,发送应用程序就接受了我的 ACK,一切都很好。

关于vb.net - 在可以发送确认/响应之前关闭 TCP 监听器连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46086177/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com