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