简体   繁体   中英

Android Java Socket - Connection timed out

I'm trying to create one simple app. I want to my cell phone be a server socket and I'm trying to send messages from my pc, my pc is the client is this case.

When they are in the same network it works fine but when I connect my cell phone in a 3G network I receive the error "Connection timed out" in my PC.

I'm using a host from no-ip (in both situation). When I do 'telnet mycellphonehost.org 8080' for example I have no problem, it is able to connect. I think the no-ip host is working fine because is give me the correct external IP.

I also use one app called FIREBIND for test if the port is open or not. The result is: "Firebind was successfully able to both transmit and receive data over this port using the TCP protocol."

I already read a lot questions about this subject, similar problems... but nothing help me solve this issue. I hope someone can help me. Thanks in advance!

Follow the codes:

  • Android Server

     try{ ServerSocket server = new ServerSocket(port); Socket s = server.accept(); BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream())); System.out.println(input.readLine()); input.close(); s.close(); server.close(); } catch(IOException e){ System.out.println("ERROR: " + e.getMessage()); } 
  • PC Client

     try{ Socket s = new Socket("myhostfromno-ip.org",port); PrintStream output = new PrintStream(s.getOutputStream()); output.println("TEST MESSAGE"); output.flush(); s.close(); } catch(IOException e){ System.out.println("ERROR: " + e.getMessage()); } 
  • PS: They have to be in different network

I would recommend using GCM for the cell Notification and afterwards making a server pull. It certainly is much faster and you could use a simple REST server for serving data.

You need to make your PC the server and your droid the client.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM