'UDP'에 해당되는 글 1건

  1. 2014.09.19 [node.js] UDP 서버 클라이언트 만들기
Programming/node.js2014. 9. 19. 11:53

출처 샘플 참고 사이트

http://www.hacksparrow.com/node-js-udp-server-and-client-example.html


1. UDP Server

  1. var PORT = 7001;
  2. var HOST = '127.0.0.1';
  3. var dgram = require('dgram');
  4. var server = dgram.createSocket('udp4');
  5. server.on('listening', function () {
  6. var address = server.address();
  7. console.log('UDP Server listening on ' + address.address + ":" + address.port);
  8. });
  9. server.on('message', function (message, remote) {
  10. console.log(remote.address + ':' + remote.port +' - ' + message);
  11. });
  12. server.bind(PORT, HOST);


2. UDP Client

  1. var PORT = 7001;
  2. var HOST = '127.0.0.1';
  3. var dgram = require('dgram');
  4. var message = new Buffer('TEST!');
  5. var client = dgram.createSocket('udp4');
  6. client.send(message, 0, message.length, PORT, HOST, function(err, bytes) {
  7. if (err) throw err;
  8. console.log('UDP message sent to ' + HOST +':'+ PORT);
  9. client.close();
  10. });


3. PHP UDP Client

  1. <?php
  2. $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
  3. $in = 'test!';
  4. var_dump (socket_sendto($socket, $in,strlen($in), 0,'127.0.0.1',7001));
  5. socket_close($socket);
  6. ?>


Posted by 시니^^