Java客户端 - 服务器程序:服务器无法从服务器读取

时间:2016-04-26 17:33:09

标签: java tcp-ip

我的Java程序存在问题。它由客户端和服务器类组成。服务器只从客户端读取字符而不是字符串。哪里出错了? 这是我的计划。

Client.java

public class Client_1324600 {

private static String text;
private static char character;
private static int numOfOccurrences;

public static void main(String args[]) throws IOException, Exception { // Main Method

    Socket clientSocket = new Socket("localhost", 6787);  // socket to establish a connection with the server

    Scanner sc = new Scanner(System.in);

    BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); // read from user

    DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); // send to server

    BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); // read from server

    do {
        System.out.print("Enter a Character to be searched: ");
        character = inFromUser.readLine().charAt(0); // read a char
        outToServer.writeByte(character); // send character to server

        System.out.print("Enter a String: ");
        text = inFromUser.readLine(); // read string
        outToServer.writeBytes(text+'\n'); // sent the text to server

        numOfOccurrences = inFromServer.read(); // read number of Occurrences

        System.out.print("The number of Occurrences are: " + numOfOccurrences);

        System.out.println("Want to repeat (Y/N): "); // ask user to repeat
        char option = sc.next().charAt(0);

        switch (option) {
            case 'y':  // option = Y or y if he wants to repeat 
            case 'Y':
                break;
            case 'n':  // option = N or n if he does not want to repeat 
            case 'N':
                System.out.println("Thank You!");
                clientSocket.close(); // close the connection
                System.exit(0);
            default:  // he enters a wrong option
                System.out.print("Error, Please enter either Y or N\n");
                System.out.println("Want to repeat (Y/N): ");
                option = sc.next().charAt(0);
                break;
        } // switch
    } while (true);

} // main

} // end class client

Server.java

public class Server_1324600 {

public static void main(String[] args) throws IOException {
    String clientSentence;
    char clientChar;
    int numOfOccurrences;
    ServerSocket welcomeSocket = new ServerSocket(6787); // socket to connect with client
    System.out.println("Waiting for a client's request for connection ...");
    //waiting for client's request to establish the connection
    Socket connectionSocket = welcomeSocket.accept();
    System.out.println("connection established!");
    while (true) {

        BufferedReader inFromClient
                = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
        DataOutputStream outToClient
                = new DataOutputStream(connectionSocket.getOutputStream());

        clientChar = inFromClient.readLine().charAt(0); // read char from client
        System.out.println("clientChar " + clientChar);

        clientSentence = inFromClient.readLine(); // read text from client
        System.out.println("clientSentence " + clientSentence+'\n');

        numOfOccurrences = CountChars(clientChar, clientSentence); // call method CountChars and store return value in numOfOccurrences

        outToClient.writeInt(numOfOccurrences + '\n'); // send numOfOccurrences to client

    }
}

static int CountChars(char character, String text) { // method CountChars to count the number of characters in a given text
    int count = 0;

    /*Since the text may contain lower-case and upper-case characters,
    my idea is to change the text as well as the character to lower-case, the search for matching characters  */
    text = text.toLowerCase(); // convert the string to lower-case
    character = Character.toLowerCase(character); // convert the character to lower-case

    for (int i = 0; i < text.length(); i++) { // traverse the String character by character

        if (text.charAt(i) == character) {  // if the message character matches with the user character
            count++; // increment counter by one
        } // if
    } // for
    return count; // return The number of Occurrences 
} //CountChars

} // end class server

输出:

Enter a Character to be searched: a
Enter a String: thank you

0 个答案:

没有答案