我创建了一个简单的 TCP 侦听器来处理 HL7 消息,我正在正确接收消息,并尝试发回 ACK 消息。另一端的服务器似乎没有收到响应,您是否发现此设置有任何问题?
我意识到它需要重构一点,现在我只是试图建立连接。
class Server
{
private TcpListener tcpListener;
private Thread listenThread;
public Server()
{
this.tcpListener = new TcpListener(IPAddress.Parse("hidden"), 55555);
this.listenThread = new Thread(new ThreadStart(ListenForClients));
this.listenThread.Start();
}
private void ListenForClients()
{
this.tcpListener.Start();
while (true)
{
TcpClient client = this.tcpListener.AcceptTcpClient();
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
clientThread.Start(client);
}
}
private void HandleClientComm(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
byte[] message = new byte[4096];
int bytesRead;
while (true)
{
bytesRead = 0;
try
{
bytesRead = clientStream.Read(message, 0, 4096);
}
catch
{
break;
}
if (bytesRead == 0)
{
break;
}
ASCIIEncoding encoder = new ASCIIEncoding();
string result = encoder.GetString(message, 0, bytesRead);
string[] Lines = result.Split('\n');
string id = "";
foreach (string line in Lines)
{
string[] values = line.Split('|');
if (values[0].Contains("MSH"))
{
id = values[9];
byte[] buffer = encoder.GetBytes("\\vMSH|^~\\&|Rhapsody|JCL|EpicADT|JCL-EPIC-TEST|||ACK|A" + id + "|P|2.4|\\nMSA|AA|" + id + "|");
Console.WriteLine("MSH|^~\\&|Rhapsody|Test|EpicADT|TEST|||ACK|A" + id + "|P|2.4|\\nMSA|AA|" + id + "|");
clientStream.Write(buffer, 0, buffer.Length);
clientStream.Flush();
}
}
}
tcpClient.Close();
}
}