Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1042703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:33:03+00:00 2026-05-16T15:33:03+00:00

I want to create a server and client that sends and receives UDP packets

  • 0

I want to create a server and client that sends and receives UDP packets from the network using Twisted. I’ve already written this with sockets in Python, but want to take advantage of Twisted’s callback and threading features. However, I need help though with the design of Twisted.

I have multiple types of packets I want to receive, but let’s pretend there is just one:

class Packet(object):
    def __init__(self, data=None):
        self.packet_type = 1
        self.payload = ''
        self.structure = '!H6s'
        if data == None:
            return

        self.packet_type, self.payload = struct.unpack(self.structure, data)

    def pack(self):
        return struct.pack(self.structure, self.packet_type, self.payload)

    def __str__(self):
        return "Type: {0}\nPayload {1}\n\n".format(self.packet_type, self.payload)

I made a protocol class (almost direct copy of the examples), which seems to work when I send data from another program:

class MyProtocol(DatagramProtocol):
    def datagramReceived(self, data, (host, port)):
        p = Packet(data)
        print p

reactor.listenUDP(3000, MyProtocol())
reactor.run()

What I don’t know is how do I create a client which can send arbitrary packets on the network, which get picked up by the reactor:

# Something like this:
s = Sender()
p = Packet()
p.packet_type = 3
s.send(p.pack())
p.packet_type = 99
s.send(p.pack())

I also need to make sure to set the reuse address flag on the client and servers so I can run multiple instances of each at the same time on the same device (e.g. one script is sending heartbeats, another responds to heartbeats, etc).

Can someone show me how this could be done with Twisted?

Update:

This is how I do it with sockets in Python. I can run multiple listeners and senders at the same time and they all hear each other. How do I get this result with Twisted? (The listening portion need not be a separate process.)

class Listener(Process):
    def __init__(self, ip='127.0.0.1', port=3000):
        Process.__init__(self)
        self.ip = ip
        self.port = port

    def run(self):
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.bind((self.ip, self.port))

        data, from_ip = sock.recvfrom(4096)
        p = Packet(data)
        print p

class Sender(object):
    def __init__(self, ip='127.255.255.255', port=3000):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.ip = (ip, port)

    def send(self, data):
        self.sock.sendto(data, self.ip)

if __name__ == "__main__":
    l = Listener()
    l.start()
    s = Sender()
    p = Packet()
    p.packet_type = 4
    p.payload = 'jake'
    s.send(p.pack())

Working solution:

class MySender(DatagramProtocol):
    def __init__(self, packet, host='127.255.255.255', port=3000):
        self.packet = packet.pack()
        self.host = host
        self.port = port

    def startProtocol(self):
        self.transport.write(self.packet, (self.host, self.port))

if __name__ == "__main__":
    packet = Packet()
    packet.packet_type = 1
    packet.payload = 'jake'

    s = MySender(packet)

    reactor.listenMulticast(3000, MyProtocol(), listenMultiple=True)
    reactor.listenMulticast(3000, s, listenMultiple=True)
    reactor.callLater(4, reactor.stop)
    reactor.run()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:33:04+00:00Added an answer on May 16, 2026 at 3:33 pm

    Just like the server example above, there is a client example to.
    This should help you get started:

    • https://twistedmatrix.com/documents/current/core/howto/udp.html
    • https://github.com/twisted/twisted/blob/trunk/docs/core/examples/echoclient_udp.py

    Ok, here is a simple heart beat sender and receiver using datagram protocol.

    from twisted.internet.protocol import DatagramProtocol
    from twisted.internet import reactor
    from twisted.internet.task import LoopingCall
    import sys, time
    
    class HeartbeatSender(DatagramProtocol):
        def __init__(self, name, host, port):
            self.name = name
            self.loopObj = None
            self.host = host
            self.port = port
    
        def startProtocol(self):
            # Called when transport is connected
            # I am ready to send heart beats
            self.loopObj = LoopingCall(self.sendHeartBeat)
            self.loopObj.start(2, now=False)
    
        def stopProtocol(self):
            "Called after all transport is teared down"
            pass
    
        def datagramReceived(self, data, (host, port)):
            print "received %r from %s:%d" % (data, host, port)
    
    
        def sendHeartBeat(self):
            self.transport.write(self.name, (self.host, self.port))
    
    
    
    class HeartbeatReciever(DatagramProtocol):
        def __init__(self):
            pass
    
        def startProtocol(self):
            "Called when transport is connected"
            pass
    
        def stopProtocol(self):
            "Called after all transport is teared down"
    
    
        def datagramReceived(self, data, (host, port)):
            now = time.localtime(time.time())  
            timeStr = str(time.strftime("%y/%m/%d %H:%M:%S",now)) 
            print "received %r from %s:%d at %s" % (data, host, port, timeStr)
    
    
    
    heartBeatSenderObj = HeartbeatSender("sender", "127.0.0.1", 8005)
    
    reactor.listenMulticast(8005, HeartbeatReciever(), listenMultiple=True)
    reactor.listenMulticast(8005, heartBeatSenderObj, listenMultiple=True)
    reactor.run()
    

    The broadcast example simply modifies the above approach:

    from twisted.internet.protocol import DatagramProtocol
    from twisted.internet import reactor
    from twisted.internet.task import LoopingCall
    import sys, time
    
    class HeartbeatSender(DatagramProtocol):
        def __init__(self, name, host, port):
            self.name = name
            self.loopObj = None
            self.host = host
            self.port = port
    
        def startProtocol(self):
            # Called when transport is connected
            # I am ready to send heart beats
            self.transport.joinGroup('224.0.0.1')
            self.loopObj = LoopingCall(self.sendHeartBeat)
            self.loopObj.start(2, now=False)
    
        def stopProtocol(self):
            "Called after all transport is teared down"
            pass
    
        def datagramReceived(self, data, (host, port)):
            print "received %r from %s:%d" % (data, host, port)
    
    
        def sendHeartBeat(self):
            self.transport.write(self.name, (self.host, self.port))
    
    
    
    class HeartbeatReciever(DatagramProtocol):
        def __init__(self, name):
            self.name = name
    
        def startProtocol(self):
            "Called when transport is connected"
            self.transport.joinGroup('224.0.0.1')
            pass
    
        def stopProtocol(self):
            "Called after all transport is teared down"
    
    
        def datagramReceived(self, data, (host, port)):
            now = time.localtime(time.time())  
            timeStr = str(time.strftime("%y/%m/%d %H:%M:%S",now)) 
            print "%s received %r from %s:%d at %s" % (self.name, data, host, port, timeStr)
    
    
    
    heartBeatSenderObj = HeartbeatSender("sender", "224.0.0.1", 8005)
    
    reactor.listenMulticast(8005, HeartbeatReciever("listner1"), listenMultiple=True)
    reactor.listenMulticast(8005, HeartbeatReciever("listner2"), listenMultiple=True)
    reactor.listenMulticast(8005, heartBeatSenderObj, listenMultiple=True)
    reactor.run()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create an application which will have a client and server components.
I want to create light object data-package to pass between client and server applications.
I want to create a simple http proxy server that does some very basic
I want to create a trace on a database server from my C# app,
I want to create a list of columns in SQL Server 2005 that have
I'm using ActiveState Perl on Windows Server 2003. I want to create a directory
I want to create a SQL Server Express database on my local machine and
In my SQL Server backend for my app, I want to create history tables
I have a table in an MS SQL Server db. I want to create
I want to develop a multi-server clustered framework that will work similarly to the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.