tcp socket programming

Discussion in 'Python' started by Mohammed Smadi, Oct 4, 2005.

  1. hi;
    If i have a tcp connection with a remote server, what is a good way to
    read all the data into a buffer before starting to process the data?
    I know that the data recieved will be 3 lines with CRLF between them.
    However if I can sock.recv(1024) the output is not consistent all the
    time, sometime i get one line and sometimes i get two. So I figures I
    should read all the data first then work on it and I used the following
    code:
    result = []
    while True:
    got=s.recv(1024)
    print got
    if not got: break
    result.append(got)
    got = [] # i tried also taking this out
    s.close()

    but this code just hangs in the loop and never quits

    any ideas will be much appreciated

    moe smadi
    Mohammed Smadi, Oct 4, 2005
    #1
    1. Advertising

  2. Mohammed Smadi wrote:
    > hi;
    > If i have a tcp connection with a remote server, what is a good way to
    > read all the data into a buffer before starting to process the data?
    > I know that the data recieved will be 3 lines with CRLF between them.
    > However if I can sock.recv(1024) the output is not consistent all the
    > time, sometime i get one line and sometimes i get two. So I figures I
    > should read all the data first then work on it and I used the following
    > code:
    > result = []
    > while True:
    > got=s.recv(1024)
    > print got
    > if not got: break
    > result.append(got)
    > got = [] # i tried also taking this out
    > s.close()
    >
    > but this code just hangs in the loop and never quits


    .... because it doesn't 'know' when to stop reading.
    The socket recv() returns anything from 0 to 1024 bytes
    depending on the amount of data that is available at that time.

    You have to design your wire protocol a bit differently if you want
    to do this in a consistent, reliable way.
    For instance, you can decide on sending *one* byte first that
    signifies the amount of bytes to read after that. (limiting the
    size to 255 ofcourse).

    Or you will have to change your read-loop to read until it
    encountered the third CRLF occurrence (and no more!)

    The latter is actually quite easily done by not reading directly
    from the socket object, but first converting it to a file-like
    object:

    s=socket.socket(....)
    s.connect(...)

    fs=s.makefile()
    fs.readline()
    fs.readline()
    fs.readline()


    --Irmen.
    Irmen de Jong, Oct 4, 2005
    #2
    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. Mohammed Smadi

    Re: tcp socket programming

    Mohammed Smadi, Oct 4, 2005, in forum: Python
    Replies:
    0
    Views:
    427
    Mohammed Smadi
    Oct 4, 2005
  2. Tiger
    Replies:
    5
    Views:
    970
    Dave Thompson
    May 1, 2006
  3. Laszlo Nagy
    Replies:
    1
    Views:
    4,822
    Mark Wooding
    Jan 27, 2009
  4. Martin Ambuhl

    Re: TCP Socket Programming

    Martin Ambuhl, Apr 18, 2011, in forum: C Programming
    Replies:
    2
    Views:
    371
    Bill Cunningham
    Apr 22, 2011
  5. Mac Man

    TCP Socket Programming

    Mac Man, Sep 6, 2008, in forum: Ruby
    Replies:
    1
    Views:
    104
    Mac Man
    Sep 6, 2008
Loading...

Share This Page