SocketServer broadcast

O

Olivier Hoarau

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.20.73'
PORT=10000

class BroadcastAcquisition(SocketServer.UDPServer,
SocketServer.ThreadingMixIn):
def __init__(self, address, handler):
self.allow_reuse_address = 1
SocketServer.UDPServer.__init__(self, address, handler)

class Handler(SocketServer.DatagramRequestHandler):
def handle(self):
print self.packet

s = BroadcastAcquisition(('', 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.20.73'
PORT=10000

class Broadcast(SocketServer.UDPServer, SocketServer.ThreadingMixIn):
def __init__(self, address, handler):
self.allow_reuse_address = 1
SocketServer.UDPServer.__init__(self, address, handler)

class Handler(SocketServer.DatagramRequestHandler):
def handle(self):
contenu=time.ctime(time.time())
self.send(contenu)
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 !!
 
T

Torsten Rueger

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.

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
 
O

Olivier Hoarau

Torsten Rueger a écrit :
If so, could you post it ?

Of course

The server

import socket
import sys
import time

GROUP = '192.168.20.255'
HOST='192.168.20.73'
PORT=10000

print "Port diffusion en fonction",PORT

service = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
service.setsockopt( socket.SOL_SOCKET, socket.SO_BROADCAST, 1 )
service.connect((GROUP,PORT))

while 1:
contenu=time.ctime(time.time())
service.send(contenu)
print "Message envoye"
time.sleep(1)

service.close()

And the client

import socket
import sys

host='192.168.20.73'
port=10000

s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))

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

s.close()

I hope it will help


Olivier
 
T

Torsten Rueger

Of course

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top