Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
What for?
to be doing any programming on an IP
network
understanding of what is happening down at
these low levels will serve you very well :
fetching pages from a web server
sending complicated queries to an industrial
database.
Watch out!!
The deployment of UDP is even rather
dangerous for the general health of the IP
network.
Instance method
sock.bind( (adrs, port) ) Bind the socket to the address and port
sock.accept() Return a client socket (with peer address information)
sock.listen(backlog) Place the socket into the listening state, able topend
backlog outstanding connection requests
sock.connect( (adrs, port) ) Connect the socket to the defined host and port
sock.recv( buflen[, flags] ) Receive data from the socket, up to buflen bytes
sock.recvfrom( buflen[, flags] ) Receive data from the socket, up to buflen
bytes, returning also the remote host and port from which the data came
sock.send( data[, flags] ) Send the data through the socket
sock.sendto( data[, flags], addr ) Send the data through the socket
sock.close() Close the socket
sock.getsockopt( lvl, optname ) Get the value for the specified socket option
sock.setsockopt( lvl, optname, val ) Set the value for the specified socket
option
Sockets
Python exposes the normal POSIX calls for raw
UDP and TCP connections rather than trying to
invent any of its own
And the normal POSIX networking calls operate
around a central concept called a socket.
Sockets provide the same idea for the networking
realm:
when you ask for access to a line of communication
you create one of these abstract socket objects and
then ask for it to be bound to the port you want to
use.
server
client
if sys.argv[1:] == ['server']:
s.bind(('127.0.0.1', PORT))
print 'Listening at', s.getsockname()
while True:
data, address = s.recvfrom(MAX)
print 'The Client ar', address, 'sys', repr(data)
s.sendto('your data was %d bytes' % len(data), address)
elif sys.argv[1:] == ['client']:
print 'Address before sending:', s.getsockname()
s.sendto(' hi this is my message', ('127.0.0.1', PORT))
print 'Address after sending', s.getsockname()
data, address = s.recvfrom(MAX)
print 'The server', address, 'says', repr(data)
Unreliability, Backoff,
Blocking, Timeouts
So how do we write a real UDP client, one that
has to deal with the fact that packets might be
lost?
This difficult choice is necessary because there is
generally no way for the client to distinguish
between three quite different events:
The reply is taking a long time to come back, but will
soon arrive.
The reply will never arrive because it, or the request,
was lost.
The server is down and is not replying to anyone.
server
client
Binding to Interface
Bind()
Ethernet
Running udp_remote.py server
Running udp_remote.py server someip
Running udp_remote.py client someip
UDP Fragmentation
UDP lets you, as a user, send raw network packets
to which just a little bit of information
your Ethernet or wireless card can only handle
packets of around 1,500 bytes instead
How to send packet over it(example 64 kb)
The actual truth is that IP sends small UDP
packets as single packets on the wire, but splits
up larger UDP packets into several small physical
packets
MTU
Create big_sender.py
Running big_sender.py somename
Socket option
Python calls getattr() and setattr()
Many options are specific to particular
operating systems
SO_BROADCAST:
SO_DONTROUTE:
SO_TYPE
broadcast
Broadcast vs Multicast
Create udp_broadcast.py