Skip to content

Instantly share code, notes, and snippets.

@jesusprubio
Created February 17, 2013 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jesusprubio/4972639 to your computer and use it in GitHub Desktop.
Save jesusprubio/4972639 to your computer and use it in GitHub Desktop.
Simple node.js UDP client written in CoffeScript which connects to an UDP server, sends a message, waits for a response, prints it and finally closes the connection.
##
# Simple node.js UDP client written in CoffeScript which
# connects to an UDP server, sends a message, waits for a
# response, prints it and finally closes the connection.
#
# Copyright (c) Jesús Pérez
# Licensed under GPLv3 - http://www.gnu.org/licenses/gpl-3.0.html
##
dgram = require "dgram"
# server info and message
targetPort = <SERVER_PORT>
targetIp = <SERVER_IP>
message = new Buffer "My KungFu is Good!"
# UDP socket is created
client = dgram.createSocket "udp4"
# event to log each received packet
client.on "message", (msg, rinfo) ->
console.log "client got: " + msg + " from " + rinfo.address + ":" + rinfo.port
# close the connection
client.close()
# message is sent
client.send message, 0, message.length, targetPort, targetIp, (err, bytes) ->
throw err if err
console.log "UDP message sent to " + targetIp + ":" + targetPort
console.log "content: " + message
# close the connection
#client.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment