Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: Java socket corruption

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java socket corruption

    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
    $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*
    Here is the corrupted version.
    ’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*


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Java socket corruption

    Interesting that the output "sorts itself out" after a few characters. The upside-down question marks may be your terminal's best effort to print something that's not in its character set and may not map onto the same underlying bit strings - even though it looks as though they're the same character. Try printing the output with either Arrays.toString(String.getBytes()) or going through byte-by-byte and printing with Integer.toString(theByte, 2) to see what has happened to the data. I suspect Java isn't your problem, but something further downstream. Are you passing data through a serial port somewhere? It's not really interesting that your c# code works and this Java code doesn't - it could well be that the C# code is working by chance rather than design.

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java socket corruption

    Dear Sean,
    Below is a sample. IS connected via a gps device over the gprs connection. So what should I do then? Beside that I have also capture packets at the NIC level and also found the corruption there. Could be it due to how I send the acknowledgement os.write("@@\r\n".getBytes());. Surprisingly not all are corrupted only some of it.

    Clean version
    $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,4 8,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,4 9,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,4 8,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,4 5,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,4 8,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,5 8,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,4 8,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,5 3,45,49,50,44,57,46,53,56,44,68,55,44,48,44,54,68, 44,49,44,48,48,48,42
    Last edited by newbie14; June 6th, 2012 at 09:16 AM.

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Java socket corruption

    You get a different corrupted version each time? Nothing in Java will be causing that.

  5. #5
    Junior Member
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java socket corruption

    Dear Sean,
    Surprisingly the C# version of the engineers work well but I saw they deal all with hexadecimal should I do the same too? What do you say on it?

  6. #6
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Java socket corruption

    also capture packets at the NIC level and also found the corruption there
    Surprisingly the C# version of the engineers work well
    'Surprisingly'! How can the C# method work when you can see corruption in the data at the NIC? Are you running the C# program on the same PC you're running the Java program on? The problem appears to be at the start of the message, which makes me think it's an entrainment (Tx-Rx synchronisation) problem.

    I think I'd be tempted to use one or more dumb terminal emulator and see if they too suffer the same issues. How do you obtain your Socket and what's at the remote end of your Socket?

  7. #7
    Junior Member
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java socket corruption

    Dear Sean,
    The C# method is totally run in a different country and not even the country I am residing in. So how do you resolve this problem of (Tx-Rx synchronisation) problem in java? Attached below is my full codes. Is there anything not right on how the socket is being build?

    public class deviceServer {

    class ConnectionHandler implements Runnable {

    private Socket sockConn1;
    ConnectionHandler(Socket receivedSocketConn1) {
    this.sockConn1=receivedSocketConn1;
    }
    Date connCreated = null;
    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;
    ByteArrayOutputStream bOutput = new ByteArrayOutputStream();
    while ((readChar=is.read()) != -1){

    bOutput.write(readChar);
    if((readChar==36 ||charCount>0) && readChar!=42){
    charCount=1;
    completeMessage += (char) readChar;
    }
    if (readChar == 42 && charCount>0){
    completeMessage += (char) readChar;
    //writeToFile(message);
    os.write("@@\r\n".getBytes());
    bOutput.reset();
    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();
    }
    }

    }

    }// main function close







    public static void main(String[] args) {
    new deviceServer();
    }
    deviceServer() { // default constructor
    try
    {
    final ServerSocket serverSocketConn = new ServerSocket(7000);
    while (true)
    {
    try
    {
    Socket socketConn1 = serverSocketConn.accept();
    new Thread(new ConnectionHandler(socketConn1)).start();
    }
    catch(Exception e)
    {
    System.out.println("MyError:Socket Accepting has been caught in main loop."+e.toString());
    e.printStackTrace(System.out);
    }
    }
    }
    catch (Exception e)
    {
    System.out.println("MyError:Socket Conn has been caught in main loop."+e.toString());
    e.printStackTrace(System.out);
    //System.exit(0);
    }

    }
    }

  8. #8
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Java socket corruption

    I doubt there's anything wrong with your Java which will be causing this problem. What's on port 7000? Have you tried using telnet or a terminal emulator on that port? Does that give you good output while your packet capture software tells you the underlying data is corrupted?

    The C# method is totally run in a different country
    If you pay me a lot of money to write a program I don't have to give to you to prove that something else works properly, I can guarantee it will also be 'totally working'

    Is there any chance they can send you their working application so that you can verify for yourself that it still works at your end?

  9. #9
    Junior Member
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java socket corruption

    Dear Sean,
    Yes I have verified nothing is running on the port 7000 except for this listener. The problem with the corruption it happens on a random basis so that is the issue here. Below is the snipped of the C# codes.

    class ReadCommand : CommandReaderBase<InfoBinary>
        {
           private static List<byte[]> markStart = new List<byte[]> 
            { 
                new byte[] { 0x24, 0x24 }  
            };
     
            private static List<byte[]> m_EndMark = new List<byte[]> 
            { 
                new byte[] { 0x2A }        
            };
     
            private static int[] m_EndOffset = new int[] { 1 }; //{ 2, 1, 2 };
     
            private int protocolType = 0;
     
            private bool boolStartFound = false;
            private int posStart = -1;
     
            public ReadCommand(IAppServer appServer)
                : base(appServer)
            {
     
            }
     
            public override InfoBinary FindCommandInfo(IAppSession session, byte[] readBuffer, int offset, int length, bool isReusableBuffer, out int left)
            {
     
                left = 0;
     
                int lastSegmentOffset = BufferSegments.Count;
                int searchEndMarkLength = length;
     
                this.AddArraySegment(readBuffer, offset, length, isReusableBuffer);
     
                if (!boolStartFound)
                {
                    for (int i = 0, j = markStart.Count; i < j; i++)
                    {
                        var sm = BufferSegments.SearchMark(lastSegmentOffset, length, markStart[i]);
                        if (sm!=null) { protocolType = i; break;}
                    }
     
                    var pos = BufferSegments.SearchMark(lastSegmentOffset, length, markStart[protocolType]);
                    if (!pos.HasValue || pos.Value < 0)
                        return null;
     
                    //Found start mark
                    posStart = pos.Value;
                    boolStartFound = true;
     
                    lastSegmentOffset = pos.Value + m_EndOffset[protocolType];
     
                    //The end mark could not exist in this round received data
                    if (lastSegmentOffset + m_EndOffset[protocolType] > BufferSegments.Count)
                        return null;
     
                    searchEndMarkLength = BufferSegments.Count - lastSegmentOffset;
     
                }
                var endPos = BufferSegments.SearchMark(lastSegmentOffset, searchEndMarkLength, m_EndMark[protocolType]);
                //Haven't found end mark
                if (!endPos.HasValue || endPos.Value < 0)
                    return null;
     
                //Found end mark
                left = BufferSegments.Count - endPos.Value - m_EndOffset[protocolType];
     
                var commandData = BufferSegments.ToArrayData(posStart, endPos.Value - posStart + m_EndOffset[protocolType]);
     
                string a = BitConverter.ToString(commandData, 0, 2);
     
                string b = Encoding.Default.GetString(commandData, 0, commandData.Length).Substring(0, 2);
     
                var commandInfo = new InfoBinary(b, commandData);
     
                //Reset state
                boolStartFound = false;
                posStart = -1;
     
                BufferSegments.ClearSegements();
     
                return commandInfo;
            }
    }

  10. #10
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Java socket corruption

    nothing is running on the port 7000
    That's not what I meant - what I mean is "what is offering port 7000" on your PC? I see in one of your programs that you open a ServerSocket on port 7000, but I don't understand why you'd read anything from that. From what you said at the start of the thread, I was expecting port 7000 to be a connection to a remote device.

    Did you run the C# code to verify that it works on your PC?

  11. #11
    Junior Member
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java socket corruption

    Dear Sean,
    Just let me clear. This pc acts like a server where the devices will connect on port 7000 and send data on that port to the server. Hope I am clear now. No I do not have the full C# codes to run either just the snippet only. Anything abnormal between my codes and the C# nothing rite?

  12. #12
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Java socket corruption

    You're wasting your time trying to get your Java code working when you say you've seen scrambled data at the network level. I think your problem is between the device and your PC. Where is the device? Is it local, but connecting via the public Internet?

  13. #13
    Junior Member
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java socket corruption

    Dear Sean,
    No I think I confuse you. The remote device will connect to this server on port 7000. The start symbol for the data is $ and end symbol is *. So do you think I am doing anything wrong here if((readChar==36 ||charCount>0) && readChar!=42){ ? Thank you.

  14. #14
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Java socket corruption

    Like I've already said more than once: I don't think there's anything wrong (that would cause the problems you report) with your Java code.
    The remote device will connect to this server on port 7000
    When you test your code to get the outputs you've copy-pasted here, do you connect one of the remote devices to your server? When you run your test is the 'remote device' in your office or somewhere else in the world?

    You've already reported corrupted data at the network level, so it seems to me that any effort spent on the Java is a waste of time. Have you tried running your code but connecting locally to port 7000 with telnet and copy-pasting the input there? Your Java code should work as you expect.

  15. #15
    Junior Member
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java socket corruption

    Dear Sean,
    When we test the remote device is connect to this server using gprs connection. Yes I have tried before to pump the data via telnet the output is the same as you mentioned. So from your experience could it be due to network connectivity giving this sort of problem?

  16. #16
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Java socket corruption

    The way the data starts out 'bad' but ends up ok makes me think your problem is at source - and probably something to do with some bit-serial code somewhere. If you're using a Java Socket as your local endpoint, then what you're reading from that socket is exactly what was put into it at the other end. If you look at the binary values of the data in your problem examples, the sarting bit patterns bear very little relationship to the input data, so it's not as simple as a character encoding or a word-length issue.

    Have you sent your program to your C#-writing friends so that they can see the problem for themselves? If it works with telnet on port 7000, I think it's only reasonable to expect them to explain why it doesn't work with the data-supplying device.

  17. #17
    Junior Member
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java socket corruption

    Dear Sean,
    Ok I will google up on bit-serial problem in detail and try to understand it better. The problem is very dynamic and not on fix interval to capture it either. Yes I will send over the java codes to them for review what could be done to overcome and match it to the device requirements. I will update you the findings too. Even the device works only at some random time it gives out this kind of corrupted data.

  18. #18
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Java socket corruption

    Newbie
    the read() & write(int) work with binary value. Now to your code. It's much clearer if you code as following:
    if((readChar=='$' ||charCount>0) && readChar!='A') {...}
    if (readChar == 'A' && charCount>0){...}

    The statement completeMessage += (char) readChar; will include any binary character in the string completeMessage hence you think it is corrupted but it won't. The 1st statement allows any binary value and any character (except 'A') to be appended to completeMessage. That's why you think it was "corrupted" but it wasn't. Your code is only incomplete. If you expect only a displayable string (incl. NEWLINE and CARRIAGE RETURN) you have to eliminate the unwanted binary values. For example:

    if((readChar=='$' || charCount>0 && readChar >= ' ' || readChar == '\r' || readChar == '\n') && readChar!='A') {...}

Similar Threads

  1. Java socket server problem
    By Lionlev in forum Java Networking
    Replies: 6
    Last Post: May 27th, 2012, 11:22 AM
  2. Re: Java socket server problem
    By viperhastle in forum Java Networking
    Replies: 1
    Last Post: May 27th, 2012, 11:21 AM
  3. java.io.EOFException when sending object through socket
    By treshr in forum Java Networking
    Replies: 2
    Last Post: November 8th, 2011, 05:13 AM
  4. Socket java client
    By atef201080 in forum Java Networking
    Replies: 4
    Last Post: July 13th, 2011, 08:35 AM
  5. Java socket programming
    By lucy in forum Java Networking
    Replies: 1
    Last Post: April 26th, 2010, 09:13 PM