1 2 3 4 Previous Next 83 Replies Latest reply: Jun 24, 2012 9:28 AM by 938489 Go to original post RSS
      • 15. Re: Trying to capture line feed or carriage return
        938489
        Dear Rp0428,
        Agree with your suggestion yes the device is sending in byte format and not characters. So that might be the cause of the corruption.Below is my modified version of the codes. Can you please comment? The first value of this System.out.println("Byte Value is : "+b) ; is 56? So what standard is that representing because I check ASCII is
        public void run() { 
                  
             String completeMessage="";
             //BufferedReader readerBuffer = null; 
             InputStream is = null;    
              BufferedWriter writeBuffer = null;
                     
             try {
                 //readerBuffer = new BufferedReader(new InputStreamReader(sockConn1.getInputStream()));
                 is = sockConn1.getInputStream();
                 writeBuffer = new BufferedWriter(new OutputStreamWriter(sockConn1.getOutputStream()));
        
                 int readChar=0;         
                 
                 sockConn1.setSoTimeout(120000);
                   //dbConnection = connectionPool.getConnection();
                 //dbConnection.setAutoCommit(false);
        
                     while (is.read() != -1) 
                      {                       
                               //System.out.println("Read Char : "+(char) readChar);
                              // completeMessage += (char) readChar;  
                          byte b = (byte)is.read();
                             System.out.println("Byte Value is : "+b) ;
                             if (readChar == '*') {
                             try {
                                  
                              //writeBuffer.write("@@\r\n\r\n");
                             //writeBuffer.flush();                     
                              //db processing
                               dbConnection.commit();
                            }
                            catch (SQLException ex){ 
                                 ex.printStackTrace();
                                    try{    
                                        dbConnection.rollback();  
                                        } 
                                        catch (Exception rollback){    
                                             rollback.printStackTrace();
                                        }
                             }
                             catch (Exception e){
                                   e.printStackTrace(System.out);
                                       try{    
                                        dbConnection.rollback();  
                                        } 
                                        catch (Exception rollback){    
                                             rollback.printStackTrace(System.out);
                                        }
                             }
                             finally{
                                    try{
                                         if ( dbStmt != null ){
                                           dbStmt.close();
                                         }                                 
                                    }
                                    catch(SQLException ex){
                                    ex.printStackTrace(System.out);
                                    }                           
                                  }       
                           completeMessage="";           
                           }
                           
                      }
                   }
                   catch (SocketTimeoutException ex){ 
                        ex.printStackTrace();
                   }  
                   catch (IOException ex){ 
                        ex.printStackTrace();
                   }  
                   catch (Exception ex){ 
                        ex.printStackTrace();
                   }      
                   finally{
                     try{
                          if ( dbConnection != null ){
                            dbConnection.close();
                          }
                     }
                     catch(SQLException ex){
                         ex.printStackTrace();
                     }
                     try{
                         if ( writeBuffer != null ){
                                 writeBuffer.close();
                          }
                     }
                     catch(IOException ex){
                        ex.printStackTrace(System.out);
                     }             
                   }
        
              }
        • 16. Re: Trying to capture line feed or carriage return
          rp0428
          >
          So that might be the cause of the corruption
          >
          NO! As EJP already said
          >
          You haven't presented any evidence of data corruption here at all
          >
          The only indication that there might be corruption is that you are converting a byte stream to a character stream when you now say that the device is sending bytes and not characters. In that case your code is what would likely be causing any corruption. You can't just convert an arbitrary byte stream to characters.
          >
          Agree with your suggestion
          >
          Apparently not because I suggested that you dump the first 100 or so bytes to see what they were. Instead you provide one byte.

          If these are the characters
          >
          ’b����b0814,05827,006,L001,V,0000.0000,N,00000.0000,E,000.0,000.0,00.0,06:09:56 29-05-12,9.58,D7,0,6D,1,000*
          >
          Then post the bytes - and make sure that you are posting all 8 bits of the byte. Better yet since 'read' returns an int just print out the first 100 int values exactly as you read them.
          • 17. Re: Trying to capture line feed or carriage return
            938489
            I have change to the inputstream method and manage to capture the corrupted data and the relevant integer values for each character.So even without the buffereader is still corrupted too what could be the cause here? Below is the details

            The proper data
            $SSM,354776032130814,05827,006,L001,V,0000.0000,N,00000.0000,E,000.0,000.0,00.0,08:10:56 31-05-12,9.58,D7,0,6D,1,000*
            
            36,83,83,77,44,51,53,52,55,55,54,48,51,50,49,51,48,56,49,52,44,48,53,56,50,55,44,48,48,54,44,76,48,48,49,44,86,44,48,48,48,48,46,48,48,48,48,44,78,44,48,48,48,48,48,46,48,48,48,48,44,69,44,48,48,48,46,48,44,48,48,48,46,48,44,48,48,46,48,44,48,56,58,49,48,58,53,54,32,36,83,83,77,44,51,53,52,55,55,54,48,51,50,49,51,48,56,49,52,44,48,53,56,50,55,44,48,48,54,44,76,48,48,49,44,86,44,48,48,48,48,46,48,48,48,48,44,78,44,48,48,48,48,48,46,48,48,48,48,44,69,44,48,48,48,46,48,44,48,48,48,46,48,44,48,48,46,48,44,48,56,58,49,48,58,53,54,32,51,49,45,48,53,45,49,50,44,57,46,53,56,44,68,55,44,48,44,54,68,44,49,44,48,48,48,42
            Corrupted version
            ¦ÃÃM,354776032130814,05827,006,L001,V,0000.0000,N,00000.0000,E,000.0,000.0,00.0,08:10:56 31-05-12,9.58,D7,0,6D,1,000*
            
            166,202,213,77,44,51,53,52,55,55,54,48,51,50,49,51,48,56,49,52,44,48,53,56,50,55,44,48,48,54,44,76,48,48,49,44,86,44,48,48,48,48,46,48,48,48,48,44,78,44,48,48,48,48,48,46,48,48,48,48,44,69,44,48,48,48,46,48,44,48,48,48,46,48,44,48,48,46,48,44,48,56,58,49,48,58,53,54,32,36,83,83,77,44,51,53,52,55,55,54,48,51,50,49,51,48,56,49,52,44,48,53,56,50,55,44,48,48,54,44,76,48,48,49,44,86,44,48,48,48,48,46,48,48,48,48,44,78,44,48,48,48,48,48,46,48,48,48,48,44,69,44,48,48,48,46,48,44,48,48,48,46,48,44,48,48,46,48,44,48,56,58,49,48,58,53,54,32,51,49,45,48,53,45,49,50,44,57,46,53,56,44,68,55,44,48,44,54,68,44,49,44,48,48,48,42
            • 18. Re: Trying to capture line feed or carriage return
              EJP
              What code did you use to capture this data?
              • 19. Re: Trying to capture line feed or carriage return
                938489
                Below is the codes used to capture both int and char values. Anything still wrong?
                public void run() { 
                          
                     String completeMessage="";
                     String intValueMessage="";
                     //BufferedReader readerBuffer = null; 
                     InputStream is = null;    
                      BufferedWriter writeBuffer = null;
                             
                     try {
                         //readerBuffer = new BufferedReader(new InputStreamReader(sockConn1.getInputStream()));
                         is = sockConn1.getInputStream();
                         writeBuffer = new BufferedWriter(new OutputStreamWriter(sockConn1.getOutputStream()));
                        
                         sockConn1.setSoTimeout(120000);
                           //dbConnection = connectionPool.getConnection();
                         //dbConnection.setAutoCommit(false);
                            int readChar =0;
                             while ((readChar=is.read()) != -1) 
                              {                       
                                      completeMessage += (char) readChar;  
                                      intValueMessage=intValueMessage+","+readChar; 
                                     System.out.println("Byte Value is : "+readChar+"   "+(char)readChar);
                                     if (readChar == '*') {
                                     try {
                                           System.out.println("\nINT VALUE ARE "+intValueMessage +"\n"); 
                                      System.out.println("\nFull Message is  "+completeMessage+"\n");
                                      writeBuffer.write("@@\r\n\r\n");
                                     writeBuffer.flush();                     
                                      //db processing
                                       dbConnection.commit();
                                    }
                                    catch (SQLException ex){ 
                                         ex.printStackTrace();
                                            try{    
                                                dbConnection.rollback();  
                                                } 
                                                catch (Exception rollback){    
                                                     rollback.printStackTrace();
                                                }
                                     }
                                     catch (Exception e){
                                           e.printStackTrace(System.out);
                                               try{    
                                                dbConnection.rollback();  
                                                } 
                                                catch (Exception rollback){    
                                                     rollback.printStackTrace(System.out);
                                                }
                                     }
                                     finally{
                                            try{
                                                 if ( dbStmt != null ){
                                                   dbStmt.close();
                                                 }                                 
                                            }
                                            catch(SQLException ex){
                                            ex.printStackTrace(System.out);
                                            }                           
                                          }       
                                   completeMessage="";      
                                            intValueMessage="";     
                                   }
                                   
                              }
                           }
                           catch (SocketTimeoutException ex){ 
                                ex.printStackTrace();
                           }  
                           catch (IOException ex){ 
                                ex.printStackTrace();
                           }  
                           catch (Exception ex){ 
                                ex.printStackTrace();
                           }      
                           finally{
                             try{
                                  if ( dbConnection != null ){
                                    dbConnection.close();
                                  }
                             }
                             catch(SQLException ex){
                                 ex.printStackTrace();
                             }
                             try{
                                 if ( writeBuffer != null ){
                                         writeBuffer.close();
                                  }
                             }
                             catch(IOException ex){
                                ex.printStackTrace(System.out);
                             }             
                           }
                
                      }
                  }
                • 20. Re: Trying to capture line feed or carriage return
                  938489
                  Just to add on we have included the proper character encoding that is US-ASCII. Anything else could be wrong or creating this scenario? Could it be network corruption?
                  • 21. Re: Trying to capture line feed or carriage return
                    EJP
                    Nothing wrong there. I think it is time to sniff the network and see what is really being sent.
                    • 22. Re: Trying to capture line feed or carriage return
                      938489
                      We have capture at the network card level and yes found that the corruption is there. So could it be a network transmission corrupted the message?
                      • 23. Re: Trying to capture line feed or carriage return
                        EJP
                        More likely that's what the device is transmitting.
                        • 24. Re: Trying to capture line feed or carriage return
                          938489
                          Dear Ejp,
                          It is still the same could it be due to how we send this writeBuffer.write("@@\r\n");. We have tried sending before and also after the database processing but corruption problem is still there. The problem the hardware guys are using C# have no problem on their side and no single corruption is occurring. Anything extra to do for java to avoid this sort of things?
                          • 25. Re: Trying to capture line feed or carriage return
                            EJP
                            Certainly it could. You do have to know the application protocol. All the evidence here is that you don't. Such as your original question.
                            • 26. Re: Trying to capture line feed or carriage return
                              938489
                              The protocol is for every message that is received we need to send an acknowledgement writeBuffer.write("@@\r\n");. Then after the device receives this message it will send the next one. Could it be corrupted during the sending of the acknowledgement and sending of the next message in between the socket stream?
                              • 27. Re: Trying to capture line feed or carriage return
                                EJP
                                Not via TCP it can't. I would try it with an output stream rather than a writer, as you are risking character conversion. socket.getOutputStream().write("@@\r\n".getBytes()). You need to sniff the network as close as possible to the device to see what it is sending and receiving.
                                • 28. Re: Trying to capture line feed or carriage return
                                  jschellSomeoneStoleMyAlias
                                  935486 wrote:
                                  Below is the codes used to capture both int and char values. Anything still wrong?
                                  What is the output of the following line?

                                  System.out.println("\nINT VALUE ARE "+intValueMessage +"\n")
                                  • 29. Re: Trying to capture line feed or carriage return
                                    938489
                                    Dear Jschell,
                                    The output for the statement corresponds to ASCII int values.
                                    1 2 3 4 Previous Next