.net - Determine remote device endpoint UDP Clojure CLR -
trying perform equivalent c# code in clojure clr
using system.net; ipendpoint sender = new ipendpoint(ipaddress.any, 0); endpoint remote = (endpoint) sender; recv = sock.receivefrom(data, ref remote);
what i've tried in clojure not work:
(let [ sender (ipendpoint. (ipaddress/any) 0) ^endpoint remote ^endpoint sender recv (.receivefrom sock data (by-ref remote)) ] (println (.tostring remote)) ;; data... )
it shows 0.0.0.0:0 i'm thinking ref not working not sure of hint / cast syntax.
i've looked @ here info on ref https://github.com/richhickey/clojure-clr/wiki/clr-interop , here specifying types: https://github.com/clojure/clojure-clr/wiki/specifying-types
i translated msdn article cited in comments on question.
(ns test.me (:import [system.net iphostentry dns ipendpoint endpoint ipaddress] [system.net.sockets socket sockettype protocoltype])) (set! *warn-on-reflection* true) (defn f [] (let [hostentry (dns/gethostentry (dns/gethostname)) endpoint (ipendpoint. ^ipaddress (first (.addresslist hostentry)) 11000) sender (ipendpoint. (ipaddress/ipv6any) 0) msg (byte-array 256)] (with-open [s (socket. (.addressfamily (.address endpoint)) sockettype/dgram protocoltype/udp)] (.bind s endpoint) (.receivefrom s msg (by-ref sender)) (println sender))))
running yields:
>clojure.main.exe clojure 1.7.0-master-snapshot user=> (load "/test/me") nil user=> (in-ns 'test.me) #object[namespace 0xaca85c "test.me"] test.me=> (f) #object[ipendpoint 0x2126697 [fefe::3030:cfdf:f7f7:ecec%11]:55056] nil
(ip address edited)
you might need adjust ipaddress/ipv6any
system.
the with-open
form used automatically close socket upon exit.
note few type hints (one, actually) needed. have turned on reflection warnings -- that's best way determine if need more hints. (in case, without ^ipaddress
type hint, loading fails error because cannot resolve ipendpoint
constructor.)