socket客户端/服务器应用程序没有返回值

时间:2014-03-01 15:23:37

标签: java sockets bufferedreader

我正在创建一个简单的客户端服务器应用程序,您在服务器上输入密码,客户端输入密码然后发送到服务器并进行比较。密码不是使用我制作的GUI发送的,但是如果我使用控制台扫描字符串,它可以正常工作。

无效的代码是PS.println(passwordguess)

这是客户端代码:

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;
        }
    }

}

这是服务器代码:

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 个答案:

答案 0 :(得分:1)

在每个打印语句后使用PS.flush()

答案 1 :(得分:0)

好的我已经对问题进行了排序 - 它与空循环有关。由于某种原因,即使我按下按钮,循环也会无限期地运行。我通过包含命令 - 任何命令来修复它。我用了PS.flush();