-1

I've tried to send a message to my server twice, but It keeps having an error. I wonder what I should do to make this thing work. I keep having an error on this line "msgToServer.witeBytes("GETACCESS"+jTextFieldUsername.getText()+" "+jTextFieldPassword.getText()+"\n")".

Client:

try {
        s = new Socket("localhost", 6000);

        Socket incoming;
        incoming = s;
     
        msgFromServer = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
        msgToServer = new DataOutputStream(incoming.getOutputStream());

        msgToServer.writeBytes("LOGIN "+jTextFieldUsername.getText()+" "+jTextFieldPassword.getText()+"\n");
        
        String result;
        result = msgFromServer.readLine();

        if(result.equals("TRUE")){
            JOptionPane.showMessageDialog(null,"Client Access is granted");
        }
        else if(result.equals("False")){
            JOptionPane.showConfirmDialog(this, "Client Access is not granted");
        }
        msgToServer.writeBytes("GETACCESS"+ jTextFieldUsername.getText() + " "+ jTextFieldPassword.getText() + "\n");
        String h1;
        h1 = msgFromServer.readLine();
        JOptionPane.showConfirmDialog(this, "hl: "+h1);
        if(h1.equals("Teacher")){
            
            refreshTable(2);
            JOptionPane.showConfirmDialog(this, "You were granted as a lecturer");
        }
        else if(h1.equals("Student")){
            refreshTable(1);
            JOptionPane.showConfirmDialog(this, "You were granted as a student");
        }

    } catch (IOException ex) {
        Logger.getLogger(presensiClientFrame.class.getName()).log(Level.SEVERE, null, ex);
    }

My Error:

java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)

//....

Odain
  • 1
  • 2

1 Answers1

0

server blocks until a client connects to it and then blocks again to listen to a message from the client, after the single message, it closes the connection because code does not dealt with continuity. to keep continuous communication active, you need to perform read, write like

            String inputLine;
            while ((inputLine = msgFromServer.readLine()) != null) {
                if (".".equals(inputLine)) {
                    out.println("bye");
                    break;
                }
                msgToServer.writeBytes("GETACCESS"+ jTextFieldUsername.getText() + " "+ jTextFieldPassword.getText() + "\n");
            }
sanjeevRm
  • 1,541
  • 2
  • 13
  • 24