indexing description: "[ Classic UDP Network example. This small application implements something like a very simple chat. ]" date: "$Date$" revision: "$Revision$" class UDP_CLASSIC inherit EM_APPLICATION EM_SHARED_ERROR_HANDLER create make feature -- Initialisation make (arguments: ARRAY[STRING]) is -- Init local remote_host: STRING local_port,remote_port: INTEGER do -- first we have to enable the network subsystem network_subsystem.enable --check wheter it worked or not if network_subsystem.is_enabled then -- now we do all sorts of input checking if arguments.count > 2 then if arguments.item (1).is_integer then local_port := arguments.item(1).to_integer if arguments.item (3).is_integer then remote_port := arguments.item (3).to_integer remote_host := arguments.item (2) -- launch the main routine of our programm run (local_port,remote_host,remote_port) else io.put_string ("remote port (" + arguments.item(3)+ ") is not an integer!%N") end else io.put_string ("local port (" + arguments.item(1)+ ") is not an integer!%N") end else io.put_string ("usage: udp_classic %NNext you may type a message,%Nwhich will be sent to the remote host after you hit enter.%N") end else io.put_string ("error: could not enable network subsystem.%N") end end feature --Example run (local_port: INTEGER; remote_host: STRING; remote_port: INTEGER) is -- run the sample application local data: STRING failed: BOOLEAN do if not failed then create destination.make_by_hostname_port (remote_host, remote_port) destination.resolve_blocking if not destination.is_resolved then error_handler.raise_error (error_handler.Em_error_resolve_ip, []) else io.put_string (destination.hostname + " has been resolved to: " + destination.host_ip_as_string) io.put_new_line end create packet.make create socket.make create data.make_empty packet.set_address (destination) socket.open (local_port) from until data.is_equal ("quit") loop from until not socket.is_packet_ready loop socket.receive_packet received_packet := socket.last_packet received_packet.address.reverse_resolve_blocking if received_packet.address.is_reverse_resolved then io.put_string (received_packet.address.hostname + ":") else io.put_string (received_packet.address.host_ip_as_string + ":") end io.put_integer (received_packet.count) io.put_string (" >> ") io.put_string (received_packet.item) io.put_new_line end io.put_string ("input>> ") io.read_line data := io.last_string packet.put_string (data) socket.send (packet) end socket.close end rescue -- error-handling if error_handler.last_error_code = error_handler.em_error_udp_open then io.put_string ("error opening udp socket%N") elseif error_handler.last_error_code = error_handler.em_error_udp_send then io.put_string ( "error sending udp packet%N") elseif error_handler.last_error_code = error_handler.em_error_udp_recv then io.put_string ("error receving udp packet%N") else io.put_string ("unknown error: something went wrong...%N") end failed := true retry end feature {NONE} -- Implementation destination: EM_INET_SOCKET_ADDRESS -- Used as destination address of the packets we send packet: EM_UDP_PACKET -- a packet received_packet: EM_UDP_PACKET socket: EM_SIMPLE_UDP_SOCKET end