'node.js'에 해당되는 글 1건

  1. 2015.06.29 node.js http/udp Server&client

Click:초보자안내사이트들

 

<설치방법:install>

1. node-xx.linux-x86.tar.gz 바이너리파일 다운로드   http://nodejs.org/download 

2. 압축풀고 README파일 참조. path와 lib path만 설정하면 될 듯한데.

# ln -s /usr/local/nodejs/v0.x.x/bin/node /usr/local/bin/node

 

 

 

<실행방법>

$> node httpSvr.js


<모듈 설치방법>

$ npm intall -g blabla   (-g 안하려면.. httpSvr.js가 있는 곳에서 실행해야 그 밑에 node_modules 폴더가 생기면서 설치됨)



==httpSvr.js=====

==> express모듈을 사용하면 웹서버가 더 쉬워짐:  참고사이트

var http = require('http');

var server = http.createServer(function (req, res) {
  res.writeHead(200, { 'Content-Type' : 'text/plain' });
  res.end('Hello World');
});

server.listen(8000);



===udpSvr.js===========================================================

var PORT = 33333;

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('Svr Log:'+remote.address + ':' + remote.port +' - ' + message);


});


server.bind(PORT, HOST);





==udpClient.js==========================================

var PORT = 33333;

var HOST = '127.0.0.1';


var dgram = require('dgram');

var message = new Buffer('My KungFu is Good!');


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();

});




==========packet분석 with cap===================


Posted by yongary
,