Input of a blockchain voting java program has a loop problem

103 views Asked by At

I would be so thankfull if someone can help me out with this program that i want to test out. It's an E-voting program, blockchain based.

The problem i'm getting is on the first option, Cast Votes. It asks for role, in order for it to work, u first need to run the server, and than run client which will connect to the server. So, when u enter server on the input, it's all okay, but then, u try to enter client server_address port which will connect the client to the server, and it just doesn't do anything.

private static final String DEFAULT_SERVER_ADDR = "localhost";
private static final int DEFAULT_PORT = 6777;

/*
 * Everything starts from here!
 */
public static void main(String[] args) {
    /*int clientId=0;*/
    System.out.println(" ----- MAIN MENU ----- \n");
    System.out.println("1. Cast Votes");
    System.out.println("2. View Votes on Blockchain");
    System.out.println("3. Count Votes");
    System.out.println("0. Exit\n");

    Scanner scanner = new Scanner(System.in);

    System.out.println("Enter your choice: ");
    int ch = scanner.nextInt();

    if(ch == 1)
    {
        System.out.println("\n ----- Casting Votes ----- \n");
        System.out.println("Please choose a role you want to be: server or client.");
        System.out.println("server PORT - The port to listen to; \"6777\" is default port.");
        System.out.println("client SERVER_ADDRESS PORT - The server address and port to connect to; \"localhost:6777\" is default address-prt combination.");
        System.out.println("Make sure run the server first and then run client to connect to it.");
        System.out.println("> ---------- ");

        Scanner in = new Scanner(System.in);
        String line = in.nextLine();
        String[] cmd = line.split("\\s+");

        if (cmd[0].contains("s"))
        {   // server selected

            /* work as server */
            int port = DEFAULT_PORT;
            if (cmd.length > 1) {
                try {
                    port = Integer.parseInt(cmd[1]);
                } catch(NumberFormatException e) {
                    System.out.println("Error: port is not a number!");
                    in.close();
                    return;
                }
            }

            ServerManager _svrMgr =new ServerManager(port);
            new Thread(_svrMgr).start();


        }
        else if (cmd[0].contains("c"))
        {
            //client selected

            /* work as client */
            String svrAddr = DEFAULT_SERVER_ADDR;
            int port = DEFAULT_PORT;
            if (cmd.length > 2) {
                try {
                    svrAddr = cmd[1];
                    port = Integer.parseInt(cmd[2]);
                } catch(NumberFormatException e) {
                    System.out.println("Error: port is not a number!");
                    in.close();
                    return;
                }
            }

            ClientManager _cltMgr = new ClientManager(svrAddr, port);

            /* new thread to receive msg */
            new Thread(_cltMgr).start();

            _cltMgr.startClient();
        }
        else {
            showHelp();
            in.close();
            return;
        }
        in.close();
    }
0

There are 0 answers