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 5 of 5

Thread: How to Distinguish Different Frames

  1. #1
    Junior Member
    Join Date
    Mar 2018
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to Distinguish Different Frames

    Hello,

    I'm learning how to use websockets, using Java for the server. I'm admittedly not an expert in socket programming, and I'm having some problems understanding how to read the incoming data in a reliable way in some cases.

    Some quick info:

    • The server receives websocket messages sent from a web browser.
    • A message consists of one or more frames.
    • Every frame tells the server if it should expect more frames or not.
    • Every frame carries the length of the payload (the body; the "actual" content) in early bytes
      (among other things), and lastly, the payload itself (or parts of it).
    • The web browser may send control frames unexpectedly in between frames that are related to eachother.

    When receiving a message split into multiple frames, I'm not sure how to determine where the first one actually ends and where the second one actually starts.

    What I think I know and assume:

    • A socket acts only as a queue of bytes, should always be waiting for another byte to add to the queue, and doesn't expect/demand any type of structure.
    • Hence, the socket adds no delimiters between frames.
    • Incoming data should always be expected to be "faked" and sent from malicious software.

    So it seems like the only way to find out where the first message ends, is to trust the payload length that has been maliciously included in the frame.

    The - perhaps unlikely - scenario that I'm worried about, is if the payload length of the first frame is longer than its actual payload, so that the server will keep reading the second frame - or a sudden control frame - as part of the first frame's payload.

    Example:

    1. The "user" sends a message split into 2 frames.
    2. The server receives frame 1 with a stated payload length of 10 bytes, but with an actual payload of only 2 bytes.
    3. The server suddenly receives an unrelated control frame, with a total length of 8 bytes.
    4. The server has now interpreted the entire frame 2 as byte 3-10 of frame 1's payload.
    5. The server receives frame 2.

    Frame 1 will tell that it's not the last frame.
    Frame 2 will only tell that it's a "continuation frame" but not of how many.
    Frame 3 will tell if it's the last frame.

    Am I by any chance missing something obvious? What should I rely on?

    Thanks!

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to Distinguish Different Frames

    Add a field with the frame number to the frame record.

    Can you post a byte by byte layout for the contents of the frames so we can easily see what data is contained in a frame?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2018
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Distinguish Different Frames

    I can't add fields, since the protocol doesn't allow it. You can see the frame structure here:
    https://tools.ietf.org/html/rfc6455#section-5.2

    And even if you could, the problem is still that if a too long payload length is given (in any frame), the server will just keep interpreting every new byte of the next frame as content, not as a frame with fields.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to Distinguish Different Frames

    Can the text for the record layout be copied and pasted here instead of a link?

    So far this problem is not related to java programming. There would be the same problems in any language.

    The basic problem seems to be finding an algorithm to solve the problem.

    Then given the algorithm, there would be questions on how to implement it in java.

    Do you have any specific java programming questions?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2018
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Distinguish Different Frames

    I figured that the link would be permanent, but sure:

    Attachment 3334

    Shortly, I suppose you're right. I've been spending so much time on Google trying to find an answer to this, but I haven't been able to find the same reflections anywhere, so I was hoping someone could shed me some light here. But I didn't mean to step over any lines, so I'm sorry if it's too off-topic.

    (I deliberately haven't pasted the additional text about the frame structure since I don't want to take up too much space if it's not welcomed in the forum. But I will if necessary.)
    Last edited by Djerkaf; March 10th, 2018 at 12:52 PM.

Similar Threads

  1. Distinguish additive + and "connecting +" in system.out.println
    By imago23 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 17th, 2014, 07:16 AM
  2. Distinguish between different Threads
    By ab1985 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 28th, 2014, 04:03 PM
  3. linking frames
    By tegalinks in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 25th, 2013, 07:48 AM
  4. Question on Frames.
    By dassix in forum Java Theory & Questions
    Replies: 2
    Last Post: April 24th, 2012, 05:03 PM
  5. GIF frames
    By vsector in forum AWT / Java Swing
    Replies: 0
    Last Post: April 15th, 2010, 05:25 PM