Why does the message send only once?

danfolkes danfolkes at gmail.com
Tue Oct 16 11:55:31 EDT 2007


Why does the message send only once?

The node sends once, then fails after that.

<code>
import socket
import thread
import time

def Node(nodeAddress):

    '''connect to node and get its file load'''

    sN = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # Echo client program
    HOST = nodeAddress        # The remote host
    PORT = 50007              # The same port as used by the server
    sN = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sN.connect((HOST, PORT))
    sN.send('Hello, world')
    data = sN.recv(1024)
    sN.close()
    print 'Received', repr(data)

def Server(address):

    ''' starts a socket server and decides what to do with incomming
stuff'''
    message = "hello from server"
    HOST = ''                 # Symbolic name meaning the local host
    PORT = 50007              # Arbitrary non-privileged port
    sP = None
    sL = None
    sR = None
    sS = None
    sStor = 0
    sTotalStorage = 0
    sCT = (sP,)
    '''look in server.conf for connect_to_server value'''

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((HOST, PORT))
    s.listen(1)
    conn, addr = s.accept()
    print 'Connected by', addr
    while 1:
        print message
        time.sleep(1)
        data = conn.recv(1024)
        #if not data: break
        conn.send(data)
    conn.close()

thread.start_new(Server,('',))
thread.start_new(Node,('127.0.0.1',))
while 1:
    time.sleep(5)
    thread.start_new(Node,('127.0.0.1',))
</code>




More information about the Python-list mailing list