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