-1

now i'm making chatting program.

but there are some problems.

first, in login process, when i send the id/pw to server, server sends that is right or wrong (protocol 3000 or 3001). then, client will get the protocol. and

islogin(boolean) will changed true or false. however, the boolean value is not changed. i don't know why it is not changed. there are some codes.

clientbackground.java

    package client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.StringTokenizer;

import crypto.des;



public class ClientBackground implements Runnable{

    Socket socket;
    DataInputStream in;
    private DataOutputStream out;
    private ClientGui gui;
    private String msg;
    String id;
    private String pass;
    private boolean islogin;
    private login_Frame lf;
    private regform rf;
    private String info;
    Thread clientThread;
    des crypto;
    String packet="";
    String tmp;
    boolean test = false;
    int protocol;


    public final void setGui(ClientGui gui) {
        this.gui = gui;
    }
    public void run() {
        try {
            socket = new Socket("192.168.0.11", 7770);
            System.out.println("connect!.");
            socket.setTcpNoDelay(true);
            crypto = new des();
            //crypto.SetD();
            out = new DataOutputStream(socket.getOutputStream());
            in = new DataInputStream(socket.getInputStream());  
            System.out.println(in.available()); //test
            System.out.println("okay!");
            while (in != null) {
                packet = in.readUTF();
                StringTokenizer st = new StringTokenizer(packet,"/");
                tmp = st.nextToken();
                msg = st.nextToken();
                this.protocol = Integer.parseInt(tmp);
                System.out.println(this.protocol+"&&&&"); //test
                switch(this.protocol){

                case 3000 :{
                System.out.println("success");
                test = true;
                this.lf.gettest(test);

                }
                break;
                case 3001 :{
                    System.out.println("wrong");
                    test = false;
                    this.lf.gettest(test);
                }
                break;
                default :{
                gui.jta.append(msg);
                System.out.println(msg);
                System.out.println(test+"%%");
                }
                break;
            }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } 
    }
    /*public void loginP() {
        System.out.println(this.protocol+"&&&&");
        switch(this.protocol){

        case 3000 :{
        System.out.println("success");
        test = true;
        this.lf.gettest(test);

        }
        break;
        case 3001 :{
            System.out.println("wrong!");
            test = false;
            this.lf.gettest(test);
        }
        break;
        }
    }*/

    public static void main(String[] args){
        ClientBackground clientBackground = new ClientBackground();
        Thread clientThread = new Thread(clientBackground);
        clientThread.setPriority(1);
        clientThread.start();
        clientBackground.lf = new login_Frame();
        clientBackground.lf.Clientback(clientBackground);

    }
    public void showFrameTest() {
        this.lf.setVisible(false); 
        this.gui = new ClientGui();
        this.gui.Clientback(this);  
    }
    public void showregfrom() {
        this.lf.setVisible(false); 
        this.rf = new regform();
        this.rf.Clientbackreg(this);
    }
    public void relogin_form() {
        this.rf.dispose(); 
        this.lf.setVisible(true);
    }

    public void sendMessage(String msg2) {
        try {
            out.writeUTF(msg2);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public boolean lcheck() {
        return islogin;
    }

    public void setid(String id, String pass) {
        this.id = id;
        this.pass = pass;
    }
    public String getid() {
        return id;
    }

}

login_Frame.java

package client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.NoSuchPaddingException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import crypto.des;



public class login_Frame extends JFrame{

    private static final long serialVersionUID = 1L;
    private String id;
    private String pass;
    JButton logb = new JButton("login");
    JButton exitb = new JButton("cancel");
    JButton regb = new JButton("reg");
    JLabel idlb = new JLabel("ID : ");
    JLabel pwlb = new JLabel("PW : ");
    JTextField idtb = new JTextField();
    JTextField pwtb = new JTextField();
    private static ClientBackground client;
    private DataInputStream in;
    byte[] CpStr = null;
    boolean test = false;
    String packet,tmp;

    public login_Frame(){
        new Thread(client).start();
        getContentPane().add(logb);
        getContentPane().add(exitb);
        getContentPane().add(regb);
        getContentPane().add(idlb);
        getContentPane().add(pwlb);
        getContentPane().add(idtb);
        getContentPane().add(pwtb);

        setLayout(null);
        setBounds(100, 100, 400, 200);
        setVisible(true);
        logb.setBounds(40, 110, 90, 40);
        exitb.setBounds(150, 110, 90, 40);
        regb.setBounds(260, 110, 90, 40);
        idlb.setBounds(20, 10, 50, 40);
        pwlb.setBounds(20, 60, 50, 40);
        idtb.setBounds(70, 10, 280, 40);
        pwtb.setBounds(70, 60, 280, 40);

        ActionListener confirmListener = new ConfirmListener();
        ActionListener exListener = new ExitListener();
        ActionListener regListener = new RegListener();
        logb.addActionListener(confirmListener);
        exitb.addActionListener(exListener);
        regb.addActionListener(regListener);
        idtb.addActionListener(confirmListener);
        pwtb.addActionListener(confirmListener);


    }
    public String getidtb(){
        return idtb.getText();
    }
    public String getpwtb(){
        return pwtb.getText();
    }
    private class ConfirmListener implements ActionListener {
        public void actionPerformed(ActionEvent e){

                try {
                    isLoginCheck();
                    System.out.println(test); //test
                    if(client.test){
                        System.out.println(client.test+"%%%%"); //test
                        client.showFrameTest();
                    }                   
                    else{
                        JOptionPane.showMessageDialog(null, "wrong!");
                    }


                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

        }
    }
    private class RegListener implements ActionListener {
        public void actionPerformed(ActionEvent e){
                regbtaction();
        }
    }
    private class ExitListener implements ActionListener {
        public void actionPerformed(ActionEvent e){
            System.exit(0);
        }
    }
    public void Clientback(ClientBackground client) {
        this.client = client;
    }
    public void isLoginCheck() throws Exception{
        id = getidtb();
        pass = getpwtb();
        new Thread(client).start();
        //CpStr = client.crypto.Encrypts(pass);
        idtb.setText("");
        pwtb.setText("");
        client.setid(id, pass);
        client.sendMessage("3004"+"/"+id+"/"+pass);

    }
    public void regbtaction() {
        client.showregfrom();
        this.setVisible(false);
    }
    public void gettest(boolean test) {
        this.test = test;
    }

}

I edit some codes and add some test codes to verify the login processing. Now, if I execute my program and click the login button, wrong message is appeared. But some test codes teach me something. connect!.

0
okay!
connect!.
0
okay!
false
3000&&&&
success

This is clientBackground's commend. Especially, 3000&&&& and success It means that clientBackground received correct protocol. Then, it edit its test boolean(true). But here are some problem. I want that when I click the login button, isLoginCheck will be executed, then test boolean have to changed. However, if(client.test){ execute ahead. I want change processing order. How to change the order..?

@Lothar plz help me.

hohodduck
  • 33
  • 10
  • in first question, there are any priority?? i think that in `login_Frame` i will click the login button. then id / pw will send server. and server send protocol. then boolean will changed. and `login_Frame' login button action's `if(test){` will be excuted, isn't it?? – hohodduck Sep 24 '17 at 13:59

1 Answers1

0

Do you see your debug messages like ok, etc.?

I can see some things here:

public void sendMessage(String msg2) {
    try {
        out.writeUTF(msg2);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

You never flush the stream, so it might be that your data never got sent out. With that you don't receive the result of the login-check.

while(in.available()>0)
{
    packet = in.readUTF();
    System.out.println(packet+"%%%");
}

This is a while-loop before the actual loop where you are parsing the data. If your response is sent during this loop, it will be lost for the subsequent read. Also, using in.available is not a good way of reading in data. available only returns the number of bytes that can be read from the stream without blocking. If the network connection is slow this would return 0 even if there are still bytes to be read.

while (in != null) {
    packet = in.readUTF();
    StringTokenizer st = new StringTokenizer(packet,"/");
    [...]

If the data to be parsed actually is the result of the previous while-loop, your replacing it by something else with the repeated call of in.readUTF

while (true) {
    Receiver receiver = new Receiver(mid_server);
    receiver.start();
}

This creates thousands and thousands of threads within seconds and will most likely kill this application with an OutOfMemoryError when the upper limit of allowed threads is reached.

Lothar
  • 5,323
  • 1
  • 11
  • 27
  • hi! do you remember my question?? maybe... it is not solved... i mean, i flushed my outputstream, but login process still don't work... – hohodduck Oct 14 '17 at 09:39
  • @hohodduck My answer contained more than just the suggestion to flush the outputstream. Have you fixed the other issues as well? If you still need help on this, edit your question and explain the current situation, i.e. what's your current code look like and what are the new sympthoms after doing the changes being suggested. – Lothar Oct 14 '17 at 20:24
  • Also, I use Robot class (delay time). But it is just a makeshift, isn't it?? I want correct more fundamentally.. – hohodduck Oct 15 '17 at 03:38