Trying to Send Repeated Messages to a Server Using sockets

danfolkes danfolkes at gmail.com
Tue Oct 16 15:35:28 CEST 2007


Hey Everyone, I am trying to send repeated messages from a "Node" to a
"Server".  It works the first time I send the from the Node to Server,
but after that it either errors, or does not do anything.

I would love some help,  here is the 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',))




More information about the Python-list mailing list