In: Computer Science
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) {}
}
}
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();
}
}