在我的服务中可以通过socket发送但无法接收数据

时间:2016-05-26 18:16:03

标签: java android sockets

我的Android应用程序应该通过套接字向服务器发送数据,但偶尔也会从该服务器接收数据。要做到这一点,我正在使用服务和。 将数据发送到服务器工作正常,我正在使用我从活动中调用的服务方法。但发送工作正常,我的应用程序没有收到任何数据。当我发送字符串“pp”时,我应该从服务器获得另一个回复,但这不会发生。我做错了什么?

我的服务:

public class MyService extends Service {
private Socket socket;
public static PrintWriter out;
private BufferedReader in;
private String line;
private final IBinder binder = new MyBinder();
@Nullable
@Override
public IBinder onBind(Intent intent) {
    return binder;
}
public class MyBinder extends Binder {
    MyService getMyService() {
        return MyService.this;
    }
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    new Thread(new Runnable() {
        public void run() {
            try {
                InetAddress serverAddr = InetAddress.getByName("10.0.2.2");
                socket = new Socket(serverAddr,8998);
                out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
                        .getOutputStream())), true);
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                Thread thread = new Thread(new Listener());
                thread.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();

    return START_STICKY;
}

private class Listener implements Runnable{

    @Override
    public void run() {
        while(true){
            try {
                    line = in.readLine();
                    if(line!=null) Log.d(line,line); //never shows up in logcat
                    line=null;

            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

public void send(String line){
    out.println(line);
}
}

我的服务器: 公共类服务器{

private static final int SERVER_PORT = 8998;        
private static ServerSocket server = null;
private static Socket client = null;
private static BufferedReader in = null;
private static boolean isConnected = true;
private static String line;

public static void main(String[] args){
    while(isConnected){     
        if(server==null || server.isClosed()){
            try{                    
                server = new ServerSocket(SERVER_PORT);
                client = server.accept();
                in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                System.out.println("Connected");
            }catch (IOException e){
                System.out.println("Error in opening Socket"+e);
                System.exit(-1);
            }
        }
        else{
            try{                    
                line = in.readLine();

                if(line != null)
                {
                    if(line.equals("pp")){
                        PrintWriter out = new PrintWriter(new BufferedWriter(
                                new OutputStreamWriter(client.getOutputStream())),
                                true);
                        String toSend = "String to send";
                        out.write(toSend);
                        System.out.println("sent");
                    }
                    System.out.println(line);

                }else if(in.read()==-1){
                    client.close();     
                    server.close();
                }


            }catch (IOException e){
                System.out.println("Read from client failed");
                System.exit(-1);
            }
        }
    }
}



}

1 个答案:

答案 0 :(得分:1)

常见问题。

  1. 你正在读行,但你不是在写生活。添加行终止符。
  2. readLine()返回null并关闭套接字时,您应该停止阅读。
  3. 一旦readLine()返回null,额外的读取测试就毫无意义。它将始终返回-1。