I searched about this issue before asking but I couldn't find something similar. I developed a client / server solution to send / receive HL7 message. I am using socket to connect the client to the server and from this connection I am able to send just 1 HL7 message using OutputSteam object. How could I send multiple HL7 in the same socket connection? I tried different approaches but they didn't work properly.
Here is the piece of my code from the client side:
//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));
From the server side
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);
}
}
How could I send more HL7 messages in the same socket connection?