mulithreaded server

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • asit

    mulithreaded server

    import socket
    import sys
    import thread

    p=1
    PORT=11000
    BUFSIZE=1024

    def getData(cSocket ):
    global stdoutlock,cSoc ketlock
    while True:
    cSocketlock.acq uire()
    data=cSocket.re cv(BUFSIZE)
    if data=='q':
    data='client exited'
    cSocket.close()
    p=0
    cSocketlock.rel ease()
    stdoutlock.acqu ire()
    stdout.write(da ta)
    stdoutlock.rele ase()

    def sendData(cSocke t):
    global stdoutlock,cSoc ketlock
    while True:
    stdoutlock.acqu ire()
    data=raw_input( '>>')
    cSocketlock.acq uire_lock()
    if data=='q':
    stdout.write('s erver exited')
    stdout.release( )
    p=0
    cSocket.close()
    sSocket.send(da ta)
    sSocketlock.rel ease()


    stdout=sys.stdo ut
    host=''
    sSocket=socket. socket(socket.A F_INET,socket.S OCK_STREAM)
    sSocket.bind((h ost,PORT,))
    sSocket.listen( 1)
    #sSocketlock=th read.allocate_l ock()
    stdoutlock=thre ad.allocate_loc k()
    print 'waiting for connection'
    cSocket,addr=sS ocket.accept()
    print 'connection from',addr
    cSocketlock=thr ead.allocate_lo ck()
    thread.start_ne w_thread(sendDa ta,(cSocket,))
    thread.start_ne w_thread(getDat a,(cSocket,))
    if p==0:
    sSocket.close()



    In the above program, why there is an unhandeled exception ???
  • Tim Roberts

    #2
    Re: mulithreaded server

    asit <lipun4u@gmail. comwrote:
    >
    >In the above program, why there is an unhandeled exception ???
    Probably because the code as you posted it has at least a half-dozen
    mistakes.
    >import socket
    >import sys
    >import thread
    >
    >p=1
    >PORT=11000
    >BUFSIZE=1024
    >
    >def getData(cSocket ):
    global stdoutlock,cSoc ketlock
    while True:
    cSocketlock.acq uire()
    data=cSocket.re cv(BUFSIZE)
    if data=='q':
    data='client exited'
    cSocket.close()
    p=0
    cSocketlock.rel ease()
    stdoutlock.acqu ire()
    stdout.write(da ta)
    stdoutlock.rele ase()
    You do not need the "global" statement there, since you are not changing
    either stdoutlock or cSocketlock. However, you ARE setting "p", so you
    need a "global p". Otherwise, you are simply creating a variable called
    "p" that is local to the function, and which disappears when the function
    returns.

    Calling a global variable "p" is a very bad practice, by the way.
    >def sendData(cSocke t):
    global stdoutlock,cSoc ketlock
    while True:
    stdoutlock.acqu ire()
    data=raw_input( '>>')
    cSocketlock.acq uire_lock()
    if data=='q':
    stdout.write('s erver exited')
    stdout.release( )
    p=0
    cSocket.close()
    sSocket.send(da ta)
    sSocketlock.rel ease()
    Same comments. You do not need the "global" statement you have, but you do
    need "global p" if you want to change the global version of "p". Further,
    as Jean-Paul pointed out and you rather rudely ignored, there is no
    variable called "sSocketloc k", because you commented it out.

    Next, "stdout.release ()" will fail. "stdout" does not have a release
    function. You meant "stdoutlock.rel ease()".

    Next, you release sSocketlock, but you never acquired it. And if data does
    not equal "q", you repeatedly acquire stdoutlock, but you never release it.

    Fix these problems, and then see if you can ask for help in a more thorough
    fashion.
    --
    Tim Roberts, timr@probo.com
    Providenza & Boekelheide, Inc.

    Comment

    Working...