views:

111

answers:

2

Hi, I have two applications,one is for server and the other is for client.at first I run my server application .then I will run the client application.When you run the client application,one frame will be shown which get user name and password and if they were correct,the other frame will be shown.but when I click on the Sign In button ,nothing will happen.would you please help me? thanks.

Main class in Server application:

 public static void main(String[] args) {



    System.out.println("Server is starting...");

    ServerSocket server = null;
    try {
        server = new ServerSocket(5000);
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }

    System.out.println("Server is listening...");
    while (true) {
        try {
            Socket socket = server.accept();
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("Client Connected...");

    }
}

client class which has socket in it:

   private static InformationClass info = new InformationClass();
private static Socket c;
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

/**
 * @param args the command line arguments
 */
public static void runAClient() {
    try {
        c = new Socket("localhost", 5000);
    } catch (UnknownHostException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public static void clienT(){ try {

        BufferedReader read = new BufferedReader(new InputStreamReader(c.getInputStream()));
        BufferedWriter write = new BufferedWriter(new OutputStreamWriter(c.getOutputStream()));
        while (true) {
            String string = reader.readLine();
            write.write(string, 0, string.length());
            write.newLine();

            write.flush();
            System.out.println(read.readLine());
        }


    } catch (Exception e) {
        System.err.println(e);
    }

}

public static boolean connected() {
    boolean bool = false;
    if (c.isConnected()) {
        info.setSituation("Connected");
        bool = true;
    } else {
        info.setSituation("disconnected");
        bool = false;
    }
    return bool;
}

the main frame in the client application that I should run it ,after when i run the server application:(a part of that which is for Sign In button)

    private void submit() {
    String id = idField.getText();
    char[] pass1 = passField.getPassword();
    String pass = new String(pass1);
    if (id.equals("") || pass.equals("")) {
        ErrorFrame frame = new ErrorFrame();
        frame.setVisible(true);
    } else {
        boolean b = Manager.Test(id, pass);
        if (b == true) {
            Main.runAClient();
            boolean boOl = Main.connected();
            if(boOl==true){
                this.setVisible(false);
                ListFrame fRAme = new ListFrame(client);
                fRAme.setVisible(true);
            }
            else{
                JOptionPane.showConfirmDialog(this, "You couldn't connect successfully,please try again!","sign_In Problem",JOptionPane.ERROR_MESSAGE);
                return;

            }



        } else {

            JOptionPane.showConfirmDialog(this, "You have entered wrong datas,try it again");
            return;
        }
    }
}

EDIT: I have edited my client class.

+4  A: 

Have you tried stepping through your code using a debugger to work out precisely which line of code is not working correctly? Simply stating that "nothing happens" when you click on the sign-in button doesn't give us much to go on.

Adamski
I debugged ,it goes well.when I run the server and then the client application:the sign in button will not work.BUT when i don't run the server(I just run the client application):the sign in button will work but the connectException will be thrown.
Johanna
Try adding a breakpoint on the ActionListener's actionPerformed(ActionEvent) method and stepping through from there. Which line of code in the client do you get too? Which path are you following through the submit() method?
Adamski
I have cleaned these lines: if(boOl==true){this.setVisible(false)ListFrame fRAme = new ListFrame(client);fRAme.setVisible(true);} else{ JOptionPane.showConfirmDialog(this, "You couldn't connect successfully,please try again!","sign_In Problem",JOptionPane.ERROR_MESSAGE); return; }and I wrote these lines instead:ListFrame fRAme = new ListFrame(client); fRAme.setVisible(true); and the sign in button works well.BUT ,I have searched a lot that how can I handle the connectException but I couldn't find anything useful.
Johanna
A: 

Are you sure your submit() method is called when you click 'Sign In' button? Another thing, Main.RunAClient() never returns constantly asking user to enter a line in system console.

Archie