Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I believe the issue here is the use of <code>RandomPort</code>. Each client and the server need to be sending/receiving on the same port for this to work. Also, the <code>for ClientIP in ClientIPList(): if ClientIP == IP[0] and data == 'Done':</code> loop is a little redundant and unnecessary. It can be replaced with <code>if ip[0] in clientIpList:</code> and placed inside the <code>if data == 'Done':</code> above it.</p> <p>A few other thoughts; never name a variable the same name as something you have imported (like <code>socket = socket.socket(..)</code>) because then you will not be able to use the imported library anymore. And unless the client/server are both running on the same system or within the same sub-net, <code>settimeout(0.5)</code> is way to short. </p> <p>I merged your code with some <a href="http://docs.python.org/library/socket.html#example" rel="nofollow">example code</a> from the <a href="http://docs.python.org/library/socket.html" rel="nofollow">python <code>socket</code> documentation</a> and came up with something that works that you should be able to easily adapt for your needs. Here are the scripts; the output from running the server and 12 clients is pasted below.</p> <p>server.py:</p> <pre><code>#!/usr/bin/python # server.py import sys import socket import time HOST = '' PORT = 50008 CLIENT_IPS = ["10.10.1.11"] ## No longer necessary if the nested loop isn't needed #class MyException(Exception): # pass def main(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind((HOST, PORT)) sock.listen(0) finishedCount = 0 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: finishedCount += 1 print "%s: Finished Count: '%d'" % (time.ctime(), finishedCount) if finishedCount == 12: #raise MyException break except Exception, e: print "%s: Server Exception - %s" % (time.ctime(), e) #except MyException: # print "%s: All clients accounted for. Server ending, goodbye!" % time.ctime() # break # close down the socket, ignore closing exceptions try: sock.close() except: pass print "%s: All clients accounted for. Server ending, goodbye!" % time.ctime() if __name__ == '__main__': sys.exit(main()) </code></pre> <p>client.py:</p> <pre><code>#!/usr/bin/python # client.py import sys import time import socket import random HOST = '10.10.1.11' PORT = 50008 def main(n): while 1: try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) s.send('Done') print "%s: Client %d: Sending - 'Done'.." % (time.ctime(), n) data = s.recv(1024) print "%s: Client %d: Recieved - '%s'" % (time.ctime(), n, data) if data == 'Thanks': break except Exception, e: print "%s: Client %d: Exception - '%s'" % (time.ctime(), n, e) time.sleep(2 + random.random() * 5) finally: try: s.shutdown(socket.SHUT_RDWR) except: pass finally: try: s.close() except: pass print "%s: Client %d: Finished, goodbye!" % (time.ctime(), n) if __name__ == '__main__': if len(sys.argv) &gt; 1 and sys.argv[1].isdigit(): sys.exit(main(int(sys.argv[1]))) </code></pre> <p>Output from running 12 Clients:</p> <pre><code>[ 10:52 jon@hozbox.com ~/SO/python ]$ for x in {1..12}; do ./client.py $x &amp;&amp; sleep 2; done Fri Nov 18 10:52:44 2011: Client 1: Sending - 'Done'.. Fri Nov 18 10:52:44 2011: Client 1: Recieved - 'Thanks' Fri Nov 18 10:52:44 2011: Client 1: Finished, goodbye! Fri Nov 18 10:52:46 2011: Client 2: Sending - 'Done'.. Fri Nov 18 10:52:46 2011: Client 2: Recieved - 'Thanks' Fri Nov 18 10:52:46 2011: Client 2: Finished, goodbye! Fri Nov 18 10:52:48 2011: Client 3: Sending - 'Done'.. Fri Nov 18 10:52:48 2011: Client 3: Recieved - 'Thanks' Fri Nov 18 10:52:48 2011: Client 3: Finished, goodbye! Fri Nov 18 10:52:50 2011: Client 4: Sending - 'Done'.. Fri Nov 18 10:52:50 2011: Client 4: Recieved - 'Thanks' Fri Nov 18 10:52:50 2011: Client 4: Finished, goodbye! Fri Nov 18 10:52:52 2011: Client 5: Sending - 'Done'.. Fri Nov 18 10:52:52 2011: Client 5: Recieved - 'Thanks' Fri Nov 18 10:52:52 2011: Client 5: Finished, goodbye! Fri Nov 18 10:52:54 2011: Client 6: Sending - 'Done'.. Fri Nov 18 10:52:54 2011: Client 6: Recieved - 'Thanks' Fri Nov 18 10:52:54 2011: Client 6: Finished, goodbye! Fri Nov 18 10:52:56 2011: Client 7: Sending - 'Done'.. Fri Nov 18 10:52:56 2011: Client 7: Recieved - 'Thanks' Fri Nov 18 10:52:56 2011: Client 7: Finished, goodbye! Fri Nov 18 10:52:58 2011: Client 8: Sending - 'Done'.. Fri Nov 18 10:52:58 2011: Client 8: Recieved - 'Thanks' Fri Nov 18 10:52:58 2011: Client 8: Finished, goodbye! Fri Nov 18 10:53:01 2011: Client 9: Sending - 'Done'.. Fri Nov 18 10:53:01 2011: Client 9: Recieved - 'Thanks' Fri Nov 18 10:53:01 2011: Client 9: Finished, goodbye! Fri Nov 18 10:53:03 2011: Client 10: Sending - 'Done'.. Fri Nov 18 10:53:03 2011: Client 10: Recieved - 'Thanks' Fri Nov 18 10:53:03 2011: Client 10: Finished, goodbye! Fri Nov 18 10:53:05 2011: Client 11: Sending - 'Done'.. Fri Nov 18 10:53:05 2011: Client 11: Recieved - 'Thanks' Fri Nov 18 10:53:05 2011: Client 11: Finished, goodbye! Fri Nov 18 10:53:07 2011: Client 12: Sending - 'Done'.. Fri Nov 18 10:53:07 2011: Client 12: Recieved - 'Thanks' Fri Nov 18 10:53:07 2011: Client 12: Finished, goodbye! [ 10:53 jon@hozbox.com ~/SO/python ]$ </code></pre> <p>Output from server running at the same time:</p> <pre><code>[ 10:52 jon@hozbox.com ~/SO/python ]$ ./server.py Fri Nov 18 10:52:44 2011: Server recieved: 'Done' Fri Nov 18 10:52:44 2011: Server sending: 'Thanks' Fri Nov 18 10:52:44 2011: Finished Count: '1' Fri Nov 18 10:52:46 2011: Server recieved: 'Done' Fri Nov 18 10:52:46 2011: Server sending: 'Thanks' Fri Nov 18 10:52:46 2011: Finished Count: '2' Fri Nov 18 10:52:48 2011: Server recieved: 'Done' Fri Nov 18 10:52:48 2011: Server sending: 'Thanks' Fri Nov 18 10:52:48 2011: Finished Count: '3' Fri Nov 18 10:52:50 2011: Server recieved: 'Done' Fri Nov 18 10:52:50 2011: Server sending: 'Thanks' Fri Nov 18 10:52:50 2011: Finished Count: '4' Fri Nov 18 10:52:52 2011: Server recieved: 'Done' Fri Nov 18 10:52:52 2011: Server sending: 'Thanks' Fri Nov 18 10:52:52 2011: Finished Count: '5' Fri Nov 18 10:52:54 2011: Server recieved: 'Done' Fri Nov 18 10:52:54 2011: Server sending: 'Thanks' Fri Nov 18 10:52:54 2011: Finished Count: '6' Fri Nov 18 10:52:56 2011: Server recieved: 'Done' Fri Nov 18 10:52:56 2011: Server sending: 'Thanks' Fri Nov 18 10:52:56 2011: Finished Count: '7' Fri Nov 18 10:52:58 2011: Server recieved: 'Done' Fri Nov 18 10:52:58 2011: Server sending: 'Thanks' Fri Nov 18 10:52:58 2011: Finished Count: '8' Fri Nov 18 10:53:01 2011: Server recieved: 'Done' Fri Nov 18 10:53:01 2011: Server sending: 'Thanks' Fri Nov 18 10:53:01 2011: Finished Count: '9' Fri Nov 18 10:53:03 2011: Server recieved: 'Done' Fri Nov 18 10:53:03 2011: Server sending: 'Thanks' Fri Nov 18 10:53:03 2011: Finished Count: '10' Fri Nov 18 10:53:05 2011: Server recieved: 'Done' Fri Nov 18 10:53:05 2011: Server sending: 'Thanks' Fri Nov 18 10:53:05 2011: Finished Count: '11' Fri Nov 18 10:53:07 2011: Server recieved: 'Done' Fri Nov 18 10:53:07 2011: Server sending: 'Thanks' Fri Nov 18 10:53:07 2011: Finished Count: '12' Fri Nov 18 10:53:07 2011: All clients accounted for. Server ending, goodbye! [ 10:53 jon@hozbox.com ~/SO/python ]$ </code></pre>
 

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