To learn Python Network programming, I wrote two small programs on the server and the client according to the book, and found that an error was reported:
Traceback (most recent call last):
File “./tsTserv.py”, line 20, in
data = tcpCliSock.recv(BUFSIZ)
File “/usr/lib/python2.6/socket.py”, line 165, in _dummy
raise error(EBADF, ‘Bad file descriptor’)
socket.error: [Errno 9] Bad file descriptor
The source code of the server side is as follows:
while True:
print 'waiting for connection...'
tcpCliSock,addr = tcpSerSock.accept()
print '...connected from:',addr
while True:
data = tcpCliSock.recv(BUFSIZ)
if not data:
break
tcpCliSock.send('[%s] %s' %(ctime(),data))
tcpCliSock.close()
tcpSerSock.close()
Solution:
tcpCliSock.close() is placed in the second while loop, causing tcpCliSock to be closed after receiving data once, and this statement should be placed in the outer loop