0

I have been tasked to create a server which emulates a calculator and which on encountering an operation other than '+','-','*' & '/' throws an Exception to the client. but I can't figure out how to send the Exception generated in the Server to the Client.

Initially, I naively thought that I could easily do this by dout.writeUTF(e.getMessage()) in the catch block.


    package networking;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.*;

class OperationDeniedException extends Exception {
    private String op;

    public OperationDeniedException(String op) {
        this.op = op;
        // TODO Auto-generated constructor stub
    }

    public String toString() {
        return "OperationDeniedException: Operation " + op + " is Denied by server";
    }
}

public class CalcServer {

    public static void main(String[] args) {
        String opr, op1, op2;
        double Dop1, Dop2;
        boolean divbyzero = false;
        DataInputStream din;
        DataOutputStream dout;
        PrintWriter p;
        try {
            ServerSocket ss = new ServerSocket(4444);
            Socket s = ss.accept();
            din = new DataInputStream(s.getInputStream());
            dout = new DataOutputStream(s.getOutputStream());
            p = new PrintWriter(s.getOutputStream());
            dout.writeUTF(opmsg);
            opr = din.readUTF();
            System.out.println(opr);
            try {
                System.out.println("Inside try for custom exception");
                if (opr.charAt(0) != '+' && opr.charAt(0) != '-' && opr.charAt(0) != '*' && opr.charAt(0) != '/') {
                    throw new OperationDeniedException(opr);
                }
            } catch (OperationDeniedException e) {
                String msge = e.getMessage();
                System.out.println("just before write");
                dout.writeUTF((String) e.toString());
                System.out.println("exception sent");
                System.exit(0);
            }
            dout.writeUTF("Send the first Operand");
            op1 = din.readUTF();
            Dop1 = Double.parseDouble(op1);
            dout.writeUTF("Send the Second Operand");
            op2 = din.readUTF();
            Dop2 = Double.parseDouble(op2);
            switch (opr) {
            case "+":
                Dop1 += Dop2;
                break;
            case "-":
                Dop1 -= Dop2;
                break;
            case "*":
                Dop1 *= Dop2;
                break;
            case "/":
                if (Dop2 == 0.0) {
                    divbyzero = true;
                    break;
                }
                Dop1 /= Dop2;
                break;

            default:
                break;
            }
            if (divbyzero) {
                dout.writeUTF("Second operand is 0 and can't divide bt zero");
            }
            dout.writeUTF("Ans of the operation is= " + Double.toString(Dop1));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    static final String opmsg = "Select one of the following operations +, -, *, /.";

}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Ask for clarifications to the person who gave you that task. We don't know anything about your functional and technical requirements. He/she does. – JB Nizet Mar 24 '19 at 10:20
  • 1
    The approach you've considered is the right idea. How is that not working for you? You need to provide more information with your question so that we know exactly what you're trying to do, and what behavior you're getting that is not what you want, and why. Include the relevant part of your code in your question. Show text results you're getting, like error messages, in your question as well – CryptoFool Mar 24 '19 at 10:25
  • By the way, nowadays I'd recommend to base client / server communications on HTTP + JSON (often misnamed as REST), and not to use plain sockets, RMI, CORBA, or RMI-IIOP for new applications. – Ralf Kleberhoff Mar 24 '19 at 11:13
  • Steve I made the code work but I think it's a crude solution to a complex problem I paste the code below, if you have the time can you suggest a better way of doing it. ps: e.getMessage() didn't work with writeUTF() for some reason it gave a runtime error so I ended up using e.toString. – Sahil Mahale Mar 24 '19 at 11:49
  • @RalfKleberhoff Oh this is just a class assignment but thanks for the suggestions – Sahil Mahale Mar 24 '19 at 12:03

1 Answers1

0

So the solution was moving the catch block of my custom exception in the scope of my DataOutputStream object to send the message to the client by doing dout.writeUTF(e.getMessage()); Sorry to the people whose time I wasted. if there a better way then please do post.