# Help with case statements not executing in client-server protocol!



## leigh507 (Feb 23, 2011)

I have been give an assignment to implement a simple Java download centre thingy, however i am having some trouble! . The problem lie in the protocol, we we given the Java knock knock example and had to adapt it to use switch statement instead of compound if statements. The protocol work before modification, but will not even execute the first Case loop! I have now been up for 36 hours because i think i am missing something really obvious. Here is the source code for the Client, server and protocol in that order... 
I would love some help please!!!

Leigh


```
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Assignment;
import java.io.*;
import java.net.*;
/**
 *
 * @author Leigh
 */
public class Assignment_1_client
{

    public static void main(String[] args) throws IOException {

        Socket kkSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            kkSocket = new Socket("Leigh-PC", 4444);
            out = new PrintWriter(kkSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: Leigh-PC.");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: taranis.");
            System.exit(1);
        }

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        String fromServer;
        String fromUser;

        while ((fromServer = in.readLine()) != null) {
            System.out.println("Server: " + fromServer);
            if (fromServer.equals("Bye."))
                break;

            fromUser = stdIn.readLine();
	    if (fromUser != null) {
                System.out.println("Client: " + fromUser);
                out.println(fromUser);
	    }
        }

        out.close();
        in.close();
        stdIn.close();
        kkSocket.close();
    }
}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Assignment;
import java.net.*;
import java.io.*;
/**
 *
 * @author Leigh
 */
public class Assignment_1_Server {
    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(4444);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            System.exit(1);
        }

        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(
				new InputStreamReader(
				clientSocket.getInputStream()));
        String inputLine, outputLine;
        Assignment_1_Protocol kkp = new Assignment_1_Protocol();

        outputLine = kkp.processInput(null);
        out.println(outputLine);

        while ((inputLine = in.readLine()) != null) {
             outputLine = kkp.processInput(inputLine);
             out.println(outputLine);
             if (outputLine.equals("Bye."))
                break;
        }
        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
    }
    }

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Assignment;

/**
 *
 * @author Leigh
 */
public class Assignment_1_Protocol{

    private static final int WAITING = 1;
    private static final int SENTREPLY = 2;
    private static final int SENTCHOICE = 3;
    private int state = WAITING;


    public String processInput(String theInput) {
        String theOutput = null;

        switch (state){
            case 1:{
                theOutput = "Terms of reference. Do you accept? Y or N";
                
                if (theInput.equalsIgnoreCase("Y")) {
            state = SENTREPLY;
                }
        }
            case 2: {
                theOutput = "1. computer program 2. picture 3. e-book";
                state = SENTCHOICE;
           int i = Integer.parseInt(theInput);
                 switch (i) {
                case 1: {
                    theOutput = " The program displays a message " ;
                }

                case 2: {
                    theOutput = "  The book is about...";
                }

                case 3: {
                    theOutput = "  The picture shows...";
                }

                default:{
                    theOutput = "That was not a valid selection";
                }

            }
     }
        }
   return theOutput;     }
    }
```


----------



## Kreij (Feb 23, 2011)

Don't you need break statments at the end of each case?


----------



## leigh507 (Feb 23, 2011)

These are the errors, 

run:
Exception in thread "main" java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(SocketInputStream.java:189)
        at java.net.SocketInputStream.read(SocketInputStream.java:121)
        at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
        at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
        at java.io.InputStreamReader.read(InputStreamReader.java:184)
        at java.io.BufferedReader.fill(BufferedReader.java:153)
        at java.io.BufferedReader.readLine(BufferedReader.java:316)
        at java.io.BufferedReader.readLine(BufferedReader.java:379)
        at Assignment.Assignment_1_client.main(Assignment_1_client.java:38)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)


----------



## leigh507 (Feb 23, 2011)

Also i have tried this and the code still wouldn't compile! Thanks for the swift help!


----------



## Kreij (Feb 23, 2011)

Connection problem? Got a firewall running or something that is blocking port access?
Just guessing here. 

I'm not super familiar with Java comiler and execution issues.


----------



## leigh507 (Feb 23, 2011)

Nope, as it works with the un-altered version of the code... its driving me mad! i have been on it for like 40 hours now! Going insane


----------



## ctrain (Feb 23, 2011)

what is going on with the { } brackets in your cases? weird usage, don't need them.

cases will fall through in java if i remember right, so remember to break when needed


----------



## morpha (Feb 24, 2011)

Kreij said:


> Don't you need break statments at the end of each case?



You do indeed.
Here is an example.


----------

