Trying to perform the 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 that does not work:
(let [
sender (IPEndPoint. (IPAddress/Any) 0)
^EndPoint remote ^EndPoint sender
recv (.ReceiveFrom sock data (by-ref remote))
]
(println (.ToString remote))
;; Do something with data...
)
It just shows 0.0.0.0:0 I'm thinking the ref is not working but also not sure of the hint / cast syntax.
I've looked at here for info on ref https://github.com/richhickey/clojure-clr/wiki/CLR-Interop And here about specifying types: https://github.com/clojure/clojure-clr/wiki/Specifying-types
I translated the MSDN article cited in the comments on the question.
Running it yields:
(IP address edited)
You might need to adjust
IPAddress/IPv6Any
for your system.The
with-open
form is used to automatically close the socket upon exit.Note that very few type hints (one, actually) are needed. I have turned on reflection warnings -- that's the best way to determine if you need more hints. (In this case, without the
^IPAddress
type hint, loading fails with an error because it cannot resolve theIPEndPoint
constructor.)