Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot receive Socket I/O more than once

Status
Not open for further replies.

akenty

Programmer
Jan 10, 2003
18
0
0
CA
Hi, having problems receiving an HL7 transmission from a third party through a socket transmission. Listening port connects fine, can receive the first transmission and process fine, but the second one is held up. Here is the code:

Code:
[blue]
    public void run() {
        
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        DateFormat dateFormat2 = new SimpleDateFormat("yyyyMMddHHmmss");
        int startCharacter = 11;
        int endOfFile = 13;
        int stopCharacter = 28;
        char charStart = (char)startCharacter;
        char charEnd = (char)endOfFile;
        char charStop = (char)stopCharacter;

        try {
            log.debug("Starting separate thread for port " + portNumber);
            servSock = new ServerSocket(portNumber);
            servSock.setSoTimeout(500);

            byte[] byteBuffer;

            for (;;) { // Run forever, accepting and servicing connections
                try {

                    Socket clntSock = servSock.accept(); 

                    Integer currentPort = clntSock.getPort();
                    String  currentIP = clntSock.getInetAddress().getHostAddress();
                    
                    log.debug("Handling client at "
                            + clntSock.getInetAddress().getHostAddress()
                            + " on port " + clntSock.getPort()
                            + " at " + dateFormat.format((java.util.Date) new java.util.Date()));

                    InputStream in = clntSock.getInputStream();
                    OutputStream out = clntSock.getOutputStream();

                    byteBuffer = processMessage(clntSock, in);
                    if (byteBuffer != null) {
                        String s = new String(byteBuffer, 0, byteBuffer.length);
                        log.debug("received=" + s);

                        // Get the Message Contorl ID and HL7 Version ID.
                        HL7Record   hl7Values = new HL7Record(s);
                        messageControlId = hl7Values.getMessageControlId();
                        HL7VersionId = hl7Values.getHL7VersionId();
                        
                        // send Acknowledgment record back.
                        String ackString = "MSH|^~\\&|Cornerstone|Client Site|"+sourceDescription+"_CHG|"+sourceDescription + 
                                            "|" + dateFormat2.format((java.util.Date) new java.util.Date()) + 
                                            "||ACK|" + messageControlId + "|P|" + HL7VersionId + "|" + 
                                            messageControlId + "|" + charEnd + "MSA|AA|" + charEnd;
    
                        // Add the Low Level Protocol characters (needs a start & stop character w/ each segment terminated).
                        ackString = charStart + ackString + charStop + charEnd;
                        
                        log.debug("Sending Acknowledgment record back. " + ackString);
                        byte[] ackBuffer = ackString.getBytes();
                        out.write(ackBuffer, 0, ackBuffer.length);

                        
                        // Forward the record the command class
                        Command c;
                        try {
                            log.debug("From the " + sourceDescription + ": " + s);
                            getLocationAndRevenueCenter(currentIP, currentPort);
                            log.debug("Revenue Center: " + revenueCenter);
                            log.debug("Inventory Location: " + inventoryLocation);
                            c = passedDaemon.execute(new BreakdownHL7RecordReceived(s, currentIP, currentPort, revenueCenter, inventoryLocation));
                        } catch (Exception e) {
                            log.error("Received HL7 record but could not process - " + dateFormat.format((java.util.Date) new java.util.Date()),e);
                        }
                    }

                    if (exitLoop) {
                        log.debug("exiting endless loop");
                        break;
                    }

                } catch (SocketTimeoutException e) {
//                    log.debug("Socket Timeout Exception");
                }
            }

        } catch (Exception e) {
            log.error("Exception on port " + portNumber + " - BUSY ");
        }finally {
            log.debug("Closing Server Socket.");

            if (servSock != null) {
                try {
                    servSock.close();
                } catch (IOException e) {
                    log.error("Error closing socket.",e);
                }
            }
        }

    }
[/blue]

When running the process with the third party, they say that they can see the first go through fine, but the second transmission sent on the same port is being held up on their end because an acknowledgment record is not being sent back. Looking through the logs, I am not receiving any of the second transmission to send an acknowledgment record for. If I were to close the input and output streams, the third party would time out after the first transmission and then use a different port number. Once connecting with the new port number, the same scenario would happen (next record would go through, but the following would fail and would not work until the next time out). Somewhat new to Java/sockets, does anyone see anything glaringly obvious or have a suggestion as to why the second transmission is not able to transmit over the same port number?

Thanks in advance
 
You should never process sockets on the same thread you are listening on. You should always spawn another thread to handle the socket you receive from .accept().
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top