Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
(ns echo-server
(:import ( InetAddress DatagramPacket DatagramSocket)))
(def udp-server (ref nil))
(def port 12345)
(defn localhost [] (. InetAddress getLocalHost))
(defn message [text]
(new DatagramPacket (. text getBytes) (. text length) (localhost) port ))
(defn send-message [text]
(.send @udp-server (message text)))
(defn create-udp-server []
(DatagramSocket. port))
(defn start-udp-server []
(dosync (ref-set udp-server (create-udp-server))))
(defn stop-udp-server []
(.close @udp-server))
(defn empty-message [n]
(new DatagramPacket (byte-array n) n))
(while true (do
(def orig-packet (empty-message 1024))
(.receive @udp-server orig-packet)
(.send @udp-server orig-packet)
(println ( new String (.getData orig-packet) "UTF-8"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment