Zolomon
Zolomon

Reputation: 9785

Trouble connecting a socket from android client app to server

I'm having trouble connecting a socket from my android phone to my computer running netcat listening on a port (5555).

What I run in my android app:

    try {
        Socket s = new Socket("My IP number here (not local IP)", 5555);
        DataOutputStream outToServer = new DataOutputStream(s.getOutputStream());
        outToServer.writeBytes("Hello!");
        s.close();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

This is the line I use for netcat: nc -l 5555

I'm not sure what I'm missing.. I've forwarded the port on my router and rebooted the router. Added permission to internet in my android app. The phone is connected via 3G and my computer is connected via broadband. netcat is allowed in my firewall.

I get a SocketTimeoutException error in my app when trying to connect.

I guess the router takes care of handling which computer in the LAN will receive the request.

Anyone know what I'm missing?

EDIT: I think I might have tracked down the problem.. I have two options on my router Virtual Server and Port forwarding. I don't know which to set to port 5555 in both UDP/TCP. http://dl.dropbox.com/u/4385192/2011-11-19%2003.17.53%20pm.png enter image description here From what I understand it's Virtual Server I should use instead of the port forwarding? If so, could someone clarify the difference between the two?

Upvotes: 2

Views: 1785

Answers (1)

Zolomon
Zolomon

Reputation: 9785

My computer had changed local IP since last configuration, so my Virtual Server/Port Forwarding rules did not apply, so I updated it (from 192.168.0.199 -> 192.168.0.196) and now it works excellently.

So all steps applied:

  • Set a rule for Virtual Server/Port Forwarding on your router .
  • Set a rule for Inbound Filtering on your router in case you wish to only allow a whitelisted IP to connect and propagate to next security layer.
  • If your router has any other firewall settings, create rules to allow your connections to be made.
  • On your server/computer allow the ports in your firewall, or the application you're running to receive inbound connections.
  • Profit!!!

Upvotes: 1

Related Questions