I'm making a program where the user logs into the server with a username and password only the server knows. They have 4 tries to get the correct username and password. If they do not enter the correct login information in 4 tries, the server will close connection to the client.
The next part of the program which I need help with is permanently banning the user from connecting for further attempts. When the user is logging in for the first time and gets all 4 attempts wrong, their ip address is written to a file called "userIP.txt".
What I tried to do was read the file and if it matches the user's IP address, they will be banned from the program. It doesn't work - when they come back to the program it lets them log in again.
I know this may not be the best way to ban a user from the server, but my assignment requires me to ban their ip address.
Any ideas how I can fix this?
Here is part of the server code:
What I have tried:
import java.lang.*;
import java.io.*;
import java.net.*;
class Server {
public static void main(String args[]) throws FileNotFoundException {
String welcome = "Welcome! The server is now connected.";
String login = "Enter username and password: ";
String message;
PrintWriter writer = new PrintWriter("userIP.txt");
try {
InetAddress localaddr = InetAddress.getLocalHost();
System.out.println("SERVER\n");
System.out.println ("Local hostnameIP: " + localaddr );
ServerSocket srvr = new ServerSocket(1234);
System.out.println("Waiting for connection on "+localaddr);
Socket skt = srvr.accept();
System.out.print("Server has connected!\n");
PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
out.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(skt.getInputStream()));
BufferedReader log = new BufferedReader(new InputStreamReader(skt.getInputStream()));
System.out.print("Sending string: '" + welcome + "'\n");
out.println(welcome);
InetAddress clientInetAddress = skt.getInetAddress();
String ip = clientInetAddress.getHostAddress();
String checkIP = "userIP.txt";
String line = null;
try {
FileReader readFile = new FileReader (checkIP);
BufferedReader br = new BufferedReader (readFile);
while ((line = br.readLine())!= null) {
System.out.println("reading file: " + line);
if (line.equals(ip)) {
System.out.println("IP MATCHES");
}
}
br.close();
}
catch (FileNotFoundException ex) {
System.out.println("Unable to open file '" + checkIP + "'");
}
catch(IOException ex) {
System.out.println("Error reading file '" + checkIP + "'");
}
int tries = 4;
while (tries>0) {
out.println(login);
String username = in.readLine();
System.out.println("Client's username: " + username);
String password = in.readLine();
System.out.println("Client's password: " + password);
if (username.equals("hello123") && password.equals("mypass")) {
out.println("Correct login!");
System.out.println ("Client's IP Address: " + ip);
tries=-1;
}
else {
tries--;
System.out.println("Number of tries left: " + tries);
out.println("Try again. Login attempts left - " + tries);
}
}
if (tries==0){
out.println("Wrong login - server closing");
out.close();
skt.close();
srvr.close();
System.out.println("local: " + localaddr.getHostAddress());
System.out.println("client: " + ip);
writer.println(ip);
writer.close();
}