我在节点js
中编写了一个简单的服务器应用程序var net = require('net')
console.log('start...')
var server = net.createServer(function (socket) {
console.log('client connected', socket.address())
socket.on('end', function () {
console.log('end: client disconnected')
})
})
var port = process.env.PORT || 4713;
server.listen(port, function () {
console.log('server is listening on port', port)
})
和Qt中的一个简单客户端:
#include <QCoreApplication>
#include <QTimer>
#include <QtNetwork/QTcpSocket>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTcpSocket socket;
QObject::connect(&socket, static_cast<void(QTcpSocket::*)(QAbstractSocket::SocketError socketError)>(&QTcpSocket::error),
[](QAbstractSocket::SocketError socketError) {
qDebug() << "Error" << socketError;
});
QObject::connect(&socket, &QTcpSocket::stateChanged,
[](QAbstractSocket::SocketState state) {
qDebug() << state;
});
//socket.connectToHost("127.0.0.1", 4713);
socket.connectToHost("herokutestoiaavb.herokuapp.com", 8080);
QTimer::singleShot(2000, [&](){qDebug() << "disconnect now..."; socket.disconnectFromHost();});
return a.exec();
}
它在我的语言环境机器上运行得非常好。
但是当我部署到heroku时,我可以看到应用程序启动并侦听但我无法连接。
我总是得到一个ConnectionRefusedError。
我认为它与端口号有关。我尝试了8080,4713和heroku动态分配给我的应用程序的端口。但仍然得到错误。
2016-12-03T13:09:35.269234 + 00:00 heroku [slug-compiler]:Slug编译开始 2016-12-03T13:09:35.269243 + 00:00 heroku [slug-compiler]:Slug编译完成 2016-12-03T13:09:35.637820 + 00:00 heroku [web.1]:重启 2016-12-03T13:09:35.638412 + 00:00 heroku [web.1]:状态从最开始变为开始 2016-12-03T13:09:36.653298 + 00:00 heroku [web.1]:使用SIGTERM停止所有进程 2016-12-03T13:09:36.739735 + 00:00 heroku [web.1]:进程退出状态143 2016-12-03T13:09:37.413979 + 00:00 heroku [web.1]:使用命令
node heroku.js
启动流程 2016-12-03T13:09:39.638158 + 00:00 app [web.1]:开始...... 2016-12-03T13:09:39.658419 + 00:00 app [web.1]:服务器正在侦听端口15665 2016-12-03T13:09:41.138467 + 00:00 heroku [web.1]:状态由开始向上变化 2016-12-03T13:09:41.108477 + 00:00 app [web.1]:客户连接{地址:&#39; :: ffff:172.17.101.182&#39;,家庭:&#39; IPv6&#39 ;,端口:15665} 2016-12-03T13:09:41.112260 + 00:00 app [web.1]:结束:客户端已断开连接 2016-12-03T13:09:41.113445 + 00:00 app [web.1]:客户连接{地址:&#39; :: ffff:172.17.101.182&#39;,家庭:&#39; IPv6&#39 ;,端口:15665} 2016-12-03T13:09:41.114384 + 00:00 app [web.1]:结束:客户端已断开连接 2016-12-03T13:11:05.571200 + 00:00 app [web.1]:客户连接{地址:&#39; :: ffff:172.17.101.182&#39;,家庭:&#39; IPv6&#39 ;,端口:15665} 2016-12-03T13:11:05.572232 + 00:00 app [web.1]:结束:客户端已断开连接 2016-12-03T13:13:20.696667 + 00:00 app [web.1]:客户连接{地址:&#39; :: ffff:172.17.101.182&#39;,家庭:&#39; IPv6&#39 ;,端口:15665} 2016-12-03T13:13:20.697078 + 00:00 app [web.1]:结束:客户端已断开连接 2016-12-03T13:15:35.879204 + 00:00 app [web.1]:客户连接{地址:&#39; :: ffff:172.17.101.182&#39;,家庭:&#39; IPv6&#39 ;,端口:15665} 2016-12-03T13:15:35.879473 + 00:00 app [web.1]:结束:客户端已断开连接 2016-12-03T13:17:51.145281 + 00:00 app [web.1]:客户连接{地址:&#39; :: ffff:172.17.101.182&#39;,家庭:&#39; IPv6&#39 ;,端口:15665} 2016-12-03T13:17:51.147231 + 00:00 app [web.1]:结束:客户端已断开连接 2016-12-03T13:20:05.676596 + 00:00 app [web.1]:客户连接{地址:&#39; :: ffff:172.17.101.182&#39;,家庭:&#39; IPv6&#39 ;,端口:15665} 2016-12-03T13:20:05.676876 + 00:00 app [web.1]:结束:客户端已断开连接
我不知道IP 172.17.101.182的连接是什么,但它不是我。有点奇怪......
我做错了什么?