danielinthebed
01-01-2012, 12:31 PM
Happy New Year, could really do with some help in getting my client server program to run, been at it hours and can't seem to pin point what I'm missing. It's a download centre in this version a description is displayed based on 1 of the 3 options, I believe the problem lies in the protocol. Here's what I've done so far...
SERVER
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.getInputS…
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
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;
}
}
CLIENT
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.getInputStrea…
} 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();
}
}
Netbeans Runtime error
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)
SERVER
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.getInputS…
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
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;
}
}
CLIENT
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.getInputStrea…
} 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();
}
}
Netbeans Runtime error
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)