Skip to content

Instantly share code, notes, and snippets.

@peace2048
Created June 12, 2015 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peace2048/dcbacc37ff4058bf79ee to your computer and use it in GitHub Desktop.
Save peace2048/dcbacc37ff4058bf79ee to your computer and use it in GitHub Desktop.
open System.Net
open System.Net.Sockets
type IpMessenger (port: int) =
let client = new UdpClient(port)
let random = new Random(Environment.TickCount)
let nextId = fun () -> random.Next(0, 1000)
let sendBuffer (address: IPAddress, message: Byte[]) =
client.Send(message, message.Length, new IPEndPoint(address, 2425))
let makeMessage (command: int, message: string) =
String.Format("1:{0}:sbot:VM-OCHIAI8:{1}:{2}", nextId(), command, message)
member x.send (address: IPAddress, command: int, ?message: String) =
let buffer = Encoding.UTF8.GetBytes(makeMessage(command, defaultArg message String.Empty))
sendBuffer(address, buffer)
interface IDisposable with
member x.Dispose() = client.Close()
using (new IpMessenger(12425)) (fun (ipmsg: IpMessenger) ->
(* 自分をブロードキャストで通知 別に要らないです
ipmsg.send(IPAddress.Broadcast, 1) |> ignore
*)
ipmsg.send(IPAddress.Loopback, 32, "Hello") |> ignore)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment