On the Node.js side, I'm simply binding to the Datagram Socket to listen for messages. once received, I send back a new (and completely distinct) message.
Server.js - Our Node.js UDP Server
// Get our Datagram library and create our UDP socket;
// I think you can think of this as being somewhat akin to Java's java.net.DatagramSocket library.
var socket = require( "dgram" ).createSocket( "udp4" );
// Listen for message events on the socket.
socket.on(
"message",
function ( message, requestInfo ) {
// Log the received message.
console.log(
"Message: " + message + " from " +
requestInfo.address + ":" + requestInfo.port
);
var response = new Buffer( "Got it on " + new Date() );
// Send a response. Note that this is entirely optional.
// The client (ColdFusion) is not waiting for a response
// [necessarily]. This is an independent action and will
// not hold up the client's message.
socket.send(
response,
0, // Buffer offset
response.length,
requestInfo.port,
requestInfo.address,
function( error, byteLength ) {
console.log( "... Sent response to " + requestInfo.address + ":" + requestInfo.port );
}
);
}
);
// Listen for error events on the socket. When we get an error, we
// want to be sure to CLOSE the socket; otherwise, it's possible that
// we won't be able to get it back without restarting the process.
socket.on(
"error",
function ( error ) {
socket.close();
}
);
// When the socket is configured and ready to receive data, simply
// log a confirmation to the console.
socket.on(
"listening",
function () {
var address = socket.address();
console.log( "socket listening " + address.address + ":" + address.port );
}
);
// Start listening on the given port. Since we are not binding to
// an explicit address [just a port], Node.js will aattempt to listen
// to all addresses on the machine.
socket.bind( 9002 );
'개발 > 프로그래밍(일반)' 카테고리의 다른 글
[WAS] Tomcat 7 JNDI DataSource+Spring framework 연동 (0) | 2016.07.21 |
---|---|
[Java]BlockingQueue을 이용한 생산자 소비자 패턴 (0) | 2014.08.19 |
vi 명령어 (0) | 2014.07.10 |
TCL (exp 스크립트) (0) | 2014.06.19 |
[스크랩] [Oracle Pro*C 실무 프로젝트 활용서] Part 1. Proc*C 개요 (0) | 2014.04.22 |