Simple UDP server

T

Tzury Bar Yochay

I am looking for the right way to write a small and simple UDP server.

I am wondering between Forking, Threading (found at SocketServer.py)
and the one describes at the snippet below.

Can you tell me the advantages and disadvantages of each
Would the one below will be capable of holding 30 concurrent
connections?

I have no intention of using Twisted or alike since I am looking for
making it as lightweight as possible

Thanks in advance,
Tzury Bar Yochay

# begin of snippet

from socket import *
# Create socket and bind to address
UDPSock = socket(AF_INET,SOCK_DGRAM)
UDPSock.bind(('',50008))

while 1:
data,addr = UDPSock.recvfrom(4*1024)

if not data:
print "No data."
break
else:
print 'from:', addr, ' data:', data
UDPSock.close()
 
F

Fredrik Lundh

Tzury said:
Would the one below will be capable of holding 30 concurrent
connections?

UDP is a connectionless datagram protocol, so that question doesn't
really make much sense.

</F>
 
T

Tzury Bar Yochay

UDP is a connectionless datagram protocol, so that question doesn't
really make much sense.

So what if it is connectionless.
It would make sense if you get a load of users who sends large sets of
binary data to each other.
 
T

Tzury Bar Yochay

Transmitting large binary data over UDP? That makes only sense for few
applications like video and audio streaming. UDP does neither guarantee
that your data is received nor it's received in order. For example the
packages A, B, C, D might be received as A, D, B (no C).

Can your protocol handle missing packages and out of order packages?

I intend of using it for audio transmission and don't care about lose
or out of order.
 
J

James Mills

Tzury,

You may consider using pymills
to simplify writing your UDP server
and just concentrating on the
behavior of the system.

You can get a copy of the
latest development branch
by cloning it with Mercurial:

hg clone http://hg.shortcircuit.net.au/pymills/

There is an example UDP Server
in examples/net/ but I'll paste
it here for your reference.

Note, as stated before, UDP is a connectionless
protocol (Datagram), concurrency doesn't apply.
Also note, technically UDP doesn't guarantee
reliability or the order of packets, however the
reality is, it doesn't give you any feedback. You
have to handle this yourself.

Have fun,

cheers
James

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: set sw=3 sts=3 ts=3

from pymills import event
from pymills.event import *
from pymills.net.sockets import UDPServer

class EchoServer(UDPServer):

@listener("connect")
def onCONNECT(self, sock, host, port):
print "New connection: %s:%d" % (host, port)

@listener("disconnect")
def onDISCONNECT(self, sock):
print "Disconnection: %s" % sock

@listener("read")
def onREAD(self, sock, line):
line = line.strip()
print "%s: %s" % (sock, line)

@listener("error")
def onERROR(self, sock, msg):
print "ERROR (%s): %s" % (sock, msg)

def main():
server = EchoServer(1234)
event.manager += server

while True:
try:
manager.flush()
server.poll()
except KeyboardInterrupt:
break

if __name__ == "__main__":
main()
 
J

James Mills

For UDP I wouldn't thread or, fork, I'd use select and run
asynchronously.

http://docs.python.org/lib/module-select.html

Actually if I really had to do this I'd use twisted. Right tool for
the job!

For anyone interested, pymills is an
event-driven, asynchronous library
geared towards Component architectures.

It currently uses select for it's socket
components, TCPServer, TCPClient,
and it's UDP counter-parts.

cheers
James
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top