我正在做一個簡單的客戶端服務器應用程序,您在服務器上輸入密碼,客戶端輸入密碼,然後發送到服務器並進行比較。密碼不是使用我所做的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;
}
}
你在輸出流上調用了flush()嗎? – kshikama
此外,如果我添加一行說PS.println(「password123」);並設置服務器密碼爲password123密碼將被接受,所以也許這是與gui按鈕或空循環暫停,直到按鈕被點擊 – user3343264