16

The following receiveFile() function reads a filename and file data from the socket and splits it using the delimiter $.

But I am unable to close the socket and a Bad file descriptor error is raised. If I comment out the self.server_socket.close() statement then there is no error but the socket is listening forever.

Code:-

def listen(self):
 self.server_socket.listen(10)
 while True:
 client_socket, address = self.server_socket.accept()
 print 'connected to', address
 self.receiveFile(client_socket)
def receiveFile(self,sock):
 data = sock.recv(1024)
 data = data.split("$");
 print 'filename', data[0]
 f = open(data[0], "wb")
 #data = sock.recv(1024)
 print 'the data is', data[1]
 f.write(data[1])
 data = sock.recv(1024)
 while (data):
 f.write(data)
 data=sock.recv(1024)
 f.close()
 self.server_socket.close()
 print 'the data is', data
 print "File Downloaded"

Traceback:-

Traceback (most recent call last):
 File "server.py", line 45, in <module>
 a = Server(1111)
 File "server.py", line 15, in __init__
 self.listen()
 File "server.py", line 20, in listen
 client_socket, address = self.server_socket.accept()
 File "c:\Python27\lib\socket.py", line 202, in accept
 sock, addr = self._sock.accept()
 File "c:\Python27\lib\socket.py", line 170, in _dummy
 raise error(EBADF, 'Bad file descriptor')
socket.error: [Errno 9] Bad file descriptor
Alex Lisovoy
6,1254 gold badges29 silver badges28 bronze badges
asked May 5, 2013 at 8:50
3
  • 1
    The error is probably not from that code, but wherever you're calling self.server_socket.accept() - would help to include a bit more of the code, and the full traceback. Commented May 5, 2013 at 8:58
  • So, is the idea you want the server to shut down once it's received a file, or do you just want to close the active connection? Commented May 5, 2013 at 9:06
  • @aya in this code i am trying to shut down the server once file is received. Commented May 5, 2013 at 9:17

2 Answers 2

14

You are closing the server's listening socket, and after that calling again accept() on it. To finish receiving one file you should close client connection's socket (sock in function receiveFile).

answered May 5, 2013 at 9:16
Sign up to request clarification or add additional context in comments.

2 Comments

i understand your point, whats a proper sequence of closing the socket and shutdown the server
It depends on when do you need to shutdown server. If you want to receive only one file, just close client connection and after that - listening socket.
5

in this code i am trying to shut down the server once file is received

What you'll need is something to break out of the while True loop when you want to shut down the server. A simple solution would be to exploit the exception generated when you close the server socket...

def listen(self):
 self.server_socket.listen(10)
 while True:
 try:
 client_socket, address = self.server_socket.accept()
 except socket.error:
 break
 print 'connected to', address
 self.receiveFile(client_socket)
 print 'shutting down'
answered May 5, 2013 at 9:20

1 Comment

Thanks @aya i work around in a different way by returning value from from receiveFile function to break the loop

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.