2013-12-16 79 views
1

我正在我的開發計算機上的服務器上運行,我嘗試與我的android平板電腦連接,但我得到一個ConnectionException,因爲它無法連接。無法連接到node.js服務器從android

我已經在我的開發機器上測試了確切的客戶端代碼(並且使用公共IP地址而不是'localhost'),並且這可以工作。但是,一旦我切換到Android上的測試,沒有這樣的運氣。

注:這類似於Why I get nothing back from node.js server on androidConnecting to a nodejs server via android and getting a timeout exception的問題,但也造成了有用的答案。

SERVER(Node.js的)

var net = require('net'); 
var couchDB = require('./serverTest'); 
var async = require('async'); 

async.parallel([ 
function(callback) { 
    console.log('first'); 

    couchDB.getMapData(function(data){ 
     var mapDataString = JSON.stringify(data); 
     callback(null, mapDataString); 
    }); 
}, 
function(callback) { 
    console.log('second'); 
    var server = net.createServer(function(c) { //'connection' listener 
     console.log('server connected'); 

     c.on('end', function() { 
      console.log('server disconnected'); 
     }); 

     callback(null, c); 
    }).listen(7000, null); 
}], 
function(err, responses) { 
    var socket = responses[1]; 
    var mapDataString = responses[0]; 
    console.log('writing data'); 
    socket.write(mapDataString); 
    socket.end(); 
}); 

客戶端(JAVA)

public class NodeClientTest { 
private static final String IP_ADDRESS = "142.169.196.74"; 
    public static void main(String[] args){ 

     String jsonString = null; 
     Socket clientSocket = null; 
     try{ 
      clientSocket = new Socket(IP_ADDRESS, 7000); 

      byte[] buffer = IOUtils.toByteArray(clientSocket.getInputStream()); 
      jsonString = new String(buffer); 
      System.out.print(jsonString); 
     } 
     catch(IOException e){ 
      e.printStackTrace(); 
     } 
     finally{ 
      try{ 
       clientSocket.close(); 
      } 
      catch(Exception e){//not initialized 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

我想可能有一些做了Windows防火牆,但我去了,並允許'Node.js'通過防火牆進行通信。有什麼我需要做的,或者失蹤?

+0

在家庭網絡上?無線上網?路由器允許端口轉發? – curtisk

+0

在公司網絡上,不確定端口轉發 – kburbach

回答

0

要麼使這一工作兩件事情:讓Node.js的通過防火牆

  • 通過防火牆允許端口7000,然後重新啓動後

    1. 重新啓動OS

    所以,確保在更改防火牆設置(我應該知道這個......)後重新啓動 - 它可以解決您的問題。