[英]Send multiple HL7 message in the same Socket connection
在詢問之前我搜索了此問題,但找不到類似的東西。 我開發了一個客戶端/服務器解決方案來發送/接收HL7消息。 我正在使用套接字將客戶端連接到服務器,並且通過此連接,我只能使用OutputSteam對象發送1條HL7消息。 如何在同一套接字連接中發送多個HL7? 我嘗試了不同的方法,但是它們無法正常工作。
這是我的客戶端代碼:
//Create socket that is connected to server on specified port
System.out.println("Connecting to Server....");
Socket socket = new Socket(ipServer, serverPort);
System.out.println("Connected to Server");
StringBuffer HL7Message1 = new StringBuffer();
//Message 1
HL7Message1
.append(START_BLOCK)
.append("MSH|^~\\&|NES|NINTENDO|TESTSYSTEM|TESTFACILITY|20010101000000||ADT^A04|Q000000000000000001|P|2.3")
.append(CARRIAGE_RETURN)
.append("EVN|A04|20010101000000|||^KOOPA^BOWSER^^^^^^^CURRENT")
.append(CARRIAGE_RETURN)
.append("PID|1||123456789|0123456789^AA^^JP|BROS^MARIO^HELLO^WORLD^ONE^||19850101000000|M|||123 FAKE STREET^MARIO \\T\\ LUIGI BROS PLACE^TOADSTOOL KINGDOM^NES^A1B2C3^JP^HOME^^1234|1234|(555)555-0123^HOME^JP:1234567|||S|MSH|12345678|||||||0|||||N")
.append(CARRIAGE_RETURN)
.append("NK1|1|PEACH^PRINCESS^^^^|SO|ANOTHER CASTLE^^TOADSTOOL KINGDOM^NES^^JP|(123)555-1234|(123)555-2345|NOK|||||||||||||")
.append(CARRIAGE_RETURN)
.append("NK1|2|TOADSTOOL^PRINCESS^^^^|SO|YET ANOTHER CASTLE^^TOADSTOOL KINGDOM^NES^^JP|(123)555-3456|(123)555-4567|EMC|||||||||||||")
.append(CARRIAGE_RETURN)
.append("PV1|1|O|ABCD^EFGH^|||^^|123456^DINO^YOSHI^^^^^^MSRM^CURRENT^^^NEIGHBOURHOOD DR NBR^|^DOG^DUCKHUNT^^^^^^^CURRENT||CRD|||||||123456^DINO^YOSHI^^^^^^MSRM^CURRENT^^^NEIGHBOURHOOD DR NBR^|AO|0123456789|1|||||||||||||||||||MSH||A|||20010101000000"
)
.append(CARRIAGE_RETURN)
.append("IN1|1|PAR^PARENT||||LUIGI")
.append(CARRIAGE_RETURN)
.append("IN1|2|FRI^FRIEND||||PRINCESS")
.append(CARRIAGE_RETURN)
.append(END_BLOCK)
.append(CARRIAGE_RETURN);
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
//Send the MLLP-wrapped HL7 message to the server
out.write(HL7Message1.toString().getBytes());
byte[] byteBuffer = new byte[200];
in.read(byteBuffer);
System.out.println("Received from Server: " + new String(byteBuffer));
從服務器端
public String getMessage(InputStream anInputStream) throws IOException {
boolean end_of_message = false;
StringBuffer parsedMessage = new StringBuffer();
int characterReceived = 0;
try {
characterReceived = anInputStream.read();
} catch (SocketException e) {
System.out
.println("Unable to read from socket stream. "
+ "Connection may have been closed: " + e.getMessage());
return null;
}
if (characterReceived == END_OF_TRANSMISSION) {
return null;
}
if (characterReceived != START_OF_BLOCK) {
throw new RuntimeException(
"Start of block character has not been received");
}
while (!end_of_message) {
characterReceived = anInputStream.read();
if (characterReceived == END_OF_TRANSMISSION) {
throw new RuntimeException(
"Message terminated without end of message character");
}
if (characterReceived == END_OF_BLOCK) {
characterReceived = anInputStream.read();
if (characterReceived != CARRIAGE_RETURN) {
throw new RuntimeException(
"End of message character must be followed by a carriage return character");
}
end_of_message = true;
} else {
parsedMessage.append((char) characterReceived);
}
}
如何在同一套接字連接中發送更多HL7消息?
您真正想要做的就是將套接字與數據分開。 我建議使用像這樣的TCP客戶端服務器來管理套接字。 然后,您只需向您的客戶寫一條消息。 我有一項服務可以為我做這件事。 基本上,我打包了HL7的任何字符串,看起來好像您已經知道該怎么做,然后調用TCP Server的Send方法。
我的看起來像這樣,但是我認為鏈接中的那個看起來像:
public int Send(string data)
{
TcpClient sender = new TcpClient();
IPEndPoint localEP = new IPEndPoint(IPAddress.Any, Port);
try
{
TcpServerConnection cn;
if (connections.Count == 0)
{
//if there are no connections then we end up here.
ConnectSocket(sender, localEP);
}
cn = connections[0];
if (cn.Socket.Connected && cn.verifyConnected())
{
cn.sendData(data);
return connections.Count;
}
cn.Socket.Close();
ConnectSocket(sender, localEP);
if (cn.Socket.Connected && cn.verifyConnected())
{
cn.sendData(data);
return connections.Count;
}
cn.Socket.Close();
return -1;
}
catch (Exception e)
{
}
}
正如您所看到的那樣,重要的部分是TCP Server確定是否有可用的套接字,並在必要時打開一個套接字。 然后它將數據向下發送。 我認為,如果不將HL7消息傳遞中的TCP傳輸/編碼分開,將會遇到很多困難。 如果要等待MLLP第2版的ACK,我個人將ACK中的出站消息ID和返回消息ID進行比較,並進行比較:
if (lastOutMessageID == lastOutAckID || lastOutMessageID == "")
{
lastOutMessageID = SendNextHL7Message;
}
然后,我監聽ACK,並對傳入的數據引發一個事件:
void _contractManager_OnAckReceived(string msgID, string ackStatus)
{
lastOutAckID = msgID;
lastOutStatus = ackStatus;
lastAckTime = DateTime.Now;
outRetries = 0;
DoNextHL7();
}
但是簡短的版本是在它們自己的類中管理傳輸和套接字,並且只是從中發送和等待數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.