Centos

如何在 Cent OS 中為 socket.io 打開低於 1024 的埠?

  • November 20, 2013

我正在嘗試843在我的 Cent OS 5.2 中打開埠,我添加了以下行etc\sysconfig\iptables

-A INPUT -p tcp --dport 843 -j ACCEPT

然後更新了我的iptables服務。我需要在由 root 使用者通過sudo node index.js命令執行的 node.js 應用程序中監聽該埠,但是forbidden port當我嘗試通過該埠建立連接時仍然出現錯誤。

這是我通過執行得到的sudo iptables -L -v

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target     prot opt in     out     source               destination                     
2811  238K ACCEPT     tcp  --  any    any     anywhere             anywhere                                tcp dpt:http
 112  6224 ACCEPT     tcp  --  any    any     anywhere             anywhere                                tcp dpt:hbci
   0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere                                tcp dpt:843
  41  2124 ACCEPT     tcp  --  any    any     anywhere             anywhere                                state NEW tcp multiport dports 5901:5903,6001:6003
13093   13M ACCEPT     all  --  any    any     anywhere             anywhere                                state RELATED,ESTABLISHED
  26  3584 ACCEPT     icmp --  any    any     anywhere             anywhere                        
   0     0 ACCEPT     all  --  lo     any     anywhere             anywhere                        
 109  6404 ACCEPT     tcp  --  any    any     anywhere             anywhere                                state NEW tcp dpt:ssh
888K   80M REJECT     all  --  any    any     anywhere             anywhere 

這是輸出sudo netstat -ptl | grep node

tcp        0      0 *:843       *:*    LISTEN      12927/node
tcp        0      0 *:http      *:*    LISTEN      12927/node
tcp        0      0 *:10843     *:*    LISTEN      12927/node

我嘗試使用以下程式碼在socket.io中偵聽該埠:

var io          = require('socket.io').listen(
                   843,
                   {   log: false,
                       flashPolicyServer: true,
                       transports: ['websocket', 'flashsocket', 'htmlfile', 'xhr-multipart', 'xhr-polling', 'jsonp-polling']
                   }
               );
io.sockets.on('connection', function(socket){
   // my event listeners
});

問題解決了。所有伺服器配置都是正確的,顯然客戶端的防火牆阻止了該埠。所以我切換到使用與 http 伺服器相同的埠(埠 80)的sockjs ,因此不存在從客戶端阻止連接的風險。

引用自:https://serverfault.com/questions/556050