confusion and problems with first socket script

Hameed Khan hameedkhaan at
Thu Feb 19 08:06:11 CET 2004

hi all,
   i am getting some problems with my first socket
script. can any one of you point me why this is
happening. the server script suppose to accept one
connection at a time and send countdown to client
connected to it from a function through a separate
thread. and echo all the connections opened and
closed. the client script is working fine. it echoes
the countdown it recieve from server.

### CODE for
import socket, thread
import time, sys

def handConnect(conn):
	for count in xrange(1,11):
			conn.send(str(conn.getpeername()) + ":
			print "Disconnected: ", conn.getpeername(),
	print "Closing Connection:", conn.getpeername()

def main():
	host, port = socket.gethostname(), 4786
	serversock = socket.socket(socket.AF_INET,

# According to documentation the argument to listen()
is the number of connections we can accept at one

	while True:
		conn, addr = serversock.accept()
		print "Client Connected:", addr
		thread.start_new_thread(handConnect, (conn,))

if __name__ == "__main__":

### Code for
import socket

host, port = socket.gethostname(), 4786
sock = socket.socket(socket.AF_INET,
sock.connect((host, port))

while True:
	data = sock.recv(60)
	if not data: break
	print "Recieved["+str(sock.getpeername())+"]:", data

print "Socket disconnected"

### End

Problem: when i run two instances of the i get the following output on the
console window of

### OUTPUT from
Client Connected: ('', 1336)
Client Connected: ('', 1337)
Disconnected:  ('', 1337) (10054, 'Connection
reset by peer')
Closing Connection: ('', 1337)
Disconnected:  ('', 1336) (10054, 'Connection
reset by peer')
Closing Connection: ('', 1336)
### End of Out put

as you can see two connections have been made at one
after another. and i have specify 1 in listen()
argument then why two clients can connect to it. i am
not good in english and thats why i am sorry if this
mail is long and if there are mistakes of grammar and
spellings. i have few more questions about socket
module and socket scripting, i will ask those
questions in my next mail because this mail is already
long enough. Anyways, Thanks in advance.

Hameed khan.

Do you Yahoo!?
Yahoo! Mail SpamGuard - Read only the mail you want.

More information about the Python-list mailing list