By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,813 Members | 1,069 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,813 IT Pros & Developers. It's quick & easy.

confusion and problems with first socket script

P: n/a
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
D:\Python\workspace\practice\socket>socketserver.p y
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.

Jul 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.