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.io.*;

public class Server extends Thread{//multiple services each is a thread on different port
   ServerSocket server;
   Socket client;
   int port;
  
  
   public Server(int port) {
       this.port=port;
       server= new ServerSocket (port);
       run();
   }
   public void run() {
       try {
           while(true) {
               server = new ServerSocket(port);
               client = server.accept();
               switch(port) {
               case 4000:
                   new ServiceServer0(client).start();
                   break;
               case 4001:           
                   new ServiceServer1(client).start();
                   break;
               default:
                   System.out.println("Invalid port");
                   System.exit(0);
                  
               }
           }
       }catch(IOException ioe) {}
   }

   public static void main(String[] args) {
       new Server (4000).start();
       new Server(4001).start();
      
   }

}

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 ServiceServer1 extends Thread{
   Socket client;
   Scanner fromNet = null;
   Formatter toNet = null;
   static int[]votes = new int[10];//one instance to accumulate voting results
   public ServiceServer1(Socket client) {
       this.client = client;
   }
   public void run() {
       System.out.println("Voting Service: Serving client ...");
       try {
           fromNet = new Scanner(client.getInputStream());
           toNet = new Formatter(client.getOutputStream());
           int vote = Integer.parseInt(fromNet.nextLine());
           ++votes[vote];
          
           showVotes();
       }catch(IOException ioe) { }
   }
   void showVotes() {
      
   }
}

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

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);

           //to dedecate with the server

           fromNet = new Scanner(server.getInputStream());

           toNet = new Formatter(server.getOutputStream());

           for(int i=0;i<3;i++){

           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!!");

           }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.println("Enter your vote 0-9: ");

                   String vote = fromUser.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.io.*;

public class Server extends Thread{//multiple services each is a thread on different port
   ServerSocket server;
   Socket client;
   int port;
  
  
   public Server(int port) {
       this.port=port;
       server= new ServerSocket (port);
       run();
   }
   public void run() {
       try {
           while(true) {
               server = new ServerSocket(port);
               client = server.accept();
               switch(port) {
               case 4000:
                   new ServiceServer0(client).start();
                   break;
               case 4001:           
                   new ServiceServer1(client).start();
                   break;
               default:
                   System.out.println("Invalid port");
                   System.exit(0);
                  
               }
           }
       }catch(IOException ioe) {}
   }

   public static void main(String[] args) {
       new Server (4000).start();
       new Server(4001).start();
      
   }

}

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 ServiceServer1 extends Thread{
   Socket client;
   Scanner fromNet = null;
   Formatter toNet = null;
   static int[]votes = new int[10];//one instance to accumulate voting results
   public ServiceServer1(Socket client) {
       this.client = client;
   }
   public void run() {
       System.out.println("Voting Service: Serving client ...");
       try {
           fromNet = new Scanner(client.getInputStream());
           toNet = new Formatter(client.getOutputStream());
           int vote = Integer.parseInt(fromNet.nextLine());
           ++votes[vote];
          
           showVotes();
       }catch(IOException ioe) { }
   }
   void showVotes() {
      
   }
}

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) {}
      
   }

}


Related Solutions

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...
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();...
Python: I want to make the following code to prompt the user if want to run...
Python: I want to make the following code to prompt the user if want to run the software again. I am new to python, so i do not know if it works like c++. If can explain that would be much appreciated base = float(input("Enter base of the triagle: ")) Triangle_Right = float(input("Enter right side of the triagle: ")) Triangle_Left = float(input("Enter left side of the triagle: ")) height = float(input("Enter the height of the triangle: ")) perimiter = base...
I marked the correct answers to these questions, but I just want to know how to...
I marked the correct answers to these questions, but I just want to know how to solve them. 1) In a cross of AaBbCcDdEeFf X AaBbccDdEeFf, what proportion will have the ABCDeF phenotype? A. 27/64 B. 27/128 C. 27/512 D. 81/512 E. 81/2048 #### 2.) In a cross of two flies +/vg Cy/+ +/se +/ab X +/vg +/+ se/se ab/ab what proportion of the offspring will be mutant in phenotype for all four markers? A. 0 B. 3/64 C. 1/16...
Show the code in matlab to display an ECG graph (I do not want code that...
Show the code in matlab to display an ECG graph (I do not want code that simply calls the ecg function in matlab but how to write that kind of code)
If there are 32 concurrent processes, how will you modify the following code? Process i do...
If there are 32 concurrent processes, how will you modify the following code? Process i do { while (turn == j);                critical section; turn = j;                remainder section } while (true);
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT