글 작성자: 써니루루



참고 URL : http://ourcstory.tistory.com/67?category=664896



TCP_SERVER.JS



/**

 * http://usejsdoc.org/

 */


// http://ourcstory.tistory.com/67?category=664896


var net = require('net');


var server = net.createServer(function(socket){

    console.log(socket.address().address + " connected.");


    // client request

    socket.on('data', function(data) {

        console.log('rcv:'+data)

    });

    

    // client disconnect

    socket.on('close', function() {

        console.log('client disconnected');

    });


    socket.write('Welcome to server!');

});


server.on('error', function(err) {

    console.log('err: ' + err);

});


server.listen(5000, function() {

    console.log('listening on 5000...');

});







TCP_CLIENT.js


/**

 * http://usejsdoc.org/

 */


// http://ourcstory.tistory.com/67?category=664896


var net = require('net');


var socket = net.connect({port: 5000});


socket.on('connect', function() {

    console.log('connected to server');


    setInterval(function() {

        socket.write('banana!');

    }, 1000);

});


socket.on('data', function(data) {

    console.log('recv: ' + data);

});


socket.on('end', function() {

    console.log('disconnected');

});


socket.on('error', function(err){

    console.log('err :', err);

});


socket.on('timeout', function() {

    console.log('connection timeout');

});