socket timing problem

doyen at mediaone.net doyen at mediaone.net
Tue Apr 17 21:02:04 EDT 2001


I have a socket which works but seems to wait a very long time to return
from the last recv call. Is there a place where I set the time-to-wait
before continueing on with processing the packets I've received. This is
on a winNT pc, but the client is comming from a unix box. I checked the
man pages for socket(2), but it didn't mean much to mean as-to setting a
timeout. I don't want the connection closed, I just want to process the
data I've gotten so far when there's a pause.

You can see I've junked it up with various attempts to listen for a few
seconds, process what I have, then return to get some more. I've been
able to process 100's of packets, but the conn.recv(2048) always seems
to be waiting at the last packet, and I have to send some more to get it
to return.  The packets are separated by etx (03 chars) but I expect to
get many logical packets within a single receive call.


import sys
import socket
import string
import time

HOST = '198.445.36.2'
PORT = 8111

def chkmsg(inmsg, outlst):
    if inmsg:
        print 'Checking ', inmsg
    outlst = 'ok'

if __name__ == '__main__':
    online = 1
    rtn = '   '
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((HOST, PORT))
    s.listen(1)
    print 'f4.py listening on ', HOST, ' port ', PORT
    conn, addr = s.accept()
    print 'Connected by', addr
    msgleft = ''
    while online:
        c = 1
        inflg = 1
        bigmsg = ''
        while inflg == 1:
            if msgleft:
                bigmsg = msgleft + bigmsg
                msgleft = ''
            s = time.time()
# this is where is seems to be waiting on the last packet
            data = conn.recv(2048)
            s2 = time.time()
            if s2 - s > 2:
                print 'time wait is ', str(s2)
                inflg = 0
            else:
                print 'received packet:', c
                bigmsg = bigmsg + data
                c = c + 1
            if not data: break
        outlst = ''
        chkmsg(bigmsg, outlst)
        msg = outlst
        conn.send(outlst)
        print 'sent ', outlst
        time.sleep(3)

    conn.close



--
Doyen & Sarah Klein
doyen at mediaone.net





More information about the Python-list mailing list