Results 1 to 3 of 3
- 12-31-2011, 11:36 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 16
- Rep Power
- 0
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
Java 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(); } }
Java 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(); } }
Java 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; } }
- 12-31-2011, 11:53 PM #2
Member
- Join Date
- Feb 2011
- Posts
- 16
- Rep Power
- 0
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)
- 01-01-2012, 12:00 AM #3
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
Similar Threads
-
Java Help Much Apreciated
By danielinthebed in forum Advanced JavaReplies: 22Last Post: 01-01-2012, 06:58 PM -
Problem! Help Apreciated
By danielinthebed in forum NetworkingReplies: 1Last Post: 02-20-2011, 03:00 PM -
Unable to execute my code can u plz help me out
By Gayathri12 in forum New To JavaReplies: 6Last Post: 07-21-2010, 08:28 AM -
Execute code from ActionListener
By Viola in forum New To JavaReplies: 3Last Post: 05-29-2010, 05:10 PM -
error stack when I try to execute my code
By paty in forum New To JavaReplies: 1Last Post: 08-02-2007, 08:32 PM
Bookmarks