客户端 - 服务器应用程序不等待输入

时间:2018-03-07 00:09:52

标签: java server io

我正在编写一个psimple客户端 - 服务器应用程序。当字符串" list"在客户端应用程序中输入,服务器应该在" serverFiles"中发送文件的名称。文件夹到客户端,然后应该打印名称。然后正确读取客户端输入,但没有任何反应。经过一些调试后,似乎服务器没有等待来自客户端的输入

服务器代码

import java.io.*;
import java.net.ServerSocket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Server
{
    public static void main( String[] args ) throws IOException
    {
        ServerSocket serverSocket = new ServerSocket(8888);
        ExecutorService service = Executors.newFixedThreadPool(10);

        while(true){
            SocketHandler socket = new SocketHandler(serverSocket.accept());
            service.submit(socket);

            socket.getWriter().close();
            socket.close();
        }

    }
}

import java.io.*;
import java.net.Socket;

public class SocketHandler extends Thread {
    private Socket socket = null;
    private BufferedReader reader;
    private PrintWriter writer;

    public SocketHandler(Socket socket) {
        try {
            this.socket = socket;
            this.reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
            this.writer = new PrintWriter(this.socket.getOutputStream());
        } catch (IOException e) {
            System.err.println("Could not create buffered reader");
        }
    }

    public void run() {
        String string = null;
        try {
            while ((string = reader.readLine()) != null) {
                System.out.println(string);
                switch (string) {
                    case "list": {
                        listFiles();
                        break;
                    }
                }
            }
        } catch (IOException e) {
        }
        System.out.println();
    }

    public void close() {
        try {
            socket.close();
        } catch (IOException e) {
            System.err.println("Could not close socket");
        }
    }

    public void listFiles() {
        System.out.println("LISTING");
        File[] files = (new File("serverFiles")).listFiles();
        for (File file : files) {
            writer.println(file.getName());
        }
        writer.flush();
    }


    public Socket getSocket() {
        return socket;
    }

    public BufferedReader getReader() {
        return reader;
    }

    public PrintWriter getWriter() {
        return writer;
    }
}

客户端代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.Scanner;

public class Client{
    public void listFiles(){

    }

    public static void main(String[] args) throws IOException
    {
        ClientHandler client = new ClientHandler();
        KeyboardScanner scanner = new KeyboardScanner();
        String string = null;
        while((string = scanner.getInput()) != null){
            switch(string){
                case "bye":{
                    client.close();
                    return;
                }case "list":{
                    client.listFiles();
                }
            }
        }
    }
}

class KeyboardScanner{
    Scanner scanner = new Scanner(System.in);

    public String getInput(){
        return scanner.nextLine();
    }
}

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ClientHandler {
    private Socket socket = null;
    private BufferedReader reader = null;
    public PrintWriter writer = null;

    public ClientHandler(){
        try {
            socket = new Socket("localhost", 8888);
            reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            writer = new PrintWriter(socket.getOutputStream());
        } catch(IOException e){
            System.err.println("Could not make connection");
        }
    }

    public void close(){
        try{
        socket.close();
    }catch(IOException e){
            System.err.println("Could not close connection");
        }
    }

    public void listFiles(){
        writer.println("list");
        writer.flush();
        String string = null;
        try {
            while ((string = reader.readLine()) != null) {
                System.out.println(string);
            }
        }catch(IOException e){}
    }
}

0 个答案:

没有答案