Got a problem I really can't figure out with my Java assignment. It's a client-server setup that's supposed to implement a protocol we've designed. The code is based on some that was provided for one of the seminars we did a few weeks ago that our group has modified to allow the client to send more than one message and to accept various responses from the server, the server will eventually do all the validation on the input (almost certainly the worst way to do it but I don't trust the coding skills of the person who was working on the client to do it properly).
The original code allowed the server to receive messages and return a basic response and the client would end as soon as it received the response from the server, unfortunately it doesn't seem to be working properly now as it does the first message-response fine but then any future messages don't seem to get to the server. I've run a packet tracer on it and it seems like the client is sending the data to the right port and it's being received but the server isn't detecting it.
The server code is:
Code:
import java.net.*;
import java.io.*;
public class Server {
private static String message =
"Welcome to CE-T ";
private static String incoming = null;
private static InputStream sin;
private static OutputStream sout;
private static class ServeClient extends Thread {
private Socket sock;
int len = 0;
byte [] messBuffer = new byte [100];
public ServeClient (Socket s) {
sock = s;
}
public void run () {
System.out.println ("** Server thread started");
String outbuff = new String (message + "\r\f");
try {
sin = sock.getInputStream ();
len = sin.read (messBuffer);
incoming = new String (messBuffer, 0, len);
if(validateInput(incoming, "success")) {
message = "message accepted at server";//outputMessage(incoming);
sout = sock.getOutputStream ();
sout.write (message.getBytes ());
System.out.println ("** Message sent");
}
else {
message = "5003";
sout = sock.getOutputStream ();
sout.write (message.getBytes ());
System.out.println ("** Message sent");
}
}
catch (IOException x ) {
System.out.println ("** Problem serving client" + x );
}
System.out.println ("** Server thread finished");
}
private String outputMessage(String inputMess) {
return inputMess;
}
private boolean validateInput(String inputMess, String type) {
String inputMessage;
if (inputMess.length() == 1) {
inputMessage = inputMess;
}
else {
inputMessage = inputMess.substring(0,2);
}
inputMessage = inputMessage.trim();
System.out.println(inputMessage);
if ((inputMessage.equals("q"))||(inputMessage.equals("Q"))) {
System.out.println("output accepted");
if (type == "success") {
return true;
}
}
else if ((inputMessage.equals("nu")) || (inputMessage.equals("NU"))) {
if (type == "success") {
return true;
}
else {
}
}
else if ((inputMessage.equals("ln")) || (inputMessage.equals("LN"))) {
if (type == "success") {
return true;
}
else {
}
}
else if ((inputMessage.equals("tr")) || (inputMessage.equals("TR"))) {
if (type == "success") {
return true;
}
else {
}
}
else if (inputMessage.equals("?")) {
if (type == "success") {
return true;
}
else {
}
}
return false;
}
}
private static int getPort ()
{
BufferedReader in = new BufferedReader
(new InputStreamReader (System.in));
try {
int p = 0;
boolean incorrect;
do {
System.out.print ("Enter port number: ");
try {
p = Integer.parseInt (in.readLine ());
try {
new InetSocketAddress ("0.0.0.0", p);
incorrect = false;
}
catch (IllegalArgumentException x) {
System.out.println ("Port number unreachable");
incorrect = true;
}
}
catch (NumberFormatException x) {
System.out.println ("I said, NUMBER!");
incorrect = true;
}
} while (incorrect);
return p;
}
catch (IOException x) {
throw new Error ("Unexpected error -> " + x);
}
}
public static void main (String [] para) {
int port = getPort ();
try {
ServerSocket server = new ServerSocket (port, 5);
System.out.println ("ServerSocket created");
while (true) {
System.out.println ("Ready to receive");
Socket socket = server.accept ();
System.out.println
("Connection received and " +
"server thread started");
Thread t = new ServeClient (socket);
t.start ();
}
}
catch (IOException x) {
System.out.println ("Socket problem: " + x );
}
}
}
the client code is
Code:
import java.net.*;
import java.io.*;
import java.util.*;
public class Main {
private static BufferedReader in;
private static Socket socket;
private static String message;
private static OutputStream sout;
private static boolean checker;
private static InetSocketAddress addr;
private static InetSocketAddress readHostAndPort () {
in = new BufferedReader
(new InputStreamReader (System.in));
try {
System.out.print ("Please enter the IP Address: ");
String h = in.readLine ();
int p = 30;
boolean incorrect;
InetSocketAddress addr = null;
do {
try {
try {
addr = new InetSocketAddress (h, p);
incorrect = false;
}
catch (IllegalArgumentException x) {
System.out.println ("Port Number 30 is unreachable. Closing down.");
incorrect = true;
}
}
catch (NumberFormatException x) {
System.out.println ("The port should be a number. Error.");
incorrect = true;
}
} while (incorrect);
return addr;
}
catch (IOException x) {
throw new Error("Unexpected exception occurred: " + x + ". Closing down.");
}
}
private static void receiveMessages(String id){
int test = Integer.parseInt(id);
switch(test){
case 5001: System.out.println("Error: Incorrect number of paramaters");break;
case 5002: System.out.println("Error: User already exists");break;
case 5003: System.out.println("Error: Invalid Command");break;
case 5004: System.out.println("Error: Invalid details entered");break;
case 5005: System.out.println("Error: Session timed out");break;
case 5006: System.out.println("Error: Server connection failure");break;
case 9999: System.out.println("Error: Unknown error");break;
case 0001: System.out.println("Success: Data returned");break;
case 0002: System.out.println("Success: User Created");break;
case 0003: System.out.println("Success: Session ID created");break;
case 0004: System.out.println("Success: Translation request created and quotation returned");break;
case 0005: System.out.println("Success: All usable commands returned");break;
}
}
public static void main (String [] args) {
byte [] buff = new byte [100];
int len = 0;
socket = new Socket ();
checker = true;
in = new BufferedReader
(new InputStreamReader (System.in));
message = "";
addr = readHostAndPort ();
if (addr.isUnresolved () ) {
System.out.print ("Couldn't resolve socket address - ");
System.out.println ("bad host = " + addr.getHostName ());
} else {
System.out.print ("Socket address resolved as ");
System.out.println (addr.getHostName () +
"/" + addr.getPort ());
try {
System.out.println ("Trying to connect to server...");
socket.connect (addr);
System.out.println ("Connected to " +
socket.getInetAddress ().getHostName ());
while (checker) {
sendMessage();
}
/** sout = socket.getOutputStream ();
sout.write (message.getBytes ());
System.out.println ("I've sent a message");
InputStream sin = socket.getInputStream ();
len = sin.read (buff);
// extract message NOT assuming last 2 bytes are "\r\f"
message = new String (buff,0, len);
*/}
catch (IOException x) {
System.out.println ("Error occured with the connection: " + x );
}
}/**
System.out.println ("Socket client finished");
sendMessage();*/
}
public static void sendMessage(){
in = new BufferedReader
(new InputStreamReader (System.in));
int len = 0;
byte [] buff = new byte [100];
System.out.println("Input a command to start: ");
String message;
try {
message = in.readLine ();
if ((message.equals("exit"))||(message.equals("EXIT"))) {
checker = false;
}
} catch (IOException aardvark) {
throw new Error ("Error in processing command");
}
try {
sout = socket.getOutputStream ();
sout.write (message.getBytes ());
InputStream sin = socket.getInputStream ();
len = sin.read (buff);
// extract message NOT assuming last 2 bytes are "\r\f"
message = new String (buff,0, len);
}
catch (IOException x) {
System.out.println ("Error in communicating with server: " + x );
}
if (message.length() == 4) {
receiveMessages(message);
}
else {
System.out.println ("Message recevied from server: " +
message);
}
}
}
I can post the output from wireshark if needed. I don't see anything obviously wrong with the code as it works fine if you close the client and reopen it and send another message, it just doesn't seem to like sending and receiving more than one.
If anyone can possibly point out either where I've gone wrong or what I need to look into then I'd be very greatful, even hints as to what I should be looking at would be a good start.
Edit: I'm aware the code isn't commented, that will be done as soon as I figure out what is causing the problem. I'd rather have working code than well commented not-working code.
Bookmarks