我無法連接到我在CloudFoundry上運行的TCP服務器示例。當在本地node.js安裝上運行我的app.js文件時,它工作得很好。特別是,當我使用vmc push運行CloudFoundry時,服務啓動並不會崩潰。有些IP連接到它,斷開連接,並且據我所知,服務繼續運行。無法連接到CloudFoundry上的TCP服務器(localhost node.js工作正常)
我只是不能使用既不是「遠程登錄」,也不是「NC」(注意對本地主機的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');
});
stdout.log(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
是的,這是正確的,我只得到504(連接超時),謝謝。我將編寫一個使用主機頭的測試程序。 – 2013-03-15 14:37:01
如果你能接受答案,那就太好了:-) – 2013-03-15 15:24:04
嗨丹,我沒有忘記你。我沒有按「接受」,因爲我還沒有設法根據您的建議實施可行的解決方案。我現在就做好了。謝謝! – 2013-03-15 15:40:31