Code Won't Execute, Help Apreciated
I'm making a Java download centre, the client can view a description of 3 items as provided by the server. I have a protocol to control what happens. The code however, won't compile properly. Here is what I've done so far. I believe the problem lies in the protocol program.
CLIENT
Code:
import java.net.*;
import java.io.*;
public class Client {
public static void main(String[] args) throws IOException {
Socket dcSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
dcSocket = new Socket("localhost", 3330);
out = new PrintWriter(dcSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(dcSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host");
System.exit(1);
} catch (IOException e) {
System.err.println("Could not get IO for connection");
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();
dcSocket.close();
}
}
SERVER
Code:
import java.net.*;
import java.io.*;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(3330); // Creates new ServerSocket object to listen on a specific port
} catch (IOException e) {
System.err.println("Message 1");
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept(); // Accepts connection from a client
} catch (IOException e) {
System.out.println("Message 2");
System.exit(-1);
}
// Opens readers and writers on socket input and output stream
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine, outputLine;
// Begin conversation with client program, creates protocol
DownloadProtocol dp = new DownloadProtocol();
outputLine = dp.processInput(null);
out.println(outputLine);
// Server reads and writes to socket
while ((inputLine = in.readLine()) != null) {
outputLine = dp.processInput(inputLine);
out.println(outputLine);
if (outputLine.equals("N")) {
break;
}
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}
PROTOCOL
Code:
import java.net.*;
import java.io.*;
public class DownloadProtocol {
private int state = WAITING;
private static final int WAITING = 1;
private static final int REPLY = 2;
private static final int CHOICE = 3;
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 = REPLY;
}
}
case 2: {
theOutput = "1. computer program 2. picture 3. e-book";
state = CHOICE;
int i = Integer.parseInt(theInput);
switch (i) {
case 1: {
theOutput = "The program displays a message";
break;
}
case 2: {
theOutput = "The book is about";
break;
}
case 3: {
theOutput = "The picture shows";
break;
}
default: {
theOutput = "Invalid choice";
break;
}
}
}
}
return theOutput;
}
}
Re: Code Won't Execute, Help Apreciated
Netbeans errors
run:
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream. java:168)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.j ava:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.ja va:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:1 58)
at java.io.InputStreamReader.read(InputStreamReader.j ava:167)
at java.io.BufferedReader.fill(BufferedReader.java:13 6)
at java.io.BufferedReader.readLine(BufferedReader.jav a:299)
at java.io.BufferedReader.readLine(BufferedReader.jav a:362)
at Version_1.Client.main(Client.java:38)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
run:
Exception in thread "main" java.lang.NullPointerException
at Version_1.DownloadProtocol.processInput(DownloadPr otocol.java:25)
at Version_1.Server.main(Server.java:39)
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)
Re: Code Won't Execute, Help Apreciated
Please only post one thread per problem.
This is appears the same as
http://www.java-forums.org/advanced-...preciated.html