Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: tcp socket programming

Reply
Thread Tools

Re: tcp socket programming

 
 
Mohammed Smadi
Guest
Posts: n/a
 
      10-04-2005

On Tue, 4 Oct 2005, Irmen de Jong wrote:

> 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.
>

that seems to have worked. I need to do more testing. Thanks

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: TCP Socket Programming Martin Ambuhl C Programming 2 04-22-2011 07:02 PM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
TCP Socket Programming Mac Man Ruby 1 09-06-2008 06:50 PM
send tcp raw socket (bogus tcp header length) Tiger C Programming 5 05-01-2006 05:53 AM
tcp socket programming Mohammed Smadi Python 1 10-04-2005 05:31 PM



Advertisments