1 2 3 Previous Next 83 Replies Latest reply on Jun 24, 2012 2:28 PM by 938489

    Trying to capture line feed or carriage return

    938489
      We have a socket application. We would like to know what is the line terminating character? Below is a snippet of codes. Any comments if this is right?
      BufferedReader readerBuffer = null;
      readerBuffer = new BufferedReader(new InputStreamReader(sockConn1.getInputStream()));
      int readChar=0;         
                  
      while ((readChar=readerBuffer.read()) != -1) 
      {
                             
           System.out.println("Read char "+(char) nextChar);  
      }
        • 1. Re: Trying to capture line feed or carriage return
          EJP
          The line terminator is whatever the peer is sending as a line terminator. Why do you care? Just use readLine(), it understands them all.
          • 2. Re: Trying to capture line feed or carriage return
            938489
            The problem is the peer wont send a fix length data.Normally if it send on individual data is ok when many data we notice some form of corruption. We would like to know where is the line break.
            • 3. Re: Trying to capture line feed or carriage return
              EJP
              I don't see what difference that makes. If the peer is sending lines, read lines, with readLine(), and if he isn't sending lines it doesn't make sense to ask what the line separator is.
              • 4. Re: Trying to capture line feed or carriage return
                rp0428
                >
                We would like to know what is the line terminating character?
                >
                The simplest way to know that is to send something with a teminating character and then look at the characters you receive.

                You can do that with code similar to the code you posted but why did you post code that can't work?
                int readChar=0;         
                while ((readChar=readerBuffer.read()) != -1) 
                {
                     System.out.println("Read char "+(char) nextChar);  
                }
                You declare 'readChar' but then try to print 'nextChar' which is not defined in the code and isn't used in the 'while' condition.

                Fix the code and then look at the characters you are receiving and you should see the terminating character and any 'corruption'.
                • 5. Re: Trying to capture line feed or carriage return
                  796440
                  935486 wrote:
                  The problem is the peer wont send a fix length data.
                  Doesn't matter. readLine() will block until a end-of-line is reached. Calling readLine() again will block until the next end-of-line is reched.
                  We would like to know where is the line break.
                  readLine() will do exactly that for you.
                  • 6. Re: Trying to capture line feed or carriage return
                    938489
                    Here is the improvised version of the codes. What we notice if we put the extra \r\n the corruption is lesser but still appear later part too. The corruption is always in the beginning of the first few characters
                    BufferedReader readerBuffer = null;
                    readerBuffer = new BufferedReader(new InputStreamReader(sockConn1.getInputStream()));
                    BufferedWriter writeBuffer = null;
                    writeBuffer = new BufferedWriter(new OutputStreamWriter(sockConn1.getOutputStream()));
                    int readChar=0;         
                                
                    while ((readChar=readerBuffer.read()) != -1) 
                    {
                                           
                         System.out.println("Read char "+(char) readChar);  
                            if readChar == '*')
                         {
                                  writeBuffer.write("@@\r\n\r\n");
                                  writeBuffer.flush();
                            }
                    }
                    • 7. Re: Trying to capture line feed or carriage return
                      EJP
                      Put in what extra \r\n? Put it in where?

                      I still dont know what this question is actually about. You haven't presented any evidence of data corruption here at all. What is being sent, what are you expecting to receive, and what are you actually receiving?
                      • 8. Re: Trying to capture line feed or carriage return
                        938489
                        Putting here writeBuffer.write("@@\r\n\r\n"); the extra \r\n. Below is sample of proper data and its corresponding corrupted data. For each complete string we send this writeBuffer.write("@@\r\n\r\n"); as acknowledgment to the peer. The problem happens where is a bulk of data, but on normal transmission no corruption at all. IS there a buffer problem which causing the corruption? I have tried to read the line by line character and did not find anything after the *
                        $SSM,354776032130814,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*
                        
                        ’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*
                        • 9. Re: Trying to capture line feed or carriage return
                          EJP
                          TCP does not lose or corrupt data, regardless of load, and neither do the Java socket or I/O classes. If you are getting data corruption, you are either misreading it or you are sending it that way. How are you sending this data? And what does your real receive code look like? Not like what you posted, because all that does is write @@\r\n\r\n.
                          • 10. Re: Trying to capture line feed or carriage return
                            938489
                            Below is the full codes without the db connection and processing as we see that will be too long here.
                            public class deviceServer {
                              class deviceConnectionHandler implements Runnable {
                            
                                private Socket sockConn1;
                                deviceConnectionHandler(Socket receivedSocketConn1) {
                                  this.sockConn1=receivedSocketConn1;
                                }
                                 Connection dbConnection = null;
                                 Statement dbStmt = null;
                                 
                                 public void run() { 
                                      
                                 String completeMessage="";
                                 BufferedReader readerBuffer = null;     
                                BufferedWriter writeBuffer = null;
                                         
                                 try {
                                     readerBuffer = new BufferedReader(new InputStreamReader(sockConn1.getInputStream()));
                                     writeBuffer = new BufferedWriter(new OutputStreamWriter(sockConn1.getOutputStream()));
                            
                                     int readChar=0;         
                                     
                                     sockConn1.setSoTimeout(120000);
                                    //dbConnection = connectionPool.getConnection();
                                     //dbConnection.setAutoCommit(false);
                            
                                         while ((readChar=readerBuffer.read()) != -1) 
                                          {                       
                                                   System.out.println("Read Char : "+(char) readChar);
                                                   completeMessage += (char) readChar;  
                                            
                                                  
                                                 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);
                                         }             
                                       }
                            
                                  }
                              }
                               public static void main(String[] args) {
                                  new deviceServer();
                               }
                               deviceServer() { 
                                  /*try {
                                  // setup the connection pool
                                      
                                           
                                           
                                  }
                                  catch (SQLException e) {
                                     e.printStackTrace(System.out);
                                 }*/
                                  try{
                                        final ServerSocket serverSocketConn = new ServerSocket(7000);                    
                                        while (true){
                                           try{
                                               Socket socketConn1 = serverSocketConn.accept();
                                             new Thread(new deviceConnectionHandler(socketConn1)).start();                           
                                           }
                                           catch(Exception e){
                                              e.printStackTrace();
                                           }
                                        }
                                  } 
                                  catch (Exception e){
                                     e.printStackTrace();
                                  }
                                 
                               }
                            
                            }
                            • 11. Re: Trying to capture line feed or carriage return
                              EJP
                              This is meaningless. You haven't shown the sending code, and all this code does is read until it finds a '*' and then write '@@\r\n\r\n'. Nothing else gets written, and everything else that gets read is thrown away. There is nothing here that demonstrates data corruption.
                              • 12. Re: Trying to capture line feed or carriage return
                                938489
                                The sending is done via a gps device. When there is connectivity and the device sends the data and there is no corruption. Problem starts when there is no connectivity (gprs) to the server then it will buffer the data. The moment it sees connectivity it will try to send each of the data in the buffer. It wont send another data till it receive the confirmation @@ of a previous message. We notice the corruption is normally occuring to the first few characters of the message string. Is it possible to confirm that the flush is sending it properly. We have checked the device log and sent message from the devices looks perfectly fine. Only when is read by the buffer reader it shows corruption.
                                • 13. Re: Trying to capture line feed or carriage return
                                  EJP
                                  You appear to be talking about two problems at the same time. (1) 'Trying to capture line feed or carriage return', and (2) 'the corruption', which you still haven't specified. You are sending '@@', and flush ensures it gets sent. I still don't understand for a moment what all this concern with line termination is about. I suggest the real problem here is that you don't understand the application protocol. You keep looking for excuses like data corruption from APIs and transport protocols that have been exercised zillions of times. Seems like just evading the issue to me.
                                  • 14. Re: Trying to capture line feed or carriage return
                                    rp0428
                                    >
                                    We notice the corruption is normally occuring to the first few characters of the message string
                                    . . .
                                    readerBuffer = new BufferedReader(new InputStreamReader(sockConn1.getInputStream()));
                                    . . .
                                    $SSM,354776032130814,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*

                                    ’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*
                                    >
                                    Well you do realize that an InputStreamReader reads 'characters' and not 'bytes' don't you? And that the network traffic is in 'bytes'?
                                    >
                                    An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.
                                    >
                                    So any 'corruption' is likely to be caused because your reader is trying to turn everything into characters even if they aren't characters.

                                    1. Comment out your code that creates and uses the readerBuffer.

                                    2. Capture the socket's inputstream
                                    InputStream is = sockConn1.getInputStream();
                                    3. Read the 'bytes' from the input stream and dump them out to the console so you can see what bytes are being sent. Or create a byte array and read the first 100 or so bytes into the byte array and examine the byte array contents in the debugger.

                                    Then you will see exactly what bytes are being sent and what your 'corruption' is really caused by.

                                    If you are going to use character readers you had better make sure that what you are getting is really characters and that the charset is the same as you expect.
                                    1 2 3 Previous Next