7

Questions

  1. What is the reason for the exception?

  2. Did the client cause any errors?

  3. If at all possible, please explain other errors.

Background

I am creating a Python GUI socket Server. When a client connects to my server, the GUI window will open (I am still working on this). But, when a client does connect, I get an error:

Unhandled exception in thread started by <function clientthread at 0x10246c230>

Since the actual script is rather long, I have provided a pastebin link.

Here is the thread code. s is the name of my socket object.

def clientthread(s):

    #Sending message to connected client
    #This only takes strings (words
    s.send("Welcome to the server. Type something and hit enter\n")

    #loop so that function does not terminate and the thread does not end
    while True:

        #Receiving from client
        data = s.recv(1024)
        if not data:
            break
        s.sendall(data)
        print data
    s.close()

Traceback

Thanks for the suggestion Morten. Here is the traceback.

Socket Created
Socket Bind Complete
Socket now listening
Connected
Traceback (most recent call last):
  File "/Users/BigKids/Desktop/Coding/Python 2/Sockets/Function/Server GUI Alpha Function.py", line 80, in clientthread
    s.send("Welcome to the server. Type something and hit enter\n")
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 170, in _dummy
    raise error(EBADF, 'Bad file descriptor')
error: [Errno 9] Bad file descriptor

Personally, I believe that many errors are due to the GUI.

Thanks!

4
  • Your code started for me fine. What causes the exception? Commented Mar 30, 2013 at 21:47
  • @Igonato probably a client disconnect wouldn't you think? That usually raises an exception IIRC Commented Mar 30, 2013 at 21:48
  • @MortenJensen I added my client. I'll check back in 15 mins - I have to go and erg :) Commented Mar 30, 2013 at 21:55
  • @xxmbabanexx bad file descriptor means the socket you're trying to write to is "bad". That could means a client disconnect. what is 's' in your exception dump? The client socket? Commented Mar 30, 2013 at 23:05

1 Answer 1

3

For one, you could catch the exception, print it and see what it is :)

Do this, for instance by surrounding it all with a try/except clause and printing whatever exception occurs.

def clientthread(s):
    try:
        #Sending message to connected client
        #This only takes strings (words
        s.send("Welcome to the server. Type something and hit enter\n")

        #loop so that function does not terminate and the thread does not end
        while True:

            #Receiving from client
            data = s.recv(1024)
            if not data:
                break
            s.sendall(data)
            print data
        s.close()
    except Exception:
        import traceback
        print traceback.format_exc()

I'm guessing the reason for this is client disconnect. This will cause an exception and you should handle it appropriately. If a client can disconnect in many ways. By telling you, by timing out, by dropping the connection while you're trying to send something etc. All these scenarios are plausible exception cases, and you should test for them and handle them. Hopefully this will help you move on, if not, please comment :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.