TCP Android服务器不接受来自Java客户端的连接

时间:2013-11-27 06:50:40

标签: java android sockets tcp client-server

我正在尝试将我的Android服务器连接到我的PC Java客户端。

首先,我将android作为客户端,将PC Java作为服务器,它完美运行!在另一个模块中,我想连接PC Java作为客户端,Android作为服务器。

在android中使用socket.accept()启动服务器后;程序在那里等待连接。然后我启动了PC Java客户端,没有任何反应,我收到“连接超时”错误!我该怎么办?

这是我的Android服务器代码

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

}

这是我的PC Java客户端代码

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

请帮忙!谢谢! :)

0 个答案:

没有答案