2014-03-01 41 views
0

我正在做一个简单的客户端服务器应用程序,您在服务器上输入密码,客户端输入密码,然后发送到服务器并进行比较。密码不是使用我所做的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; 
    } 

} 
+1

你在输出流上调用了flush()吗? – kshikama

+0

此外,如果我添加一行说PS.println(“password123”);并设置服务器密码为password123密码将被接受,所以也许这是与gui按钮或空循环暂停,直到按钮被点击 – user3343264

回答

1

使用PS.flush()每次打印后,声明。

+0

我做到了这一点,同样的事情正在发生。 – user3343264

+0

嗯,可以肯定的是,使用'PrintStream(stream,true)进行自动刷新 – user3297129

0

好吧我已经排序的问题 - 这是与空循环。出于某种原因,循环将无限期运行,即使按下按钮后也是如此。我通过包含一个命令来修复它 - 任何命令。我用PS.flush();

相关问题