matip matip - 6 months ago 159
Android Question

Can send but cannot receive data through socket in my service

My android app is supposed to send data to the server through socket but also occasionally receive data from that server. To do that I am using service and.
Sending data to the server works ok, I'm using service method that i'm calling from activity. But while sending works just fine my app doesnt receive any data back. While I send string "pp" I should get another sring back from server but that just doesn't happen. What am I doing wrong?

My service:

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


And my server:
public class Server {

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



}

EJP EJP
Answer

Usual problems.

  1. You are reading lines but you aren't writing lives. Add a line terminator.
  2. You should stop reading when readLine() returns null, and close the socket.
  3. Once readLine() has returned null, the extra read test is pointless. It will always return -1.