nodejs:echoserverclient
차이
문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 양쪽 이전 판이전 판 | |||
| nodejs:echoserverclient [2013/07/13 20:17] – 바깥 편집기 127.0.0.1 | nodejs:echoserverclient [2013/07/17 14:41] (현재) – 삭제함 changwoo | ||
|---|---|---|---|
| 줄 1: | 줄 1: | ||
| - | ====== node.js 에코 서버와 클라이언트 제작해보기 ====== | ||
| - | node.js 기반의 에코 서버, 파이썬 기반으로 대응되는 간단한 콘솔 클라이언트를 제작해봅니다. | ||
| - | |||
| - | ===== 시놉시스 ===== | ||
| - | ==== 서버 ==== | ||
| - | - 클라이언트가 접속하면 접속하였다는 메시지를 출력합니다. | ||
| - | - 클라이언트로부터 메시지를 받으면 받은 메시지를 그대로 출력하고, | ||
| - | - 클라이언트로부터 접속이 종료되면 종료 메시지를 출력합니다. | ||
| - | |||
| - | ==== 클라이언트 ==== | ||
| - | - 서버에 접속하면 접속했다는 메시지를 출력합니다. | ||
| - | - 서버에 메시지를 보냅니다. 받은 메시지는 서버에 전달되었다가, | ||
| - | - < | ||
| - | |||
| - | ===== 서버측 구현===== | ||
| - | <code javascript echo_server.js> | ||
| - | // 필요한 객체를 선언합니다. | ||
| - | var net = require(' | ||
| - | var port = 9000; | ||
| - | |||
| - | // 소켓에 필요한 핸들러는 이 곳에 미리 선언합니다. | ||
| - | function server_handler(socket) | ||
| - | { | ||
| - | socket.on(' | ||
| - | // | ||
| - | socket.on(' | ||
| - | socket.on(' | ||
| - | socket.on(' | ||
| - | socket.on(' | ||
| - | socket.on(' | ||
| - | | ||
| - | // socket에 효과적으로 접근하기 위해서는 이 방법이 효과적입니다. | ||
| - | socket.on(' | ||
| - | onSocketData(socket, | ||
| - | }); | ||
| - | } | ||
| - | |||
| - | // net.Socket event handlers | ||
| - | function onSocketConnect() | ||
| - | { | ||
| - | console.log(' | ||
| - | } | ||
| - | |||
| - | function onSocketData(socket, | ||
| - | { | ||
| - | var msg = 'ECHO: ' + buffer; | ||
| - | | ||
| - | console.log(' | ||
| - | process.stdout.write(msg); | ||
| - | | ||
| - | if(socket.write(msg)) | ||
| - | console.log(' | ||
| - | else | ||
| - | console.log(' | ||
| - | } | ||
| - | |||
| - | // emitted when the other side of the socket sends a FIN packet | ||
| - | function onSocketEnd() | ||
| - | { | ||
| - | console.log(' | ||
| - | } | ||
| - | |||
| - | function onSocketTimeOut() | ||
| - | { | ||
| - | console.log(' | ||
| - | } | ||
| - | |||
| - | // when the write buffer becomes empty | ||
| - | function onSocketDrain() | ||
| - | { | ||
| - | console.log(' | ||
| - | } | ||
| - | |||
| - | function onSocketError(err) | ||
| - | { | ||
| - | console.log(' | ||
| - | } | ||
| - | |||
| - | function onSocketClose(had_error) | ||
| - | { | ||
| - | console.log(' | ||
| - | | ||
| - | if(had_error) | ||
| - | { | ||
| - | console.log(' | ||
| - | } | ||
| - | else | ||
| - | { | ||
| - | console.log(' | ||
| - | } | ||
| - | } | ||
| - | |||
| - | |||
| - | // net.Server event handler | ||
| - | function onServerListening() | ||
| - | { | ||
| - | console.log(' | ||
| - | console.log(' | ||
| - | } | ||
| - | |||
| - | function onServerConnection(socket) | ||
| - | { | ||
| - | var ip = socket.remoteAddress; | ||
| - | var port = socket.remotePort; | ||
| - | | ||
| - | console.log(' | ||
| - | } | ||
| - | |||
| - | function onServerClose() | ||
| - | { | ||
| - | console.log(' | ||
| - | process.exit(0); | ||
| - | } | ||
| - | |||
| - | function onServerError(err) | ||
| - | { | ||
| - | | ||
| - | } | ||
| - | |||
| - | function onStdinData(server, | ||
| - | { | ||
| - | console.log(' | ||
| - | console.log(' | ||
| - | if(data == ' | ||
| - | { | ||
| - | console.log(' | ||
| - | server.close(); | ||
| - | process.exit(0); | ||
| - | } | ||
| - | } | ||
| - | |||
| - | function onStdinEnd() | ||
| - | { | ||
| - | console.log(' | ||
| - | } | ||
| - | |||
| - | |||
| - | //////////////////////////////////////////////////////////////////////////////// | ||
| - | // Beginning | ||
| - | // argument parsing | ||
| - | if (process.argv.length != 3) | ||
| - | { | ||
| - | console.log(' | ||
| - | process.exit(0); | ||
| - | } | ||
| - | else | ||
| - | { | ||
| - | port = process.argv[2] | ||
| - | } | ||
| - | |||
| - | // 서버 생성 | ||
| - | var server = net.createServer(server_handler); | ||
| - | // 서버의 이벤트를 설정합니다. | ||
| - | // server.on(' | ||
| - | server.on(' | ||
| - | server.on(' | ||
| - | server.on(' | ||
| - | server.on(' | ||
| - | // 서버는 대기 | ||
| - | server.listen(port); | ||
| - | // stdin 활성화 | ||
| - | process.stdin.resume(); | ||
| - | process.stdin.on(' | ||
| - | process.stdin.on(' | ||
| - | </ | ||
| - | |||
| - | ==== 사용법 ==== | ||
| - | node echo_server.js 9000 | ||
| - | | ||
| - | |||
| - | |||
| - | ===== 클라이언트 측 스크립트 ===== | ||
| - | <code javascript echo_client.js> | ||
| - | // 필요한 객체를 선언합니다. | ||
| - | var net = require(' | ||
| - | |||
| - | // net.Socket event handlers | ||
| - | function onSocketConnect() | ||
| - | { | ||
| - | console.log(' | ||
| - | } | ||
| - | |||
| - | function onSocketData(socket, | ||
| - | { | ||
| - | console.log(' | ||
| - | process.stdout.write(buffer); | ||
| - | } | ||
| - | |||
| - | // emitted when the other side of the socket sends a FIN packet | ||
| - | function onSocketEnd() | ||
| - | { | ||
| - | console.log(' | ||
| - | } | ||
| - | |||
| - | function onSocketTimeOut() | ||
| - | { | ||
| - | console.log(' | ||
| - | } | ||
| - | |||
| - | // when the write buffer becomes empty | ||
| - | function onSocketDrain() | ||
| - | { | ||
| - | console.log(' | ||
| - | } | ||
| - | |||
| - | function onSocketError(err) | ||
| - | { | ||
| - | console.log(' | ||
| - | } | ||
| - | |||
| - | function onSocketClose(had_error) | ||
| - | { | ||
| - | console.log(' | ||
| - | | ||
| - | if(had_error) | ||
| - | { | ||
| - | console.log(' | ||
| - | } | ||
| - | else | ||
| - | { | ||
| - | console.log(' | ||
| - | } | ||
| - | | ||
| - | process.exit(0); | ||
| - | } | ||
| - | |||
| - | function onStdinData(socket, | ||
| - | { | ||
| - | socket.write(data); | ||
| - | console.log(' | ||
| - | } | ||
| - | |||
| - | |||
| - | //////////////////////////////////////////////////////////////////////////////// | ||
| - | // Beginning | ||
| - | // argument parsing | ||
| - | if (process.argv.length != 4) | ||
| - | { | ||
| - | console.log(' | ||
| - | process.exit(0); | ||
| - | } | ||
| - | |||
| - | var host = process.argv[2]; | ||
| - | var port = process.argv[3]; | ||
| - | |||
| - | // 소켓 생성 | ||
| - | var options = {' | ||
| - | var socket | ||
| - | |||
| - | // 핸들러 설정 | ||
| - | socket.on(' | ||
| - | socket.on(' | ||
| - | socket.on(' | ||
| - | socket.on(' | ||
| - | socket.on(' | ||
| - | socket.on(' | ||
| - | |||
| - | // socket에 효과적으로 접근하기 위해서는 이 방법이 효과적입니다. | ||
| - | socket.on(' | ||
| - | onSocketData(socket, | ||
| - | }); | ||
| - | |||
| - | // stdin 활성화 | ||
| - | process.stdin.resume(); | ||
| - | process.stdin.on(' | ||
| - | </ | ||
| - | |||
| - | ==== 사용법 ==== | ||
| - | node echo_client.js 127.0.0.1 9000 | ||
| - | | ||
nodejs/echoserverclient.1373746672.txt.gz · 마지막으로 수정됨: 2014/10/09 21:23 (바깥 편집)
