-- Contributed by Don Berryman (berryman@orca.drep.dnd.ca) -- -- This program is essentially RECEIVER with an extra call -- to FILE.CONTROL.SETFL to specify non-blocking IO. -- This program receives varing length packets sent by -- BROADCASTER over a UDP Datagram Broadcast Socket. -- It is only possible to have ONE RECEIVER per CPU, but -- each CPU on a network can have its own receiver. The -- problem is that the BIND must be to a unique address -- and port number on a particular machine. with text_io; with ERROR, DEBUG, INET, NETWORK, FILE; procedure nonblocking_receiver is package net renames network; package intio is new text_io.integer_io (natural); bufsize: constant integer := 64; subtype buffer is string(1..bufsize); procedure buf_recvfrom is new file.recvfrom (buffer, false); port: constant inet.ipport := 1456; domain: constant net.address_format := net.af_inet; sock: file.descriptor; buf: buffer := (others => ascii.nul); server: file.sockaddr (family => net.af_inet); client: file.sockaddr (family => net.af_inet); rval: natural := 0; empty: natural := 0; begin sock := file.socket (domain, file.dgram); file.sockopt.set_rcvbuf (sock, 16384); file.control.setfl (sock, file.nonblock); -- Specify non-blocking IO server.addr := inet.addr_any; server.port := port; file.bind (sock, server); loop delay (0.1); -- Slow down a little buf_recvfrom (sock, buf, bufsize, file.msg_none, client, rval); if rval = 0 then empty := empty + 1; if empty mod 100 = 0 then text_io.put_line("Waiting for data..."); end if; else text_io.put ("#EmptyReads:"); intio.put (empty, 5); text_io.put (" From " & inet.ntoa(client.addr)); text_io.set_col (30); text_io.put (" Bytes:"); intio.put (rval, 3); text_io.put (" <<<" & buf(1..rval) & ">>>"); text_io.new_line; empty := 0; end if; end loop; end nonblocking_receiver;