0

我正在尝试使用客户端/服务器搜索命令行中指定的单词(从文件中)。这是我的代码,但是当客户端部分运行时它什么也不显示。要运行服务器,请在命令行中 -s <port number> <file.txt>为客户端键入和。-c localhost <port number> <word to be searched>

import java.net.*;
import java.util.*;
import java.io.*;

public class quotes {
    public static InetAddress host;
    public static ServerSocket serverSocket;
    public static String target;
    public static void main(String[] args) throws IOException {

        if(args[0].equals("-c")){
            Client(args);
            target = args[3];
        }
        else if(args[0].equals("-s")){
            System.out.println("Server");
            Server(args);
        }
    }

    @SuppressWarnings("resource")
    public static void Client(String[] args) throws IOException{
            String hostname = args[1];

            if(hostname.equals("localhost")) host = InetAddress.getLocalHost();
            else host = InetAddress.getByName(hostname);

            int port = Integer.parseInt(args[2]);

            Socket socket = new Socket(host, port);

            Scanner input = new Scanner(System.in);

            Scanner networkInput = new Scanner(socket.getInputStream());
            PrintWriter networkOutput = new PrintWriter(socket.getOutputStream(), true);

            Scanner userEntry = new Scanner(System.in);

            String response;



                networkOutput.println(target);
                response = networkInput.nextLine();
                while(!response.equals("|")){

                    System.out.println("\n " + response);
                    response = networkInput.nextLine();
                }



    }

    public static void Server(String[] args) throws IOException {
        int port = Integer.parseInt(args[1]);
        String file = args[2];
        serverSocket = new ServerSocket(port);
        do {

            Socket client = serverSocket.accept();

            System.out.println("\nNew client accepted.\n");


            ClientHandler3 handler = new ClientHandler3(client, file);

            handler.start();    
        }while(true);   


    }
}

    class ClientHandler3 extends Thread {
        private Socket client;
        private Scanner input;
        private PrintWriter output;
        private ArrayList<String> quotes;
        public ClientHandler3(Socket socket, String file) {
            client = socket;
            try {
                BufferedReader buffer = new BufferedReader(new FileReader(file));
                BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream())); 
                String line = reader.readLine();
                try {
                    int ctr = 0;
                    quotes = new ArrayList<String>();
                    while(line != null){
                        quotes.add(ctr, line);

                        ctr++;
                        line = buffer.readLine();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }


            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


            try {
                input = new Scanner(client.getInputStream());
                output = new PrintWriter(client.getOutputStream(), true);
            }
            catch(IOException e) {
                e.printStackTrace();
            }
        }


        public void run() {
            String target;
            String message = "";
                target= args[3];
                for(int i = 0; i<quotes.size(); i++){
                    if(quotes.get(i).toUpperCase().contains(target.toUpperCase())){
                        output.println(quotes.get(i));
                    }
                }
                output.println("|");
            try {
                if (client != null) {
                    System.out.println("Closing down connection...");
                    client.close();
                }
            }
            catch(IOException e) {
                System.out.println("Unable to disconnect!");
            }
        }
    }


(感谢 JB Nizet 爵士的一些修改和建议)我遇到了问题,target= args[3];因为class ClientHandler3我知道覆盖没有意义。我是这个编程领域的新手,我需要你的帮助。请帮我弄清楚事情。谢谢!

编辑

import java.net.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.*;

public class quotes {
    public static InetAddress host;
    public static ServerSocket serverSocket;
    public static void main(String[] args) throws IOException {

        if(args[0].equals("-c")){
            Client(args);
        }
        else if(args[0].equals("-s")){
            System.out.println("SERVER KA!!!");
            Server(args);
        }
    }

    @SuppressWarnings("resource")
    public static void Client(String[] args) throws IOException
            String hostname = args[1];
            String target, response;
            if(hostname.equals("localhost")) host = InetAddress.getLocalHost();
            else host = InetAddress.getByName(hostname);

            int port = Integer.parseInt(args[2]);
            target = args[3];
            Socket socket = new Socket(host, port);

            Scanner input = new Scanner(System.in);

            Scanner networkInput = new Scanner(socket.getInputStream());
            PrintWriter networkOutput = new PrintWriter(socket.getOutputStream(), true);

            // Set up stream from keyboard entry...
            Scanner userEntry = new Scanner(System.in);

                networkOutput.println(target);
                response = networkInput.nextLine();
                while(!response.equals("|")){
                // Display server's response to user ...
                    System.out.println("\n " + response);
                    response = networkInput.nextLine();
                }   

    }

    public static void Server(String[] args) throws IOException {
        int port = Integer.parseInt(args[1]);
        String file = args[2];
        String target = "";
        serverSocket = new ServerSocket(port);
        do {
            // Wait for client...
            Socket client = serverSocket.accept();

            System.out.println("\nNew client accepted.\n");

            ClientHandler3 handler = new ClientHandler3(client, file, target);

            handler.start();    
        }while(true);   


    }

}

    class ClientHandler3 extends Thread {
        private Socket client;
        private Scanner input;
        private PrintWriter output;
        private ArrayList<String> quotes;
        private String target;
        public ClientHandler3(Socket socket, String file, String target) {

            // Set up reference to associated socket...
            client = socket;
            try {
                BufferedReader buffer = new BufferedReader(new FileReader(file));
                BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
                this.target = reader.readLine(); 
                String line = reader.readLine();
                try {
                    int ctr = 0;
                    quotes = new ArrayList<String>();
                    while(line != null){
                        quotes.add(ctr, line);

                        ctr++;
                        line = buffer.readLine();
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            try {
                input = new Scanner(client.getInputStream());
                output = new PrintWriter(client.getOutputStream(), true);
            }
            catch(IOException e) {
                e.printStackTrace();
            }
        }


        public void run() {
            String message = "";

                target= input.nextLine();

                for(int i = 0; i<quotes.size(); i++){
                    if(quotes.get(i).toUpperCase().contains(target.toUpperCase())){
                        output.println(quotes.get(i));
                    }
                }
                output.println("|");


            try {
                if (client != null) {
                    System.out.println("Closing down connection...");
                    client.close();
                }
            }
            catch(IOException e) {
                System.out.println("Unable to disconnect!");
            }
        }
    }
4

1 回答 1

1

将 target 设置为 ClientHandler3 的一个字段,以便您可以在run()方法中使用它。

class ClientHandler3 extends Thread {
...
private String target;

...

并使用:

this.target = reader.readLine();

就在之前

String line = reader.readLine();

线。

于 2013-02-26T12:02:51.437 回答