0

クライアント/サーバーを使用して、コマンド ラインで指定された (ファイルから) 単語を検索しようとしています。これが私のコードですが、クライアント部分の実行時には何も表示されません。サーバーを実行するには、コマンド ラインで -s <port number> <file.txt>クライアントの場合は and と入力します。-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 に答える