我创建了一个简单的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();
}
}