Click here to Skip to main content
15,892,839 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 {
			//Detecting the localhost's ip address
			InetAddress localaddr = InetAddress.getLocalHost();
			System.out.println("SERVER\n");
			System.out.println ("Local hostnameIP: " + localaddr );

			// Creating a server socket for connection
			ServerSocket srvr = new ServerSocket(1234);
			System.out.println("Waiting for connection on "+localaddr);
			// Accept incoming connection
			Socket skt = srvr.accept();
			System.out.print("Server has connected!\n");
			// get Input and Output streams
			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())); //read input for login
			System.out.print("Sending string: '" + welcome + "'\n");
			out.println(welcome);
			InetAddress clientInetAddress = skt.getInetAddress();
			String ip = clientInetAddress.getHostAddress();
			
			//read file

			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");
						
						//closing server
//						out.println("You are banned. Server closing.");
//						out.close();
//						skt.close();
//						srvr.close();
					}
				}
				br.close();
				
				
			}
			catch (FileNotFoundException ex) {
				System.out.println("Unable to open file '" + checkIP + "'");
			}
			catch(IOException ex) {
				System.out.println("Error reading file '" + checkIP + "'");
			}
			
			//login attempts
			int tries = 4;
			while (tries>0) {
				out.println(login);
				
				//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  { //if wrong login - give 3 more tries
					
					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();
				
				//ban ip address permanently 
				System.out.println("local: " + localaddr.getHostAddress()); 
				System.out.println("client: " + ip);
				 
				 //write ip address to file
				writer.println(ip);
				writer.close();
				
			}
Posted
Comments
Richard MacCutchan 27-Mar-18 4:48am    
It should be a simple matter to print the two values to see why they do not match.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900