Home > Software engineering >  How to create a socket connection between this client and server
How to create a socket connection between this client and server

Time:04-19

I have to connect two java application using a socket. The client is a java form where i insert the username and password, while the server is the "authenticator".

Client App GUI

My question is: I should create a client connection each time or i have to continue to use the same connection until the application is closed? Because in this case, how can i send the data only when i click the button?

ClientSocket class

public class ClientSocket {

public void start(String txtUser, String txtPsw) {

    String hostname = "127.0.0.1";
    int port = 42768;

    try (Socket socket = new Socket(hostname, port)) {

        System.out.println("Socket started");

        OutputStream output = socket.getOutputStream();
        PrintWriter writer = new PrintWriter(output, true);

        writer.println(txtUser);
        writer.println(txtPsw);

    } catch (UnknownHostException ex) {

        System.out.println("Server not found: "   ex.getMessage());

    } catch (IOException ex) {

        System.out.println("I/O error: "   ex.getMessage());
    }
}
}

FormGUI class

public class FormGUI {
private JPanel panel1;
private JTextField txtUser;
private JPasswordField txtPsw;
private JButton button1;


public FormGUI() {

    button1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println(txtUser.getText());
            System.out.println(txtPsw.getPassword());
            new ClientSocket().start(txtUser.getText(), String.valueOf(txtPsw.getPassword()));
        }
    });
}

public static void main(String[] args) {
    JFrame frame = new JFrame("FormGUI");
    frame.setContentPane(new FormGUI().panel1);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);

}

}

CodePudding user response:

First I hope this is just a learning example. Otherwise please avoid to send passwords over plain connections as this is really bad practice!

As for your real question: As user253751 already mentioned, it is up to you.

But you should think about the protocol that is used between the client and the server. According to your code you send the username on the first line and the password on the second line. But I think you will not just log in to the server but also do some actions/process some data. Think about how you will recognize if the message is a login, a data request or a processing command. I suggest you read about some often used protocols like http, smtp etc. to learn and get inspiration from them.

Also be aware that when you receive data, it will not always be the whole message. It can be the first part of a message, then in the same buffer the second (and last) part of the message and the beginning of the next message.

Another thing you should keep in mind when you try to decide if you keep the connection open or not is that you have to keep the state of the client in the server and make sure that only logged in users can start the actions. The open connection can be an easy way, but it can also be solved by a session identifier that has to be provided with each action (and also should not be readable plain on the network).

Hope this helps for taking the next steps.

  • Related