How to end TCP socket data while using readline()?

Arjun Chennu arjun.chennu at gmail.com
Mon Mar 1 10:53:10 EST 2010


It DOES seem like only when the connection socket is closed via conn.close()
that the data is flushed and the 'waiting' ends. So with the earlier
suggestion that I open one file-obj for reading and one for writing, I still
cannot acheive two-way communication because I need to close the connection
for it to actually occur completely!

My client waits after the "done sending" line indefinitely. Weird.

A

On Mon, Mar 1, 2010 at 16:35, Arjun Chennu <arjun.chennu at gmail.com> wrote:

> Thanks for the feedback.
>
> Opening a separate file-obj for writing and for reading is just what I've
> been trying, but I don't seem to get it to work. I'm new to python and I'm
> not sure if I'm missing the intricacy of some command. Please help:
>
> Here is my server snippet:
>
>
>             (conn, addr) = sock1.accept()    # connected socket
>             print 'Client (localhost) port: ', addr[1]
>
>             cf = conn.makefile('r',0)    # file obj for reading
>
>             lf = open('ccs.txt','w')
>
>             for linenum, line in enumerate(cf):    # iterate over socket
> lines
>                 lf.write(line)
>                 #sys.stdout.write(line)
>                 print len(line)
>
>             cf.close()
>
>             stat = 'wrote %s lines to file.\n' %(linenum+1)
>             cff = conn.makefile('w',0)   # file obj for writing
>             cff.writelines(stat)      # cff.write(stat) does not work
> either!!
>             cff.close()
>
>             lf.close()
>             conn.close()
>             print stat, "DONE!"
>
> And here is the client that I have for it:  (sfp is the local file object i
> read from)
>
>
>         for line in sfp.readlines():
>             cf.write(line)
>             print len(line)
>
>         print 'done sending'
>         cf.close()   #writing ends here
>
>         cff = s.makefile('r',0)   # file obj for writing
>         for line in cff.readlines():
>             print line
>
>         cff.close()
>
>         sfp.close()
>         s.close()
>
> The execution sends all the lines (and prints out the len(line) ) and then
> stays waiting. THen when I manually terminate the client script, the server
> script happily types the "DONE!" output.
>
> Where is this protocol hanging up? Help much appreciated, with a small
> explanation.
>
> Cheers,
> Arjun
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100301/8325baf0/attachment-0001.html>


More information about the Python-list mailing list