473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

confusion and problems with first socket script

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.p y
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

Jul 18 '05 #1
0 1577

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John Hunter | last post by:
I have a test script below which I use to fetch urls into strings, either over https or http. When over https, I use m2crypto.urllib and when over http I use the standard urllib. Whenever, I...
0
by: Jacob Lee | last post by:
I'm getting a rather bizarre error while using the socket module. If I start out disconnected from the net and then connect while the program or interpreter session is open, I do not always gain...
1
by: VB | last post by:
Dear friends, I have a big problem. With an usersonline script I need to extract in php the output of a cgi using buffering output in this way: <?php ob_start();...
0
by: Robert Björn | last post by:
I'm trying to process a MemoryStream of XML data, which at any point in time may have incomplete fragments (because data arrives from a socket). If I process data until XmlTextReader.Read()...
0
by: soaphead | last post by:
Can I use Socket.Poll for blocking socket? It is unclear in documentation if I can use this with a synchronous socket, can my code possibly turn a synchronous socket to asynchronous? It seems to...
2
by: Rick | last post by:
Hi, I'm trying to get a simple UdpClient app working. I've been looking at the MSDN info regarding UdpClient. When I set it up on my own PC and send messages to myself it works OK. If I try to...
0
by: ZR | last post by:
I am writing two applications which needs to (among other things) communicate through network, so one of them is a client and the other one is a server. I have used asynchronous socket examples...
1
by: jmalone | last post by:
I have a python script that I need to freeze on AIX 5.1 (customer has AIX and does not want to install Python). The python script is pretty simple (the only things it imports are sys and socket)....
0
by: clemima | last post by:
Hi, i have a script that listens on a port for simple text messages and then executes sql to a remote mssql database . The script is started via crontab . There are 2 problems : 1 . The line to...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.