tcp socket programming

M

Mohammed Smadi

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
 
I

Irmen de Jong

Mohammed said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top