我无法连接到在 CloudFoundry 上运行的 TCP 服务器示例。在本地 node.js 安装上运行我的 app.js 文件时,它工作得很好。具体来说,当我使用 vmc push 运行 CloudFoundry 时,服务会启动并且不会崩溃。一些 IP 连接到它,断开连接,据我所知,该服务继续运行。
我只是无法使用“telnet”或“nc”连接到它(请注意,当定向到 localhost node.js 服务器时,这两个都可以正常工作。
这失败了:
> nc themagicsandbox2.cloudfoundry.com 8124
这有效
> nc localhost 8124
hello from TCP server! (intended reply)
我的代码在这里提交,Cloud Foundry stdout.log 在它下面提交。
代码:
myTrace('loaded'); // myTrace prepends timestamp to text and sends to console.log
var tcpServer = require('net').createServer(function(sock) { //'connection' listener
sock.on('connect', function() {
myTrace('client ' + sock.remoteAddress + ':' + sock.remotePort +' connected');
sock.write('hello from TCP server!\r\n');
sock.pipe(sock);
});
sock.on('end', function() {
myTrace('client disconnected');
});
});
tcpServer.listen(8124, process.env.VCAP_APP_HOST || "localhost");
tcpServer.on('listening', function() {
myTrace('server is listening - bound!');
});
tcpServer.on('error', function(err) {
myTrace('server err: ' + err);
if (err.code == 'EADDRINUSE') {
myTrace('Address in use, retrying ...');
setTimeout(function() {
tcpServer.close(function (err) {
myTrace('server.close: ' + err);
});
tcpServer.listen(SLIDEIN_TCP_PORT, process.env.VCAP_APP_HOST || "localhost");
}, 1000);
}
});
tcpServer.on('close',
function() {
myTrace('server has closed');
});
标准输出日志(CloudFoundry):
Getting file contents... OK
Fri Mar 15 2013 11:59:02 GMT+0000 (UTC) loaded
Fri Mar 15 2013 11:59:02 GMT+0000 (UTC) server is listening - bound!
Fri Mar 15 2013 11:59:03 GMT+0000 (UTC) client 172.30.50.10:31840 connected
Fri Mar 15 2013 11:59:03 GMT+0000 (UTC) client disconnected
标准输出(本地主机 node.js):
Fri Mar 15 2013 12:57:39 GMT+0100 (CET) loaded
Fri Mar 15 2013 12:57:39 GMT+0100 (CET) server is listening - bound!
Fri Mar 15 2013 12:57:53 GMT+0100 (CET) client 127.0.0.1:52260 connected
Fri Mar 15 2013 12:57:59 GMT+0100 (CET) client disconnected
Fri Mar 15 2013 12:58:00 GMT+0100 (CET) client 127.0.0.1:52261 connected
Fri Mar 15 2013 12:58:01 GMT+0100 (CET) client disconnected