我正在使用 Telnet - 客户端来验证电子邮件,我连接到服务器,它响应 250,但是当我编写另一个命令并询问答案时,它根本不回答我。
这是我的代码:
function ConnectTelnet(){
//var connection = new telnet();
var response;
var HOST = 'mail.dominio.com';
var PORT = 25;
var net = require('net');
var client = net.connect(25,'mail.dominio.com',function(){
console.log('connected to server!');
console.log('CONNECTED TO: ' + HOST + ':' + PORT);
client.on('data', function(data) {
console.log('Received: ' + data);
response = data;
if(response.indexOf("220") === -1){
client.write('EHLO dominio.com')
console.log(data)
}
});
})
}
有人知道我该如何继续吗?谢谢:)
最佳答案
您无法发送数据、获得答案,然后在同一连接上发送更多数据。 TCP 不会“单独”发送消息。 TCP 是一种流协议(protocol),这意味着当您向套接字写入字节时,您会在接收端以相同的顺序获得相同的字节。不存在“消息边界”或“数据包”之类的概念。
如果您想这样做,则每次都需要建立一个新连接。
这就是我在同一连接上发送多个 EHLO 的方法:
const net = require('net');
const client = net.createConnection({ host: '127.0.0.1', port: 1025 }, () => {
console.log('connected to server!');
checkEHLO(client, [ 'xxx@xxx.xxx', 'xxx@xxx.xxx', 'xxx@xxx.xxx' ]);
});
client.on('data', (data) => {
console.log(data.toString());
client.end();
});
client.on('end', () => {
console.log('disconnected from server');
});
function checkEHLO(client, emails){
emails.forEach((email) => {
client.write('EHLO ' + email + '\n');
});
}
这是我得到的回复:
connected to server!
220 127.0.0.1 ESMTP Service Ready
250-Hello xxx@xxx.xxx
250-PIPELINING
250-8BITMIME
250-STARTTLS
250 AUTH PLAIN LOGIN
250-Hello xxx@xxx.xxx
250-PIPELINING
250-8BITMIME
250-STARTTLS
250 AUTH PLAIN LOGIN
250-Hello xxx@xxx.xxx
250-PIPELINING
250-8BITMIME
250-STARTTLS
250 AUTH LOGIN PLAIN
关于javascript - 如何在 Node.js 中与 Net 建立持久连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52562907/