Is correct using "io.readpartial" instead of "io.read"?

Discussion in 'Ruby' started by Iñaki Baz Castillo, Apr 2, 2008.

  1. Hi, I'm doing a SIP server (listening in TCP) so I receive messages as the=
    =20
    following:

    =2D---------------------------------------------------------
    INVITE sip:user@ip SIP/2.0 CRLF
    From: xxxxx CRLF
    To: xxxxx CRLF
    Content-Lenght: 235 CRLF
    CRLF
    body body body body body body body body body body body body body body body
    body body body body body body body body body body body body body body body
    body body body body body body=20
    body body body body body body body body body

    body body body body body body body body body body body body body body bod=
    y=20
    body body
    =2D---------------------------------------------------------

    I receive and parse correctly the headers and when I detect an empty line I=
    =20
    must read the number of bytes indicated in "Content-Length" header (235 the=
    =20
    example). This is like SMTP or HTTP.

    I wonder which method is better in order to receive the body: io.read or=20
    io.readlines.

    io.read blocks after read the 235 bytes while io.readparti

    **** io.read
    Reads at most length bytes from the I/O stream (blocks).

    **** io.readpartial
    Reads at most maxlen bytes from the I/O stream. It blocks only if ios has n=
    o=20
    data immediately available. It doesn=E2=80=98t block if some data available.

    The code I use to read the body is:

    begin
    request.body =3D io.readpartial(request.hdr_content_length)
    rescue EOFError
    return
    end


    =46or now it works perfectly in both ways but there is an obvious differenc=
    e:

    With io.readpartial my code reads all the bytes indicated in Content-Length=
    =20
    (235) just if they come "together", but if they come "discontinnuated"=20
    io.readpartial return with the read bytes (in the example it can be less th=
    an=20
    235 bytes). Note that I don't use a loop for io.readpartial.

    With io.read the code blocks until read 235 bytes. If the data comes "not=20
    together" it waits for the 235 bytes.


    =46or now io.readpartial works perfectly and I like it since there could be=
    a=20
    SIP error (a client sends a body with less bytes than "Content-Length" says=
    )=20
    and in this way I can detect it inmediately.
    But I just do my test in localhost and so, not in real network which can be=
    =20
    congested, etc.


    Any opinion with this please? Thanks a lot.




    =2D-=20
    I=C3=B1aki Baz Castillo
    Iñaki Baz Castillo, Apr 2, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U3JpZGhhcg==?=

    Changing the Application Domain to read correct .config file

    =?Utf-8?B?U3JpZGhhcg==?=, Jan 26, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    5,628
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Jan 27, 2006
  2. joon
    Replies:
    1
    Views:
    511
    Roedy Green
    Jul 8, 2003
  3. Dan

    correct or not correct?

    Dan, Oct 2, 2003, in forum: HTML
    Replies:
    7
    Views:
    434
  4. J.Ram
    Replies:
    7
    Views:
    642
  5. froil
    Replies:
    12
    Views:
    304
    Gunnar Hjalmarsson
    Mar 2, 2006
Loading...

Share This Page