First off I'm want to learn socket/network programming with python so a lot of what I ask is newbie related.
I have written a test socket server that runs as a daemon. It listens on two sockets (say at ports 8000 and 9000) so that I can telnet over from another machine and get process info (ps type of info), one one port and control the server on the other port. What I want is to have the daemon run thread (A) which listens on port 8000, and then runs another thread (B) which listens 9000. Thread A monitors whether thread B is alive with a thread_B_.isSet() call. If thread B has terminated then thread A can exit when user tells it to, otherwise not. Thread B will terminate when user tells it to exit. I can start the daemon and connect to both ports just fine. If I leave the connections on using telnet for example, the connections keep working (stay alive). This part works. But I also want to be able to disconnect the telnet sessions, but leave the daemon server still listening on both ports so I can reconnect to both ports later. If I disconnect and try to reconnect within about 10secs it works fine. However if I stay disconnected from more than a minute then I cannot reconnect later. It seems as if the the server is not listening anymore....although the threads are still running i.e. the daemon is up. The message from telnet is: >>telnet: connect to address xxx.xxx.xxx.xxx: Connection refused Perhaps I do not understand the function of the following: socket.setdefaulttimeout(15) socket.settimeout(15.0) What is the difference between these?? Yes I have read the python docs - setdefaulttimeout(timeout) Set the default timeout in floating seconds for new socket objects. whereas: - settimeout(value): Set a timeout on blocking socket operations. So what this means to me is that with setdefaulttimeout one can set a global timeout for new sockets, wheres with settimeout one can fine tune it after a socket has been created. This is probably all wrong. Any ideas as to what is causing the server to stop listening after I disconnect for a long period of time. Also does one have to do a socket.shutdown() before one does a socket.close?? How should a server disconnect a client but keep listening for subsequent connections from other clients? I have included a summary of important code below. Let me know if should provide more or different info. Thanks a lot in advance. Pete ------------------------------------- The program basically works as below: ------------------------------------- (1) become a daemon (2) call server function which does the following (summarized): import socket ... HOST = '' socket.setdefaulttimeout(15) try: sa = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sa.bind((HOST, AdminPort)) sa.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sa.settimeout(15.0) sa.listen(1) except: ## exit on failure ## run 2nd socket handler as thread # create an event to signal the completion of client handler thread client_handler_complete=threading.Event() client_handler_complete.clear() server_handler_thread = threading.Thread (None, ClientHandler, None, \ (StateValues, client_handler_complete)) try: server_handler_thread.start() except: ## exit on failure while 1: ## wait for a connection try: #...waiting for connection (client, address)=sa.accept() except sa.timeout: #...waiting for connection timeout continue except: continue ## for now ignore this! #...someone's connecting, so check if ipaddress is allowed remote = str(address[0]) if Allowed_Hosts.has_key(remote): hostname = Allowed_Hosts[remote] Message = '%s connection accepted from: %s (%s:%s))' % \ (FUNCNAME, hostname, address[0], address[1]) log_message(StateValues, Message, 1) else: client.close() Message = '%s connection rejected from: %s' % (FUNCNAME, address) log_message(StateValues, Message, 1) continue socketfile = client.makefile() while 1: ## wait for user input data = '' data=read_data(socketfile) if not data or data == 'CR': continue else: Message = '%s input received: %s' % (FUNCNAME, data) log_debug_message(StateValues, Message) if data == 'q': ##disconnect client but keep waiting for connections ... client.close() elif data == 'x': ##disconnect client and shutdown server ... client.close() socketfile.close() sys.exit(0) ## wait for user input ## end wait for a connection (3) the thread which handles the second socket is coded like that above. -- http://mail.python.org/mailman/listinfo/python-list