how to get simple android client and pc server get working

余生颓废 提交于 2019-12-25 04:35:07

问题


I have a simple server running on my pc and a simple client on my htc desire running android 2.2.

The server and client both use the same port. I hard code the servers ip into the clients code. When I try to connect to the running server via the client on android the client throws this exception:

IOException ...: java.net.SocketException: The operation timed out.

Here are parts of the client and server codes:

client code

  InetAddress addr;
  Socket socket = null;

  byte [] ipAddress = new byte[] {(byte)82,(byte)168,(byte)175,(byte)141};

  try{

   addr = InetAddress.getByAddress(ipAddress);

   socket = new Socket(addr, 1234);
   System.out.println("socket = "+socket);

   BufferedReader in = new BufferedReader(
            new InputStreamReader(
             socket.getInputStream()));

   PrintWriter out = new PrintWriter(
            new BufferedWriter(
             new OutputStreamWriter(socket.getOutputStream())),true);

        out.println("hello");
        socket.close();
  } catch (UnknownHostException e) {
   System.out.println("UnknownHostException ...: "+e);
  } catch (IOException e) {
   System.out.println("IOException ...: "+e);
  }

,

server code

    ServerSocket ss = new ServerSocket(PORT);
  System.out.println("Started:"+ss);

  try{
   //block until a connection occures
   Socket socket = ss.accept();

   try{
    System.out.println("Conncetion accepted:"+socket);
    InputStream IS = socket.getInputStream();
    InputStreamReader ISR = new InputStreamReader(IS);
    BufferedReader in = new BufferedReader(ISR);

    OutputStream OS = socket.getOutputStream();
    OutputStreamWriter OSR= new OutputStreamWriter(OS);
    BufferedWriter BW = new BufferedWriter(OSR);
    //output automatically flushed by PrintWriter
    PrintWriter out = new PrintWriter(BW,true);

    while(true){
     String str = in.readLine();
     System.out.println("Client: "+str);
     out.println("hi");
    }
   }finally{
    System.out.println("Closing...");
    socket.close();
   }
  }finally{
   ss.close();
  }

This code is working using wifi but not through 3g.

Any help is welcome...and thank you in advance.


回答1:


You probably use a router/wifi-access-point to connect to internet?

If your router uses NAT protocol, then any devices on the Internet can not open connectio to your PC. NAT only allows PC to open connection to the internet and not vice-versa.

So when your phone is on wifi it can connect to PC because they are both on the same network and NAT is not used. When your phone is on 3g, and hence of the internet, than there is NAT in-between your phone and your PC.

Solution:

Enable port forwarding on your router, than connect with your app to router's IP which will forward this connection to your PC.



来源:https://stackoverflow.com/questions/4851013/how-to-get-simple-android-client-and-pc-server-get-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!