출처 샘플 참고 사이트
http://www.hacksparrow.com/node-js-udp-server-and-client-example.html
1. UDP Server
- var PORT = 7001;
- var HOST = '127.0.0.1';
- var dgram = require('dgram');
- var server = dgram.createSocket('udp4');
- server.on('listening', function () {
- var address = server.address();
- console.log('UDP Server listening on ' + address.address + ":" + address.port);
- });
- server.on('message', function (message, remote) {
- console.log(remote.address + ':' + remote.port +' - ' + message);
- });
- server.bind(PORT, HOST);
2. UDP Client
- var PORT = 7001;
- var HOST = '127.0.0.1';
- var dgram = require('dgram');
- var message = new Buffer('TEST!');
- var client = dgram.createSocket('udp4');
- client.send(message, 0, message.length, PORT, HOST, function(err, bytes) {
- if (err) throw err;
- console.log('UDP message sent to ' + HOST +':'+ PORT);
- client.close();
- });
3. PHP UDP Client
- <?php
- $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
- $in = 'test!';
- var_dump (socket_sendto($socket, $in,strlen($in), 0,'127.0.0.1',7001));
- socket_close($socket);
- ?>