我正在编写一个需要接收消息并发回确认的TCP侦听器(服务器)。很基本的东西。实际上有几十个例子,包括MSDN,我从中复制了大部分代码。我可以收到消息没问题。当我尝试发回响应时出现问题。发送客户端(Corepoint HL7引擎)报告以下错误:
在收到回复之前关闭了连接
我已经使用自己的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
答案 0 :(得分:1)
事实证明我的代码没有任何问题。 TCP已经并且正在正常运行。这个应用程序是一个HL7监听器,我错过了我的ACK周围的MLP。一旦我补充说,发送应用程序接受了我的确认,一切都很好。