• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

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

leigh507

New Member
Joined
Feb 23, 2011
Messages
4 (0.00/day)
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

Code:
/*
 * 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

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
Don't you need break statments at the end of each case?
 

leigh507

New Member
Joined
Feb 23, 2011
Messages
4 (0.00/day)
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

New Member
Joined
Feb 23, 2011
Messages
4 (0.00/day)
Also i have tried this and the code still wouldn't compile! Thanks for the swift help!
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
Connection problem? Got a firewall running or something that is blocking port access?
Just guessing here. :eek:

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

leigh507

New Member
Joined
Feb 23, 2011
Messages
4 (0.00/day)
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

New Member
Joined
Jan 12, 2010
Messages
393 (0.08/day)
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
 
Last edited:
Joined
Jan 15, 2009
Messages
440 (0.08/day)
Location
Australia
System Name Krogoth
Processor AMD Ryzen 5 5600G @stock
Motherboard Gigabyte B550 AORUS ELITE AX V2
Cooling Stock CPU cooler + 2x Intake 120mm @ 8v + 2x outtake 120mm @8v
Memory G.Skill Ripjaws V 32GB (2x16GB) PC4-28800 DDR4
Video Card(s) MSI Radeon R9 290 Gaming 4GB GDDR5
Storage 1x 4tb WD HDD, 1x1Tb Intel m.2 SSD
Display(s) 2x 27" IPS on swing arms. @UHD
Case Antec Solo II
Audio Device(s) HyoperX Cloud 2 Wireless Headphones
Power Supply I forgot.
Mouse Logitech G302
Keyboard Lenovo legion K500 RGB
Software Windows 11 64bit
Top