0

One of my case statements is never reached when I try to call it from my Server, but I don't know if its because of the Server, the DataOutputStream or if I just made a logic mistake. If I try to call "useEffect" it always jumps to the default block.

Also my "testCounter" variable should be 1 if I call for the "dealdmg" case, but its outputting 2 everytime I run the app.

EDIT: testCounter is initilized as public static testCounter = 0; It is not called or modified anywhere else. My guess is that the Thread cycles through the DEALDMG case more than 1 time causing it to be set to 2 when output.

This is my Server:

public class ServerForStrickerCardsTesting {

    public static void main(String[] args) throws InterruptedException {
        try {
            ServerSocket myServer;
            System.out.println("Type \"start\" to start the Server");
            Scanner scan = new Scanner(System.in);
            String starter= new String();
            starter = scan.nextLine();
                if(starter.contentEquals("start")) {
                    myServer = new ServerSocket(56564);
                }else {
                    System.out.println("failed");
                    return;
                }

            Socket mySocket = myServer.accept();
            System.out.println("Accepted connection");

            System.out.println("Sending package now...");

            DataOutputStream data = new DataOutputStream(mySocket.getOutputStream());

            data.writeUTF("dealdmg"+" "+ "1"+"\n");
            //data.flush();
            data.writeUTF("useEffect"+" "+"2"+"\n");
            //data.flush();
            //data.writeUTF("stop");


            data.close();
            System.out.println("Package send");

            System.out.println("Type \"stop\" to close the Server");

            String ending = scan.nextLine();

            if(ending.contentEquals("stop")) {

                myServer.close();
                System.out.println("Server closed!");
            }

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

    }

}

This is how I try to reach it from my App:

class CommunicationThread implements Runnable{

        Socket clientSocket;

        //constructor
        public CommunicationThread(Socket inSocket){

            clientSocket = inSocket;

            try {

                enemyDataReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

                //Toast.makeText(getApplicationContext(),"Reached Inputstreamreader",Toast.LENGTH_LONG).show();

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

        }

        @Override
        public void run() {

            try {

                while(!Thread.currentThread().isInterrupted()){

                        String read = enemyDataReader.readLine();
                        String line;
                        if((line = read)!=null){
                            read.trim();
                            incomingDataHandler.post(new TransferThread(read));
                            read=null;
                        }


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


        }
    }

    class TransferThread implements Runnable{


        public TransferThread(String inString){

            incomingData = inString.split(" ");

        }

        @Override
        public void run() {


            switch(incomingData[0].toUpperCase()){

                case "DEALDMG":
                    testCounter++;
                    Toast toast = Toast.makeText(TheGame.this,incomingData[0] + incomingData[1] + testCounter,Toast.LENGTH_LONG);
                    switch(testCounter){
                        case 1:
                            toast.show();
                            break;
                        case 2:
                            toast.setGravity(Gravity.CENTER,0,0);
                            toast.show();
                            break;
                        case 3:
                            toast.setGravity(Gravity.TOP,0,0);
                            toast.show();
                            break;
                    }
                    break;
                case "USEEFFECT":
                    Toast.makeText(getApplicationContext(),incomingData[1],Toast.LENGTH_LONG).show();
                    break;
                default:
                    Toast.makeText(getApplicationContext(),"default",Toast.LENGTH_SHORT).show();
                    break;
            }

        }


    }

Note: I know Toasts are not very good to get feedback from my code, but I'm a beginner so this works fine for me. the point is it jumps to the default block.

Dboy0Z
  • 43
  • 1
  • 1
  • 8
  • Suggest you use `"default Case for: " + incomingData[0].toUpperCase()` in the default case, just to see what your code sees. May be its a space in the wrong or something like that. – Madushan Jun 01 '20 at 09:21
  • You were right with this one! There was indeed a space added to the front, thought I solved this by using .trim() but I used in in the wrong place. thank you. The testCounter problem remains tho – Dboy0Z Jun 01 '20 at 09:59
  • You don't show how `testCounter` is initialized or where else it's modified. Clearly it's set to `1` when entering `DEALDMG`, and it's being incremented to 2. – Ryan M Jun 01 '20 at 23:14
  • testCounter is initilized as public static testCounter = 0; It is not called or modified anywhere else. My guess is that the Thread cycles through the DEALDMG case more than 1 time causing it to be set to 2 when output. – Dboy0Z Jun 02 '20 at 05:17

0 Answers0