Trying to Send Repeated Messages to a Server Using sockets

Discussion in 'Python' started by danfolkes, Oct 16, 2007.

  1. danfolkes

    danfolkes Guest

    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',))
     
    danfolkes, Oct 16, 2007
    #1
    1. Advertising

  2. danfolkes a écrit :
    > 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:


    Posting the trackbacks may have help too.

    > import socket
    > import thread
    > import time
    >
    >
    > def Node(nodeAddress):


    pep08:
    - function names should be all_lower. MixedCase is for class names.
    - variable names should be all_lower

    > '''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


    These two last lines are useless here. The second would be meaningful
    outside the function's body. The first one looks like a constant, but is
    only an alias for the nodeAddress param.

    > sN = socket.socket(socket.AF_INET, socket.SOCK_STREAM)


    This discards the first socket instanciated above...

    > sN.connect((HOST, PORT))
    > sN.send('Hello, world')
    > data = sN.recv(1024)
    > sN.close()
    > print 'Received', repr(data)
    >
    > def Server(address):


    pep08 again.

    > ''' 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


    Idem. But this time, you just don't use the 'address' param...

    > sP = None
    > sL = None
    > sR = None
    > sS = None
    > sStor = 0
    > sTotalStorage = 0
    > sCT = (sP,)


    None of those very badly named variables are used in the code.

    > '''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()


    Hem...

    > print 'Connected by', addr
    > while 1:
    > print message
    > time.sleep(1)
    > data = conn.recv(1024)
    > #if not data: break
    > conn.send(data)


    Your server loops forever on the first connection it got...


    > 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',))
    >


    Here's a somewhat corrected version. I'm not sure it's an example to
    follow wrt/ threaded server implementation (it's the first time I write
    one myself), but at least it works.

    import socket
    import thread
    import time

    HOST = ''
    PORT = 50007

    def node(host, num):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((host, PORT))
    print "node %s: sending" % num
    s.send("node '%s' says : Hello, world" % num)
    time.sleep(10) # play with this to see how the server reacts...
    print "node %s: receiving" % num
    data = s.recv(1024)
    s.close()
    print "node %s: received '%s'" % (num, data)
    print "node %s: done" % num


    def server(address):
    thread_ids = []

    def handle(conn, addr):
    tid = thread.get_ident()
    print "server (handle %s): handling connection for %s" % (tid,
    str(addr))

    print "server (handle %s): receiving" % tid
    data = conn.recv(1024)
    while data:
    print "server (handle %s): got '%s'" % (tid, data)
    print "server (handle %s): sending '%s'" % (tid, data)
    conn.send(data)
    time.sleep(2)
    print "server (handle %s): receiving" % tid
    data = conn.recv(1024)

    print "server (handle %s): no more data for %s, closing" %
    (tid, str(addr))
    conn.close()
    thread_ids.remove(tid)


    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((HOST, PORT))
    s.listen(10)

    while 1:
    print "server: active handler threads : %s" % thread_ids
    nb_handlers = len(thread_ids)
    while nb_handlers > 10:
    print "server: too much connections, waiting for current
    handlers to terminate"
    time.sleep(5)
    nb_handlers = len(thread_ids)

    print "server: accepting connections - actually %s handlers" %
    nb_handlers
    conn, addr = s.accept()
    print "server: connected by", addr
    print "server: starting a new handler thread for connection %s"
    % nb_handlers
    thread_ids.append(thread.start_new(handle, (conn, addr)))
    time.sleep(1)

    def main():
    print "starting server..."
    thread.start_new(server,('',))

    num = 1
    while 1:
    time.sleep(1)
    print "new node %s..." % num
    thread.start_new(node,('127.0.0.1',num))
    num += 1

    if __name__ == '__main__':
    main()
     
    Bruno Desthuilliers, Oct 16, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. zorhel
    Replies:
    5
    Views:
    599
    Marina
    Aug 24, 2004
  2. Thomas Schulz

    logging: repeated messages

    Thomas Schulz, Nov 19, 2003, in forum: Python
    Replies:
    2
    Views:
    460
    Peter Otten
    Nov 19, 2003
  3. Robert Dodier
    Replies:
    4
    Views:
    333
    Raymond Hettinger
    Mar 14, 2006
  4. SammyBar
    Replies:
    3
    Views:
    12,245
    twebb72
    Oct 18, 2009
  5. Kenosis
    Replies:
    1
    Views:
    93
    Kenosis
    Jan 13, 2006
Loading...

Share This Page