在未在客户端程序中执行时的语句

时间:2013-07-15 07:34:02

标签: java sockets client-server

我正在尝试从服务器读取文件并能够成功完成。但是,在客户端,while循环之后的语句根本不执行。请帮我解决这个问题。要求客户输入的行根本不显示。请帮忙

//Client Side Code
import java.io.*;
import java.net.*;
import java.util.StringTokenizer;
import java.lang.*;

public class oss_client1 {
    public static void main(String args[]) throws Exception {

        Socket sock = new Socket("127.0.0.1", 2222);
        char n1;
        int choice;

        // reading the file name from keyboard. Uses input stream
        BufferedReader keyRead = new BufferedReader(new InputStreamReader(
                System.in));
        BufferedReader dis = new BufferedReader(
                new InputStreamReader(System.in));

        // receiving the contents from server. Uses input stream
        InputStream istream = sock.getInputStream();
        InputStream istream1 = sock.getInputStream();


        // sending the file name to server. Uses PrintWriter
        OutputStream ostream = sock.getOutputStream();
        BufferedReader dRead = new BufferedReader(
                new InputStreamReader(istream));
        BufferedReader socketRead = new BufferedReader(new InputStreamReader(
                istream1));


        PrintWriter pwrite = new PrintWriter(ostream, true);
        PrintWriter pwrite1 = new PrintWriter(ostream, true);

        do {
            System.out.println("Enter the website name: ");
            String u_input = keyRead.readLine();
            pwrite.println(u_input);
            String str;

            while ((str = dRead.readLine()) != null) {
                // reading line-by-line
                System.out.println(str);
            }


            str = dRead.readLine();
            System.out.println(str);
            dRead.close();

            System.out
                    .print("\nPlease enter the product code which you want to buy: ");

            String pcode = dis.readLine();
            pwrite1.println(pcode);
            String pcode_res = socketRead.readLine();

            System.out.print("\nBack from server " + pcode_res + "\n");
            System.out.println("\nDo you want to continue(Y/N)");
            String n = dis.readLine();
            n1 = n.charAt(0);

        } while (n1 == 'Y' || n1 == 'y');

        pwrite.close();
        socketRead.close();
        keyRead.close();
    }

}

//服务器端代码

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

public class oss_server1 {
    static Socket clientSocket = null;
    static ServerSocket serverSocket = null;
    static clientThread t[] = new clientThread[10];

    public static void main(String args[]) throws Exception {

        int port_number = 2222; // The default port

        if (args.length < 1) {
            System.out.println("Now using port number=" + port_number);
        } else {
            port_number = Integer.valueOf(args[0]).intValue();
        }

        /* Try to open a server socket on port port_number (default 2222) */
        try {
            serverSocket = new ServerSocket(port_number);
        } catch (IOException e) {
            System.out.println(e);
        }

        while (true) {
            try {
                clientSocket = serverSocket.accept();
                for (int i = 0; i <= 9; i++) {
                    if (t[i] == null) {
                        (t[i] = new clientThread(clientSocket, t)).start();
                        port_number++;
                        break;
                    } // end of if loop
                } // end for looop
            }// end of try loop
            catch (IOException e) {
                System.out.println(e);
            }
        } // end of while loop

    }
}

class clientThread extends Thread {


    DataInputStream is = null;
    DataOutputStream dout = null;
    Socket clientSocket = null;
    clientThread t[];

    public clientThread(Socket clientSocket, clientThread[] t) {
        this.clientSocket = clientSocket;
        this.t = t;
    }

    public void run() {
        try {
            System.out.println("Welcome to US ONLINE SHOPPING SYSTEM");
            String website = "www.US_OSS.com";

            // buffer stream for reading the choice from client
            InputStream istream = clientSocket.getInputStream();
            InputStream istream1 = clientSocket.getInputStream();
            BufferedReader webRead = new BufferedReader(new InputStreamReader(istream));
            BufferedReader pcodeRead = new BufferedReader(new InputStreamReader(istream));


            // buffer stream reading the file contents
            BufferedReader displayRead = new BufferedReader(new FileReader(
                    "display_client.txt"));
            BufferedReader prodRead = new BufferedReader(new FileReader(
                    "product_list.txt"));

            // keeping output stream ready to send the contents
            OutputStream ostream = clientSocket.getOutputStream();
            PrintWriter pwrite = new PrintWriter(ostream, true);
            String str, str1, pcode;
            String fname = webRead.readLine();
            if (fname.compareTo(website) != 0) {
                pwrite.println("Error 404: NOT FOUND");
            } else {
                while ((str = displayRead.readLine()) != null) // reading line-by-line from file 
                {
                    pwrite.println(str);
                }
                displayRead.close();
                str = "END OF PRODUCT LIST";

                pwrite.println(str);
                pcode = pcodeRead.readLine();
                System.out.print("\nReceived PCODE is: " + pcode);
                pwrite.println(pcode);
            }
            // pwrite.close();
        } // end of try block
        catch (IOException e) {
            System.out.println(e);
        }

    } // end of run class
}// end of Client thread class 

1 个答案:

答案 0 :(得分:1)

BufferedReaderEOF之后阅读时会发出IOException

相反,我建议您使用Scanner来读取文件 您的代码看起来像......(在ServerSide中)

Scanner scnFile=new Scanner(new File("display_client.txt")); //give absolute path if necessary

while(scnFile.hasNext()){

System.out.println(scnFile.nextLine());

}

scnFile.close();