mulithreaded server

Discussion in 'Python' started by asit, Mar 11, 2008.

  1. asit

    asit Guest

    import socket
    import sys
    import thread

    p=1
    PORT=11000
    BUFSIZE=1024

    def getData(cSocket):
    global stdoutlock,cSocketlock
    while True:
    cSocketlock.acquire()
    data=cSocket.recv(BUFSIZE)
    if data=='q':
    data='client exited'
    cSocket.close()
    p=0
    cSocketlock.release()
    stdoutlock.acquire()
    stdout.write(data)
    stdoutlock.release()

    def sendData(cSocket):
    global stdoutlock,cSocketlock
    while True:
    stdoutlock.acquire()
    data=raw_input('>>')
    cSocketlock.acquire_lock()
    if data=='q':
    stdout.write('server exited')
    stdout.release()
    p=0
    cSocket.close()
    sSocket.send(data)
    sSocketlock.release()


    stdout=sys.stdout
    host=''
    sSocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    sSocket.bind((host,PORT,))
    sSocket.listen(1)
    #sSocketlock=thread.allocate_lock()
    stdoutlock=thread.allocate_lock()
    print 'waiting for connection'
    cSocket,addr=sSocket.accept()
    print 'connection from',addr
    cSocketlock=thread.allocate_lock()
    thread.start_new_thread(sendData,(cSocket,))
    thread.start_new_thread(getData,(cSocket,))
    if p==0:
    sSocket.close()



    In the above program, why there is an unhandeled exception ???
    asit, Mar 11, 2008
    #1
    1. Advertising

  2. asit

    Tim Roberts Guest

    asit <> wrote:
    >
    >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,cSocketlock
    > while True:
    > cSocketlock.acquire()
    > data=cSocket.recv(BUFSIZE)
    > if data=='q':
    > data='client exited'
    > cSocket.close()
    > p=0
    > cSocketlock.release()
    > stdoutlock.acquire()
    > stdout.write(data)
    > stdoutlock.release()


    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(cSocket):
    > global stdoutlock,cSocketlock
    > while True:
    > stdoutlock.acquire()
    > data=raw_input('>>')
    > cSocketlock.acquire_lock()
    > if data=='q':
    > stdout.write('server exited')
    > stdout.release()
    > p=0
    > cSocket.close()
    > sSocket.send(data)
    > sSocketlock.release()


    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 "sSocketlock", because you commented it out.

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

    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,
    Providenza & Boekelheide, Inc.
    Tim Roberts, Mar 13, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UGVyZG8=?=
    Replies:
    0
    Views:
    569
    =?Utf-8?B?UGVyZG8=?=
    Feb 6, 2004
  2. -
    Replies:
    2
    Views:
    386
    Alan Krueger
    Jul 29, 2005
  3. Replies:
    1
    Views:
    550
    Karlo Smid
    Jan 16, 2006
  4. Alexander Eisenhuth

    Heap problems in Mulithreaded Python extension

    Alexander Eisenhuth, Feb 1, 2007, in forum: Python
    Replies:
    1
    Views:
    259
    Alexander Eisenhuth
    Feb 1, 2007
  5. Jean-Paul Calderone

    Re: mulithreaded server

    Jean-Paul Calderone, Mar 11, 2008, in forum: Python
    Replies:
    3
    Views:
    268
Loading...

Share This Page