无法从客户端回传到服务器

时间:2013-12-10 04:36:05

标签: java multithreading sockets

这是我的Server类,以及Admin / Client ...管理员可以从服务器接收初始消息,但无法响应服务器。创建服务器套接字,当管理员连接到服务器时,将收到并打印服务器的消息。虽然一旦服务器的菜单打印输出(“1.登录等...”)。我无法通过控制台输入选项。一旦管理员打印出从服务器发送的菜单,它似乎陷入僵局。

/**
   A server that executes the Simple Bank Access Protocol.
*/
public class BankServer extends Thread {  

private Socket sAdmin;
private Scanner inServer;
private PrintWriter outServer;

public static void main(String[] args) throws IOException {
    Bank bank = new Bank(10);
    ServerSocket server = new ServerSocket(8888);
    System.out.println("Waiting for clients to connect...");
    Thread admin = new BankServer();
    admin.start();
    while (true) {
        Socket s = server.accept();
        System.out.println("Client connected.");
        BankService service = new BankService(s, bank);
        Thread t = new Thread(service);
        t.start();
    }
}

@SuppressWarnings("resource")
public void run() {
    try {
        ServerSocket server = new ServerSocket(8889);
        try {
            System.out.println("Waiting for admin to connect...");
            while (true) {
                sAdmin = server.accept();
                System.out.println("Admin connected.");
                InputStream instream = sAdmin.getInputStream();
                OutputStream outstream = sAdmin.getOutputStream();
                inServer = new Scanner(instream);
                outServer = new PrintWriter(outstream);
                outServer.print(...+"1. Login\n"+etc...);
                outServer.flush();
                findCommand();
            }
        } finally {
            sAdmin.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void findCommand() throws IOException {
    while (true) {
        if (!inServer.hasNext()) {
            return;
        }
        String command = inServer.next();
        String chosenCommand;
        switch (command) {
            case "1":
                chosenCommand = "LOGIN";
                runCommand(chosenCommand);
                break;
            case "2":
            etc...
        }
    }
}
...
}

我删除了runCommand方法和一些不会导致问题的字段,以便于阅读。

这是管理类。它将输出从服务器发送的选项,但不会退出输出初始消息的循环,并允许我输入选项。

public class BankAdmin {
public static void main(String[] args) throws IOException {
    Socket inAdmin = new Socket("localhost", 8889);
    InputStream instream = inAdmin.getInputStream();
    OutputStream outstream = inAdmin.getOutputStream();
    Scanner in = new Scanner(instream);
    PrintWriter adminOut = new PrintWriter(outstream);
    Scanner console = new Scanner(System.in);

    while (true) {
        if (!in.hasNextLine()) {
            String command = console.next();
            System.out.println("Sending: " + command);
            adminOut.print("command");
            adminOut.flush();
        } else {
            String response = in.nextLine();
            System.out.println(response);
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

这就是发生的事情:

服务器将菜单发送到客户端后,客户端卡在in.hasNextLine()中。这是因为in.hasNextLine()将阻止(暂停)输入,等待新行(否则,它无法确定是否会有下一行)。要解决此问题,必须使用线程,其中一个等待输入,而另一个从控制台读取。

有关详细信息,请参阅Javadoc on Scanner.hasNextLine()