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±
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(); } }
sample proper message
Here is the corrupted version.$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*