Created
February 17, 2013 18:21
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# 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