Question

In: Computer Science

look this code is a correct but i want modify it to allow the client to...

look this code is a correct but i want modify it to allow the client to have three attempts to login to the server

package hw2;

import java.net.*;
import java.util.Formatter;
import java.util.Random;
import java.util.Scanner;
import java.io.*;

public class Client {
   Socket server;
   int port;
   Formatter toNet = null;
   Scanner fromNet = null;
   Scanner fromUser = new Scanner(System.in);

   public Client() {
       try {
           // login at server at local host port 4000
           server = new Socket("localhost",4000);
           System.out.println("UserName: ");
           String user = fromUser.nextLine();
           System.out.println("Password: ");
           String pass = fromUser.nextLine();
           //to dedecate with the server
           fromNet = new Scanner(server.getInputStream());
           toNet = new Formatter(server.getOutputStream());
           toNet.format("%s\n", user);
           toNet.flush();
           toNet.format("%s\n", pass);
           toNet.flush();
           String response=fromNet.nextLine();
           if(!response.equals("valid") {
               System.out.println("Invalid Login!!");
           }else {
               // if login is successful vote at local host port 4001
               server = new Socket("localhost",4001);//establish new connection
               //establish a strem
               toNet = new Formatter(server.getOutputStream());
               System.out.prinln("Enter your vote 0-9: ");
                   String vote = formUser.nextLine();
                   toNet.format("%s\n", vote);
                   toNet.flush();
           }
              
       } catch (IOException ioe) { }
   }

   public static void main(String[] args) {
           new Client();
   }
}

package hw2;
import java.net.*;
import java.util.Formatter;
import java.util.Scanner;
import java.io.*;

public class ServiceServer0 extends Thread{//login multithreaded service
   Socket client;
   Scanner fromNet = null;
   Formatter toNet = null;
   private String login[][] = { { "user1", "pass1" }, { "user2", "pass2" }, { "user3", "pass3" }, { "user4", "pass4" },
           { "user5", "pass5" }, { "user6", "pass6" }, { "user7", "pass7" }, { "user8", "pass8" },
           { "user9", "pass9" }, };
   public ServiceServer0(Socket client) {
       this.client = client;
   }
   public void run() {
       System.out.println("Login Service: Serving client ...");
       try {
           fromNet = new Scanner(client.getInputStream());
           toNet = new Formatter(client.getOutputStream());
           String user = fromNet.nextLine(); //read the user
           String pass = fromNet.nextLine(); // read the pass
           String respone = " ";
           boolena fount = false;
           for (int i=0; i<long.length; i++) {
               if(login[i][0].equals(user) && login[i][1].equals(pass)) {
                   respone="valid";
                   found = true;
                   break; // to go out from for loop
               }
           }
          
           if(!found)
               response= "Invalid";
              
           toNet.format("%s\n", response);
           toNet.flush();
          
       }catch(IOException ioe) {}  
   }
}

package hw2;
import java.net.*;
import java.util.Formatter;
import java.util.Scanner;
import java.io.*;

public class ServiceServer2 extends Thread{//other services in similar manner
   Socket client;
   Scanner fromNet = null;
   Formatter toNet = null;

   public ServiceServer2(Socket client) {
       this.client = client;
   }
   public void run() {
       System.out.println("ServiceServer2: Serving client ...");
       try {
           fromNet = new Scanner(client.getInputStream());
           toNet = new Formatter(client.getOutputStream());

          
       }catch(IOException ioe) {}
      
   }

}

Solutions

Expert Solution

You have multiple ways to allow 3 attemps to login

1) maintain a Map<String,Boolean> userloginattemptedmap which will conatins userid vs allowedFlag. set it false when user attemped 3 times to login.

2) simple count variable to check the attempt made.

i have described 2nd one here...For more dynamic code you can use 1st one as well.

package hw2;

import java.net.*;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
import java.io.*;

public class Client {
Socket server;
int port;
Formatter toNet = null;
Scanner fromNet = null;
Scanner fromUser = new Scanner(System.in);
Map<String,Boolean> map=new HashMap<String,Boolean>();

public Client() {
try {
// login at server at local host port 4000
server = new Socket("localhost",9200);
//to dedecate with the server
fromNet = new Scanner(server.getInputStream());
toNet = new Formatter(server.getOutputStream());
int count=0;
while(count>=0&&count<3) {
System.out.println("UserName: ");
String user = fromUser.nextLine();
System.out.println("Password: ");
String pass = fromUser.nextLine();
  
toNet.format("%s\n", user);
toNet.flush();
toNet.format("%s\n", pass);
toNet.flush();
String response=fromNet.nextLine();
if(!response.equals("valid")) {
System.out.println("Invalid Login!!");
count++;
}else {
   count=-1;
// if login is successful vote at local host port 4001
server = new Socket("localhost",4001);//establish new connection
//establish a strem
toNet = new Formatter(server.getOutputStream());
System.out.println("Enter your vote 0-9: ");
String vote = fromUser.nextLine();
toNet.format("%s\n", vote);
toNet.flush();
}
}
if(count==3) {
   System.out.println("You have reached maximum allowed login request! please contact the Administrator");
}
  
} catch (IOException ioe) {
   ioe.printStackTrace();
}
}

public static void main(String[] args) {
new Client();
}
}


