ook this code is a correct but i want modify it to allow the client to have
Question:
ook 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", user);
toNet.flush();
toNet.format("%s", 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", 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; iif(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", 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) {}
}
}
Vector Mechanics for Engineers Statics and Dynamics
ISBN: 978-0073212227
8th Edition
Authors: Ferdinand Beer, E. Russell Johnston, Jr., Elliot Eisenberg, William Clausen, David Mazurek, Phillip Cornwell