0

EDIT: The problem is the input in the Client.class so when the Android devices receives an answer from the Server. Those are the following lines that causes the crash:

        InputStream input = client.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));

        String text;

        while ((text = reader.readLine()) != null) {
            Log.d("ClientLog", "Received from Server"+ text);
        }

Here is my Client.class in Android:

public class Client implements Runnable{

public Client() {

}

@Override
public void run() {
    try {
        Log.d("ClientLog", "Socket creation incoming");
        Socket client = new Socket("localhost", 5555);
        Log.d("ClientLog", "Client has been started");

        // Streams:
        OutputStream out = client.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));

        InputStream input = client.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
        // -----------------------------------------------------------------------

        writer.write("Test");
        writer.newLine();
        writer.flush();

        String text;

        while ((text = reader.readLine()) != null) {
            Log.d("ClientLog", "Received from Server"+ text);
        }

        writer.close();
        reader.close();

    } catch (UnknownHostException e) {
        Log.d("ClientLog", "Error: Host not found");
        e.printStackTrace();
    } catch (IOException e) {
        Log.d("ClientLog", "Error: IOException");
        e.printStackTrace();
    }
}

}

And if needed my Server.class in Java:

public class Server {

public static void main(String[] args) {

    ExecutorService executor = Executors.newFixedThreadPool(30);

    ServerSocket server;
    try {
        server = new ServerSocket(5555);
        System.out.println("Server has been started on Port 5555");

        while(true) {
            try {
                Socket client = server.accept();

                //Thread t = new Thread(new Handler(client));
                //t.start();
                executor.execute(new Handler(client));

            } catch (IOException e) {
                System.out.println("Error IOExceotion");
                e.printStackTrace();
            }
        }

    } catch (IOException e1) {
        e1.printStackTrace();
    }

}

The Handler.class needed for Server.class:

public class Handler implements Runnable{
private Socket client;

public Handler(Socket pClient) {
    client = pClient;
}

@Override
public void run() {
    try {
                // Streams:
                OutputStream out = client.getOutputStream();
                PrintWriter writer = new PrintWriter(out);

                InputStream input = client.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                // -----------------------------------------------------------------------

                String text = null;

                while ((text = reader.readLine()) != null) {
                    writer.write(text+ "\n");
                    writer.flush();
                    System.out.println("Recieved from Client: "+ text);
                }

                //Close Streams
                writer.close();
                reader.close();

                client.close();
    } catch (IOException e) {
        System.out.println("Error IOException");
        e.printStackTrace();
    }
}

}

Urbs
  • 131
  • 9
  • The IOException should mean the client is unable to establish a connection to the server. If you're using an android device like a phone, you might need to enable some permissions. Have you looked into that yet? https://stackoverflow.com/questions/4074808/java-socket-ioexception-permission-denied – user2570465 Jun 08 '17 at 18:21
  • @user2570465 Yeah i've read this post and i got all Internet permissions. And if i load the App to my Android phone the server gets the Test Message but the App crashes. So the connection problem might come from the Android Emulator – Urbs Jun 08 '17 at 18:29
  • Ok. So the error described above with the IOException on `Socket client = new Socket("localhost", 5555);` is fixed and now there's a new error? Do you want to edit in the details and the tracebacks? – user2570465 Jun 08 '17 at 18:30
  • @user2570465 I've changed the Android Emulator Device and i get the Test Message but the app still crashes, the crazy thing is that I don't get the logcat but the message is still send, and this should be impossible if you look at the order in my Client.class – Urbs Jun 08 '17 at 18:36
  • Do you have code for how the server handles the clients it receives? It looks like you wrote a `Handler` class to do this? Can you edit that in? – user2570465 Jun 08 '17 at 18:40
  • @user2570465 I've fixed the crash with deleting the input lines, so Android didn't liked my way how i received the text message back from my server, need to take a look at that, and i'll post the Handler.class – Urbs Jun 08 '17 at 18:42
  • @user2570465 edited my question, you can now see the lines that are actually causing the crash, sorry for the inconvenience. – Urbs Jun 08 '17 at 18:51
  • How did you find out those lines were causing the crash? Was there a traceback or something? – user2570465 Jun 08 '17 at 18:54
  • @user2570465 I've deleted exactly this part of the class (The receiving method/part) and then everything worked fine, so the error muste be in those lines. – Urbs Jun 08 '17 at 18:57
  • I assume you were still writing to the server and you just deleted the reading part. What happens if you just do `text = reader.readLine()` instead of that while loop? Does it still crash? – user2570465 Jun 08 '17 at 18:59
  • @user2570465 Yes that fixed the crash, but without the while loop i think i can't receive anymore message with multiple lines. I actually don't need multiple lines at the moment, but i wanted to implement it. – Urbs Jun 08 '17 at 19:07
  • Hmm... The while loop logic seems right, but I don't know why it would crash. Try these separately? 1) Do twice `text = reader.readLine();` . 2) In a for loop do `text = reader.readLine()` 100 times and if text becomes `null`, then `break`. Let me know if either of them crashes? – user2570465 Jun 08 '17 at 19:12
  • @user2570465 Your 2.) option works perfect! Didn't test the 1.) option because it's not dynamic enough ^^ Maybe you could post an answer, so I can thank you? :) – Urbs Jun 08 '17 at 19:27
  • Sure. I added in the answer. Thanks! It might've just been something weird with your while loop condition. Did you try just doing a `while (true) { text = reader.readLine(); if (text == null) break;}`? – user2570465 Jun 08 '17 at 19:34
  • @user2570465 yes of course good idea a while(true) loop with an break is way more better then the for loop ^^ The only crazy thing is that my old while loop works perfect on server and client in Java but i think maybe it's an Android thing. But now it's working so the code is okay^^ – Urbs Jun 08 '17 at 19:43

1 Answers1

1

The debugging was resolved in the comments above.

Step 1: Add permissions to the android manifest file as was outlined here: Java socket IOException - permission denied

Step 2: Change the while ((text = reader.readLine()) != null) check. I guess the text = reader.readLine() != null check was problematic.

user2570465
  • 2,437
  • 2
  • 18
  • 22