Page 383 - 2020학년도 MDP과제발표회 자료집 (통신과) (3)
P. 383
// 서버 생성
var server = net.createServer(function (socket) { // 콜백함수 : 클라이언트 소
켓 연결 시
console.log('[!] ' + socket.remoteAddress + " connected."); // 콘솔에 연결된 클라이언트 주
소 출력
socket.setEncoding("utf8"); // 데이터 UTF-8 로 인코딩
socket.on('data', function (data) { // on('data') : 'data' 이벤트가
왔을 때 콜백함수 실행
var wrong = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0); // 지정된 명령어를 입력했는
지 확인하는 배열
var command = data.split(":"); // 들어온 데이터를 ":" 로 구분해
배열로 저장
console.log(socket.remoteAddress + " : " + data); // 해당 소켓에서 들어온 데이터
를 콘솔에 출력
try {
if (command[0] == " 책목록") { // 커맨드가 책목록 일 경우' '
var sql = "select bookname,location from " + command[1] + " order by location";
//command[1] 에 들어온 기계 번호에 있는 책이름과 층수를 받아온다
link.query(sql, function (err, result, fields) { // 쿼리문 실행
if (!err) {
if (result != "") { //result 에 값이 있을 때
socket.write(result[0].bookname + ":" + result[1].bookname + ":" +
result[2].bookname + ":" + result[3].bookname + ":" + result[0].location + ":" + result[1].location +
":" + result[2].location + ":" + result[3].location);
console.log(result[0].bookname + ":" + result[1].bookname + ":" +
result[2].bookname + ":" + result[3].bookname + ":" + result[0].location + ":" + result[1].location +
":" + result[2].location + ":" + result[3].location);
} else { //result 에 값이 없을 때 책이 없다고 인식
socket.write(".");
console.log(" 기계에 책이 없습니다");
}
} else {
console.log('[!] "' + sel + '" Query has error!');
}
})
인천전자마이스터고등학교
- 399 - 정보통신기기과 399