0

I'm making a simple client server application where you enter a password on the server, the client enters a password which is then sent to server and compared. The password isn't sending using the GUI I've made, however if I scan a string using the console it works fine.

The code which isn't working correctly is PS.println(passwordguess)

here is the client code:

class client extends JFrame implements ActionListener 
{
    JTextArea textarea;
    JTextField textfield;
    JPanel p1, p2;
    JButton button;

    String fieldtext;
    int buttonflag = 0;

    public static void main (String args[]) throws Exception
    {
        client CLIENT = new client(); //create object of class CLIENT
        CLIENT.run();   
    }

    public client() //constructor for frame 
    {

        setTitle("client");
        setLocation(100,100);
        setSize(500,500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        p1 = new JPanel(); //panel for text area
        p2 = new JPanel(); //panel for textfield
        textarea = new JTextArea();
        textarea.setText("Starting...\n");
        textfield = new JTextField("", 15);
        button = new JButton("Enter");

        p1.add(textarea);
        p2.add(textfield);
        button.addActionListener(this);
        p2.add(button);


        add(p1,BorderLayout.NORTH);
        add(p2,BorderLayout.SOUTH);


        setVisible(true);
    }


    public void run() throws Exception
    {

        Socket SOCK = new Socket("localhost", 8888);
        textarea.append("Connected to server\n");

        InputStreamReader ISR = new InputStreamReader(SOCK.getInputStream());
        BufferedReader BR = new BufferedReader(ISR); //buffered reader receives text                     using readLine method

        String MESSAGE = BR.readLine();  //expects String message from server
        textarea.append(MESSAGE);

        while (buttonflag == 0)   //empty loop pauses program until JButton is pressed
        {
        }

        String passwordguess = fieldtext; 

        buttonflag = 0;
        PrintStream PS = new PrintStream(SOCK.getOutputStream()); 
        PS.println(passwordguess);  //sends passwordguess to server to authenticate
        MESSAGE = BR.readLine();
        textarea.append("\n" + MESSAGE);

    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == button)
        {
            fieldtext = textfield.getText();
            textfield.setText("");
            buttonflag = 1;
        }
    }

}

and here is the server code:

public class server
{
    static String password;

    public static void main(String[] args) throws Exception
    {

        server SERVER = new server(); //create object of class
        password = SERVER.password();
        SERVER.run();

    }

    public void run() throws Exception
    {


        ServerSocket SERSOCK = new ServerSocket(8888);    //creates server socket object on port 8888
        Socket SOCK = SERSOCK.accept();   //creates socket and calls accept method on serversocket, return value is assigned to SOCK
        System.out.println("Client connected");
        InputStreamReader ISR = new InputStreamReader(SOCK.getInputStream()); 
        BufferedReader BR = new BufferedReader(ISR); //buffered reader reads data from socket


        PrintStream PS = new PrintStream(SOCK.getOutputStream()); //printstream prints data to socket
        PS.println("SERVER: requesting password");

        String passwordguess = BR.readLine();   //receives passwordguess from client 
        if (passwordguess.equals(password))   //compares passwordguess to password
        {
            System.out.println("password accepted");
            PS.println("SERVER: password accepted");
            //call next function
        }
        else 
        {
            System.out.println("password denied"); 
            PS.println("SERVER: password denied");
            //close streams and socket
        }
    }

    public String password()
    {
        String password;
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter a server password");
        password = scan.nextLine();
        return password;
    }

}
2
  • 1
    Have you called flush() on the output stream? Commented Mar 1, 2014 at 15:27
  • Furthermore, if I add a line saying PS.println("password123"); and set the server password to password123 the password will be accepted, so maybe it's to do with the gui button or the empty loop which pauses until button is clicked Commented Mar 2, 2014 at 12:20

2 Answers 2

1

use PS.flush() after each print statement.

Sign up to request clarification or add additional context in comments.

2 Comments

I did that and the same thing is happening.
hmm to be sure, use `PrintStream(stream,true) for auto flush
0

Ok I've sorted the issue - it was to do with the empty loop. For some reason the loop would run indefinitely, even after I press the button. I fixed it by including a command - any command. I used PS.flush();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.