나는 간단한 TCP 서버를 만들었고 클라이언트 입력을 변수에 저장된 하드 코딩 된 문자열과 비교해야합니다. 그러나 data == username
은 항상 실패합니다.문자열 비교 실패
왜? 그것에 대해 무엇을 할 수 있습니까?
예 :
나는 클라이언트 구현, 코드를 업데이트 한var authenticateClient = function(client) {
client.write("Enter your username:");
var username = "eleeist";
client.on("data", function(data) {
if (data == username) {
client.write("username success");
} else {
client.write("username failure");
}
});
}
var net = require("net");
var server = net.createServer(function(client) {
console.log("Server has started.");
client.on("connect", function() {
console.log("Client has connected.");
client.write("Hello!");
authenticateClient(client);
});
client.on("end", function() {
console.log("Client has disconnected.");
});
}).listen(8124);
'데이터'에는 무엇이 들어 있습니까? 끝에 개행 문자가 포함되어 있습니까? – Sjoerd
잘 모르겠습니다. 나는 그것을 'eleeist \ n'과 비교하려고 시도했지만 여전히 운이 없다. – Eleeist