SocketServer broadcast

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Olivier Hoarau

    SocketServer broadcast

    Hello,

    I have build a client/server application with the socket module. The
    server mades UDP broadcasting and the client only reads UDP broadcast
    messages. All work fine.
    Now I want to use for the same thing the socketserver module, it's ok
    for the client, but I don't succeed in making work the server :-((

    Here is the client (which works)

    import sys
    import SocketServer

    host='192.168.2 0.73'
    PORT=10000

    class BroadcastAcquis ition(SocketSer ver.UDPServer,
    SocketServer.Th readingMixIn):
    def __init__(self, address, handler):
    self.allow_reus e_address = 1
    SocketServer.UD PServer.__init_ _(self, address, handler)

    class Handler(SocketS erver.DatagramR equestHandler):
    def handle(self):
    print self.packet

    s = BroadcastAcquis ition(('', PORT), Handler)
    s.serve_forever ()


    And the server (doesn't work)

    import sys
    import time
    import SocketServer

    GROUP = '192.168.20.255 '
    HOST='192.168.2 0.73'
    PORT=10000

    class Broadcast(Socke tServer.UDPServ er, SocketServer.Th readingMixIn):
    def __init__(self, address, handler):
    self.allow_reus e_address = 1
    SocketServer.UD PServer.__init_ _(self, address, handler)

    class Handler(SocketS erver.DatagramR equestHandler):
    def handle(self):
    contenu=time.ct ime(time.time() )
    self.send(conte nu)
    print "Message envoye"
    time.sleep(1)


    s = Broadcast(('', PORT), Handler)
    s.serve_forever ()

    I know I have to set the SO_BROADCAST attribute and the network mask for
    the broadcast but I don't know how and where in the programm.

    Olivier
    PS: Excuse for my poor english !!
  • Torsten Rueger

    #2
    Re: SocketServer broadcast

    On Dec 19, 2003, at 10:51 AM, Olivier Hoarau wrote:[color=blue]
    > I have build a client/server application with the socket module. The
    > server mades UDP broadcasting and the client only reads UDP broadcast
    > messages. All work fine.[/color]

    Are you saying you have working server client/server code that works,
    but doesn't use the SocketServer ?
    If so, could you post it ?

    I've been trying to do the same thing, though I'd be fine with it not
    using the Socketserver, but no success. Sorry.


    Torsten


    Comment

    • Olivier Hoarau

      #3
      Re: SocketServer broadcast



      Torsten Rueger a écrit :[color=blue]
      > If so, could you post it ?[/color]

      Of course

      The server

      import socket
      import sys
      import time

      GROUP = '192.168.20.255 '
      HOST='192.168.2 0.73'
      PORT=10000

      print "Port diffusion en fonction",PORT

      service = socket.socket( socket.AF_INET, socket.SOCK_DGR AM )
      service.setsock opt( socket.SOL_SOCK ET, socket.SO_BROAD CAST, 1 )
      service.connect ((GROUP,PORT))

      while 1:
      contenu=time.ct ime(time.time() )
      service.send(co ntenu)
      print "Message envoye"
      time.sleep(1)

      service.close()

      And the client

      import socket
      import sys

      host='192.168.2 0.73'
      port=10000

      s=socket.socket (socket.AF_INET , socket.SOCK_DGR AM)
      s.setsockopt(so cket.SOL_SOCKET , socket.SO_REUSE ADDR, 1)
      s.bind((host, port))

      while 1:
      t,server=s.recv from(65535)
      print t

      s.close()

      I hope it will help


      Olivier

      Comment

      • Torsten Rueger

        #4
        Re: SocketServer broadcast

        On Dec 19, 2003, at 12:44 PM, Olivier Hoarau wrote:[color=blue]
        >
        > Of course[/color]

        Thanks a bunch!

        I don't have client or servers really, just peers. So I need to have
        both client + server in the same process.

        When I do that, the client bind fails. But when I bind to '' (empty
        string) then it works. Maybe that helps you too (?)

        Anyway, it works for me now, thanks a lot

        Torsten


        Comment

        Working...