Pregunta

I am running on server on my development machine that I am trying to connect to with my android tablet, but I am getting a ConnectionException because it is failing to connect.

I have tested the exact client code on my development machine (and using the public IP address instead of 'localhost') and this works. But once I switch over to testiing on Android, no such luck.

Note: this is similar to the problems in Why I get nothing back from node.js server on android and Connecting to a nodejs server via android and getting a timeout exception, but neither resulted in useful answers.

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();
});

Client (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();
            }
        }
    }
}

I think it might have something to do with Windows Firewall, but I went in and allowed 'Node.js' to communicate through the firewall. Is there something else I need to do, or am missing?

¿Fue útil?

Solución

Either one of two things made this work:

  1. Restarting the OS after letting Node.js through the firewall
  2. Allowing the port 7000 through the firewall, and then restarting

So, make sure to restart after changing firewall settings (I should have known this...) - it could fix your problem.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top