newbie14 0 Posting Pro

We have a socket connection which receive data from gps devices. The problem now is that we find some of the data coming is corrupted and therefore it disconnect the socket. The issue here we have checked the device logs files all looks fine. The engineer have implemented in C# version things work fine. The problem now in java version we have this problem. Below is snippet of codes. So we purposely trying out with many os.flush things looks a bit better. What could be the cause any idea based on past experiences? Here is how part of the corruption looks like ˜RUŠ Â’b‚‚‚2±. For each complete message the protocol is to send os.write("@@\r\n".getBytes()); this as an acknowledgement. So then only the next message will be send. So when the message is corrupted no acknowledgement is sent then it just be there till it gets read time out. Then another round of connection will be made and normally the previous corrupted message will be the first to be send but this time is clean.

public void run() { 

     InputStream is  = null;    
     OutputStream os = null;

     String completeMessage="";

         try {

         is = sockConn1.getInputStream();
         os = sockConn1.getOutputStream();
         sockConn1.setSoTimeout(120000);
         int readChar = 0, charCount=0;
            while ((readChar=is.read()) != -1){               

                  if((readChar==36 ||charCount>0) && readChar!=42){               
                  charCount=1;            
                  completeMessage += (char) readChar;  
                  }
                  if (readChar == 42 && charCount>0){
                     completeMessage += (char) readChar; 
                     //writeToFile(message);
                     os.write(" ".getBytes()); 
                     os.flush();
                     os.write(" ".getBytes());   
                     os.flush();
                     os.flush();
                     os.flush();
                     os.write("@@\r\n".getBytes());
                     os.write("\r\n".getBytes());
                     os.write("\r\n".getBytes());
                     os.flush();
                     completeMessage="";
                     charCount=0;           
                   }
             }

          }
          catch (SocketTimeoutException ex){ 
               ex.printStackTrace();
          }  
          catch (IOException ex){ 
               ex.printStackTrace();
          }  
          catch (Exception ex){       
               ex.printStackTrace();
          }      
          finally
          {
            try{
                if ( os != null ){
                    os.close();
                }
            }
            catch(IOException ex){
               ex.printStackTrace();
            }
          }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.