El servidor TCP Android no aceptará la conexión del cliente Java

I'm trying to connect my Android server to my PC Java client.

First I made the android as the client and PC Java as the server and it worked perfectly! In another module I want to connect as PC Java as the client and the Android being the server.

After I start the server in android by using socket.accept(); the program holds there waiting for a connection. Then I started the PC Java client and nothing happens and I get "Connection timed out" error! What do I do?

Here is my Android server code

public class Controller extends Service{

private Socket socket;
private ServerSocket serversocket;
private int port=1339;
private static Thread controllerThread,waitForConnection;


@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
        waitForConnection = new Thread(){
            public void run(){
                try {
                    System.out.println("Waiting for connection");
                    serversocket=new ServerSocket(port);
                    controllerThread.start();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    controllerThread.interrupt();
                    e.printStackTrace();
                }

            }
        };

        controllerThread=new Thread(){
            public void run(){
                try {
                    System.out.println("Controller thread!");           
                    while(true){
                        Thread.sleep(60);
                        System.out.println("In here!");
                        socket=serversocket.accept();
                        System.out.println("Yaay");
                        BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        String command;
                        if((command=br.readLine())!=null)
                            System.out.println(command);
                        socket.close();
                        br.close();
                    }           
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        waitForConnection.start();

    return Service.START_NOT_STICKY;
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    controllerThread.interrupt();
    waitForConnection.interrupt();
}

}

Here is my PC Java client code

private void initialize() {
    System.out.println("IP Address = "+ipaddress);
    Socket sock;
    try {
        System.out.println("Initializing connection");
        sock = new Socket(ipaddress, port);
        System.out.println("Connected!");
        System.out.println("Address = "+sock.getInetAddress());
        sock.close();
    } catch (UnknownHostException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    } catch (IOException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
}

¡Por favor ayuda! ¡Gracias! :)

preguntado el 27 de noviembre de 13 a las 06:11

i have the same problem. did you ever get this to work? -

What is your ipaddress for the android phone and the PC client? You can try to ping from your PC to the phone first. -

0 Respuestas

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.