Related Solutions

look this code is a correct but i want modify it to allow the client to...
look this code is a correct but i want modify it to allow the client to have three attempts to login to the server package hw2; import java.net.*; import java.util.Formatter; import java.util.Random; import java.util.Scanner; import java.io.*; public class Client {    Socket server;    int port;    Formatter toNet = null;    Scanner fromNet = null;    Scanner fromUser = new Scanner(System.in);    public Client() {        try {            // login at server at local host...
Could you modify my code so it meets the following requirement? (Python Flask) I want the...
Could you modify my code so it meets the following requirement? (Python Flask) I want the user to register for account using email and password, then store that data into a text file. Then I want the data to be read when logging in allowing the user to go to home page. -------------Code-------------------- routes.py from flask import Flask, render_template, redirect, url_for, request, session import json, re app = Flask(__name__) '''@app.before_request def before_request(): if 'visited' not in session: return render_template("login.html") else:...
These code are correct, and I want some explanation or comment for them(purpose for each function)...
These code are correct, and I want some explanation or comment for them(purpose for each function) def annoying_factorial(n): if n == 0 or n == 1: return 1 if n == 2: return 2 if n == 3: return 6 if n == 4: return 4 * annoying_factorial(3) if n == 5: return 5 * annoying_factorial(4) if n == 6: return 6 * annoying_factorial(5) else: return n * annoying_factorial(n-1) def annoying_fibonacci(n): if n==0: return 0 if n==1: return 1 if...
Modify the following code to make the input and output look like this. Input 5 Vader...
Modify the following code to make the input and output look like this. Input 5 Vader 1300 Kirk 1250 Adama 1000 Reynolds 1615 Oneill 1470 Output Enter the number of candidates: 5 Enter candidate's name :Vader 1300 Enter votes received :Enter candidate's name :Kirk 1250 Enter votes received :Enter candidate's name :Adama 1000 Enter votes received :Enter candidate's name :Reynolds 1615 Enter votes received :Enter candidate's name :Oneill 1470 Enter votes received : Name Votes Percentage Vader 1300.00 19.59% Kirk...
I WANT THIS CODE TO BE SIMPLE BECAUSE I AM NEW TO CODING AND I WANT...
I WANT THIS CODE TO BE SIMPLE BECAUSE I AM NEW TO CODING AND I WANT TO KNOW THE DETAILS! straight C Program. write a quiz game where a number of questions are asked and the user will win each time he answers right. and he loses each time he answers wrong. the type of questions asked can be about sports or anything else. You can put 5 quiz questions. It should only include the following: #include <stdio.h> and #include...
this is my code I want the opposite i want to convert a postfix expression to...
this is my code I want the opposite i want to convert a postfix expression to infix expression #include <iostream> #include <string> #define SIZE 50 using namespace std; // structure to represent a stack struct Stack {   char s[SIZE];   int top; }; void push(Stack *st, char c) {   st->top++;   st->s[st->top] = c; } char pop(Stack *st) {   char c = st->s[st->top];   st->top--;   //(A+B)*(C+D)   return c; } /* function to check whether a character is an operator or not. this function...
I don't want explanation , I just want a correct answer, Very quickly. 12- If the...
I don't want explanation , I just want a correct answer, Very quickly. 12- If the slope of the consumption function is equal to one, then Select one: a. The multiplier is undefined b. The multiplier is smaller than one c. The multiplier is just one d. The multiplier is larger than one e. None of the above 11- If the nominal interest rate 8% and expected inflation 5%, the expected real interest rate in year t is approximately Select...
how to correct this java code so that i get the correct day of week? and...
how to correct this java code so that i get the correct day of week? and test the year public static void main(String[] args) {        //               Scanner s = new Scanner(System.in); //needed info //year month, day int year, month, dayOfMonth; // add day of week , century yr int dayOfWeek, century, yearOfCentury;    //user inputs year System.out.print("Enter year: (example, 2020):"); year = s.nextInt(); //user inputs month by number System.out.print("Enter month: 1-12:"); month = s.nextInt();...
I have to modify the following code to: 1. Use the unique algorithm to reduce the...
I have to modify the following code to: 1. Use the unique algorithm to reduce the array to unique values 2. Use the copy algorithm to display the unique results. #include<iostream> #include<vector> #include<algorithm> using namespace std; int main() {     //creating an array of 20 integers     int array[20];     //creating an empty vector     vector<int> vec;     //input from end user to get 20 ints and a for loop to interate through 20     cout << "Enter 20 integers:"...
Hi, I have this code so far and need to modify it so that the output...
Hi, I have this code so far and need to modify it so that the output does not print something like 2x^0 but instead will just print 2. Currently it prints 2x^0. I also am having a problem with the output of Polynomial 1 - Polynomial 2. If both coefficient values for the polynomials are equal instead of Polynomial 1 - Polynomial 2 = 0 it outputs nothing. Just Polynomial 1 - Polynomial 2 = For example if I input...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT