Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Alright, this took me a while but I think I figured out what was causing this:</p> <ol> <li>glglgl's answer is correct - using 'localhost' causes the machine to only listen to itself and not to other machines on the network. This was the main culprit.</li> <li>Increasing the number allowed in the que from 0 to 5 reduced the likelihood of getting a "connection refused" error on the client side.</li> <li><p>I made the mistake of assuming that socket connections in an infinite while loop can be shut down infinitely fast - however, having an infinite while loop on both sides sometimes caused a client to sometimes be counted twice because the while loops were not synchronized. This, of course, caused 'client-agnostic' finishedCount to increase twice which led the server the believe all clients were done when they weren't. Using chown's code (thank you chown!), this can be dealt with like this:</p> <pre><code>def main(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind((HOST, PORT)) sock.listen(0) FINISHEDCLIENTS = [] while 1: data = 'None' IP = [0, 0] try: client, ip = sock.accept() data = client.recv(1024) print "%s: Server recieved: '%s'" % (time.ctime(), data) if data == 'Done': print "%s: Server sending: 'Thanks'" % time.ctime() client.send('Thanks') if ip[0] in CLIENT_IPS and ip[0] not in FINISHEDCLIENTS: FINISHEDCLIENTS.append(ip[0]) if len(FINISHEDCLIENTS) == 12: #raise MyException break except Exception, e: print "%s: Server Exception - %s" % (time.ctime(), e) </code></pre> <p>On the client side, I changed the code to this (where of course, RandomPort is the same as the one used in the server script above): </p> <pre><code>SentFlag = 0 data = 'no' while SentFlag == 0: try: f = socket.socket(socket.AF_INET, socket.SOCK_STREAM) f.connect((IPofServer, RandomPort)) f.settimeout(20) f.send('Done') data = f.recv(1024) if data == 'Thanks': f.shutdown(socket.SHUT_RDWR) f.close() SentFlag = 1 except: f.close() time.sleep(2*random.random()) </code></pre></li> </ol> <p>PS: My understanding of .shutdown() vs .close() is that closes the connection but not necessarily the socket if it is engaged in another communication. .shutdown() shuts down the socket no matter what else it is doing. I don't have any proof for this though.</p> <p>I think that should do it - thank you all again for helping fix this code!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload