title: Client/server username/password authentication
body: <p>I am working on a Client/server authentication program, but I ran into a problem. The client makes the server connection fine, but once I type in my password and username it doesn't return whether it's a valid username/password.  If the user logins in with right username/password server is supposed to return "Welcome, username" and if it's invalid it returns "failed to login". I have looked at the printwriter and bufferedreader docs to make sure that I am using the right methods to pass the text between server/client properly. I tried debugging by printing the username and password on both the server and client to make sure that they are both listening/writing, which they seem to be, because it does print out the proper username/password. Can someone give me some insight on where I am going wrong? </p>
  
  <pre><code>public class Connect {
      private String USERNAME = "java";
      private String PASSWORD = "java";
      private int PORT = 9090;
      private String HOSTNAME = "localhost";
  
      public String getUsername(){
          return this.USERNAME;
      }
  
      public String getPassword(){
  
          return this.PASSWORD;
      }
  
      public int getPort(){
          return this.PORT;
      }
  
      public String gethostName(){
          return this.HOSTNAME;
      }
  }
  </code></pre>
  
  <p><br></p>
  
  <pre><code>import java.io.*;
  import java.io.net.*;
  public class Client {
      private final String FILENAME = null;
      Connect c = new Connect();
      Socket socket;
      BufferedReader read;
      PrintWriter output;
  
      public void startClient() throws UnknownHostException, IOException{
          //Create socket connection
          socket = new Socket(c.gethostName(), c.getPort());
  
          //create printwriter for sending login to server
          output = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
  
          //prompt for user name
          String username = JOptionPane.showInputDialog(null, "Enter User Name:");
  
          //send user name to server
          output.println(username);
  
          //prompt for password
          String password = JOptionPane.showInputDialog(null, "Enter Password");
  
          //send password to server
          output.println(password);
          output.flush();
  
          //create Buffered reader for reading response from server
          read = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  
          //read response from server
          String response = read.readLine();
          System.out.println("This is the response: " + response);
  
          //display response
          JOptionPane.showMessageDialog(null, response);
      }
  
      public void fileInfo(){
  
      }
  
      public static void main(String args[]){
          Client client = new Client();
          try {
              client.startClient();
          } catch (UnknownHostException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p><br></p>
  
  <pre><code>import java.io.*;
  import java.io.net.*;
  public class Server {
      private int currentTot;
      ServerSocket serversocket;
      Socket client;
      int bytesRead;
      Connect c = new Connect();
      BufferedReader input;
      PrintWriter output;
  
      public void start() throws IOException{
          System.out.println("Connection Starting on port:" + c.getPort());
          //make connection to client on port specified
          serversocket = new ServerSocket(c.getPort());
  
          //accept connection from client
          client = serversocket.accept();
  
          System.out.println("Waiting for connection from client");
  
          try {
              logInfo();
          } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  
      public void logInfo() throws Exception{
          //open buffered reader for reading data from client
          input = new BufferedReader(new InputStreamReader(client.getInputStream()));
  
          String username = input.readLine();
          System.out.println("SERVER SIDE" + username);
          String password = input.readLine();
          System.out.println("SERVER SIDE" + password);
  
          //open printwriter for writing data to client
          output = new PrintWriter(new OutputStreamWriter(client.getOutputStream()));
  
          if(username.equals(c.getUsername()) &amp;&amp;password.equals(c.getPassword())){
              output.println("Welcome, " + username);
          }else{
              output.println("Login Failed");
          }
  
      }
      public static void main(String[] args){
          Server server = new Server();
          try {
              server.start();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }       
  }
  </code></pre>