Threading and Sockets Problem

Chris chrisahlers at yahoo.com
Mon Aug 25 02:37:26 CEST 2003


For some reason, I can only connect to my server once. After that, I
keep getting 'connection refused 111' messages).

import socket
import threading

class listen_server(threading.Thread):
	def __init__(self, port):
		threading.Thread.__init__(self)
		self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		self.socket.bind(("", port))
		self.setDaemon(True)
		self.start()
	def run(self):
		self.socket.listen(5)
		while 1:
			clientsocket, address = self.socket.accept()			
			print 'connection accepted'
			
class connection(threading.Thread):
	def __init__(self, host = None, port = None):
		threading.Thread.__init__(self)
		self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		self.lock = threading.Lock()
		self.alive = True
		self.connect(host,port)
		self.start()
	def connect(self, host, port):
		self.socket.connect((host,port))
	def disconnect(self):
		self.lock.acquire()
		self.alive = False
		self.lock.release()
	def run(self):
		while 1:
			self.lock.acquire()
			if not self.alive:
				self.lock.release()
				break
			self.lock.release()
		self.socket.close()
		print 'socket closed'

#main program:
x = listen_server(11090)
y = connection('localhost', 11090)
y.disconnect()

Any ideas?




More information about the Python-list mailing list