#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2014
    Posts
    19
    Rep Power
    0

    Sending messages over UDP


    Hi all,

    I'm trying to make a small "application" that will send text over UDP and then display it on netcat. FYI, I'm forced to use Python 2.6, because reasons...

    So, this is how the "server" looks like:
    Code:
    nc -4 -u -l 30000
    And this is the client:
    Code:
    # send_nc.py:
    
    # HOW TO RUN OVER UDP:
    #  $ python send_nc.py -u
    
    import random
    import socket
    import time
    import sys 
    
    def dispatch_message(message_body):
      addr = ("localhost", 30000)
    
      if len(sys.argv) > 1:
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.sendto(message_body, addr)
      else:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect(addr)
        sock.sendall(message_body)
        sock.close()
    
    def send_message():
      message_body = " Current time:" + time.ctime() + "\n\n"
      message_body = message_body + " Value 1          | Value 2             | Value 3\n"
      message_body = message_body + " -----------------+---------------------+------------------------\n"
      message_body = message_body + " aaaaaaaaaaa      | rrrrrrrrrrr         | 238\n"
      message_body = message_body + " bbbbbbbbbbb      | yyyyyyyyyyy         | 7\n"
      message_body = message_body + " ccccccccccc      | rrrrrrrrrrr         | -82.3\n"
      message_body = message_body + " ddddddddddd      | yyyyyyyyyyy         | 64\n\n"
    
      dispatch_message(message_body)
    
    def main():
      send_message()
    
    if __name__ == "__main__":
      main()
    I run the script with the -u flag.

    On the "server", I'm expecting to see a new message being displayed each time that I run the script. However, I see this message just once and that's all!! Why? Am I missing something about UDP? If so, what is it?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2014
    Posts
    19
    Rep Power
    0
    I meant to say that I run the script once and I see the desired result on the "server" that is netcat. Ok, that's good. I run the script a second time and I see nothing being displayed on the "server" side of things.

    I can't seem to edit the original post, hence this post.

IMN logo majestic logo threadwatch logo seochat tools logo