H
Hameed Khan
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 socketserver.py
import socket, thread
import time, sys
def handConnect(conn):
for count in xrange(1,11):
try:
conn.send(str(conn.getpeername()) + ":
Counting..."+str(count)+"\n")
except:
print "Disconnected: ", conn.getpeername(),
sys.exc_info()[1]
break
time.sleep(2)
print "Closing Connection:", conn.getpeername()
conn.close()
def main():
host, port = socket.gethostname(), 4786
serversock = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
serversock.bind((host,port))
serversock.listen(1)
# According to documentation the argument to listen()
is the number of connections we can accept at one
time.
while True:
conn, addr = serversock.accept()
print "Client Connected:", addr
thread.start_new_thread(handConnect, (conn,))
if __name__ == "__main__":
main()
### Code for socketclient.py
import socket
host, port = socket.gethostname(), 4786
sock = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
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
socketclien.py i get the following output on the
console window of socketserver.py
### OUTPUT from socketserver.py
D:\Python\workspace\practice\socket>socketserver.py
Client Connected: ('10.10.1.8', 1336)
Client Connected: ('10.10.1.8', 1337)
Disconnected: ('10.10.1.8', 1337) (10054, 'Connection
reset by peer')
Closing Connection: ('10.10.1.8', 1337)
Disconnected: ('10.10.1.8', 1336) (10054, 'Connection
reset by peer')
Closing Connection: ('10.10.1.8', 1336)
^C
D:\Python\workspace\practice\socket>
### 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.
http://antispam.yahoo.com/tools
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 socketserver.py
import socket, thread
import time, sys
def handConnect(conn):
for count in xrange(1,11):
try:
conn.send(str(conn.getpeername()) + ":
Counting..."+str(count)+"\n")
except:
print "Disconnected: ", conn.getpeername(),
sys.exc_info()[1]
break
time.sleep(2)
print "Closing Connection:", conn.getpeername()
conn.close()
def main():
host, port = socket.gethostname(), 4786
serversock = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
serversock.bind((host,port))
serversock.listen(1)
# According to documentation the argument to listen()
is the number of connections we can accept at one
time.
while True:
conn, addr = serversock.accept()
print "Client Connected:", addr
thread.start_new_thread(handConnect, (conn,))
if __name__ == "__main__":
main()
### Code for socketclient.py
import socket
host, port = socket.gethostname(), 4786
sock = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
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
socketclien.py i get the following output on the
console window of socketserver.py
### OUTPUT from socketserver.py
D:\Python\workspace\practice\socket>socketserver.py
Client Connected: ('10.10.1.8', 1336)
Client Connected: ('10.10.1.8', 1337)
Disconnected: ('10.10.1.8', 1337) (10054, 'Connection
reset by peer')
Closing Connection: ('10.10.1.8', 1337)
Disconnected: ('10.10.1.8', 1336) (10054, 'Connection
reset by peer')
Closing Connection: ('10.10.1.8', 1336)
^C
D:\Python\workspace\practice\socket>
### 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.
http://antispam.yahoo.com/tools