Qn: Socket performance under Windows

Colin Brown cbrown at metservice.com
Wed Nov 12 02:50:24 CET 2003


Python2.3.2: Running the following receiver and sender code in separate
windows under Win2K does not work properly (missed & very delayed
transmissions). Under Redhat Linux 9 (where I will be using it) it works
fine (with up to 250 sending threads; I hit the thread limit at 255!)

Any idea why do I not get any errors reported under Windows?

Colin Brown
PyNZ

--[Sender.py]--------------------------------------------------------
# Send 50 'simultaneous' tcp transmissions to receiver

import os,socket,sys,time,thread,traceback

def error():
 tb =
traceback.format_exception(sys.exc_info()[0],sys.exc_info()[1],sys.exc_info(
)[2])
 return tb[len(tb)-1].replace('\n','')

class client:
    def __init__(self,nodeport):
        self.nodeport = nodeport
        self.cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.cli.settimeout(60.0)
    def connect(self):
        self.cli.connect((self.nodeport))
    def write(self,data):
        self.cli.sendall(data)
    def close(self):
        self.cli.close()

def send(v,data):
    cli = client(('localhost',20031))
    for cnt in range(5):
        try:
            cli.connect()
            break
        except:
            if cnt == 4:
                print v,'Connect failure: ',error()
            else:
                time.sleep(0.3)
    try:
        cli.write(data)
    except:
        print v,'Send failure: ',error()
    cli.close()
    print v

def action(data,number):
    for ii in range(1,1+number):
        thread.start_new_thread(send,(str(ii),data))

if __name__ == '__main__':
    action(''.join(['00001000',chr(32)*1000]),50)
    time.sleep(75.0)

--[Receiver.py]------------------------------------------------------
import select,socket,thread, time

COUNTER = 0
TIME0 = 0.0

class rx:
    def __init__(self,LocPort=20031,NumConn=100):
        self.svr = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.svr.bind (('', LocPort))
        self.svr.listen(NumConn)

    def nextConnAddr(self):
        if select.select([self.svr], [], [], None)[0]:
            return self.svr.accept()
        else:
            raise 'SocketReceiver.rx.nextConnAddr','connection lost'

    def rxThread(self,recvr):
        thread.start_new_thread(newSession,(recvr,))

    def close(self):
        self.svr.close()

def newSession(conn):
    sess = session(conn)
    sess.run()

class session:
    def __init__(self,conn,timeout=180.0):
        self.conn = conn
        self.conn.settimeout(timeout)

    def recvblock(self,length):
        block = []
        size = length
        while size > 0:
            chunk = self.conn.recv(size)
            if chunk:
                block.append(chunk)
                size = size - len(chunk)
            else:
                raise 'Session.recvblock','connection lost'
        return ''.join(block)

    def getsize(self):
        length = self.recvblock(8)
        return int(length)

    def getdata(self,size):
        return self.recvblock(size)

    def run(self):
        global COUNTER, TIME0
        data = self.getdata(self.getsize())
        if len(data) != 1000:
            print 'Transmission error'
        else:
            COUNTER = COUNTER + 1
            if TIME0 == 0.0:
                TIME0 = time.time()
            print '%i%s%0.3f' % (COUNTER,'\t',time.time()-TIME0)

rcvr = rx()
while 1:
    rcvr.rxThread(rcvr.nextConnAddr()[0])

----------------------------------------------------------------------






More information about the Python-list mailing list