java中的客户端/服务器:程序在输入第二个文件名时卡住

时间:2014-11-05 06:52:11

标签: java file client-server

我正在开发代码以向服务器端发送多个文件名,然后确保服务器收到这些内容并将其写入自己文件夹中的文件。当我键入第一个文件名时它工作得很好,但是当我键入第二个文件名时,代码类型被卡住了。

这是我的客户代码: -

package fileTransfer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class EchoClient {

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

    try {
        Socket clientSock=new Socket("localhost",8888);

        //to read from server
        BufferedReader br=new BufferedReader(new  InputStreamReader(clientSock.getInputStream())); //to read

        //to write to server
        PrintWriter pw=new PrintWriter(clientSock.getOutputStream(), true); 

        //for user input
        BufferedReader userIn=new BufferedReader(new InputStreamReader(System.in));
        BufferedReader fileContent=null;
        String str=null;
        String fileContentLine=null;
        while(true){
            if((str=br.readLine()).contains("file name")) //recieve echo from server
                System.out.println(str);


            str=userIn.readLine(); //read user input
            fileContent=new BufferedReader(new FileReader(str));
            pw.println(str);
            while((fileContentLine=fileContent.readLine()) != null){
                pw.println(fileContentLine);
            }

            while((str=br.readLine())!=null)
                System.out.println(str);

            pw.flush();

        }

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


}

}

这是我的服务器代码

package fileTransfer;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class EchoServer {

 public static void main(String[] args){
    try {
        ServerSocket serverSock=new ServerSocket(8888);
        System.out.println("Waiting for client");
        Socket connectFromClient=serverSock.accept();

        File file=null;
        //reading data from client
        BufferedReader input=new BufferedReader(new InputStreamReader(connectFromClient.getInputStream()));

        //will write back to client
        PrintWriter pr=new PrintWriter(new OutputStreamWriter(connectFromClient.getOutputStream()));

        PrintWriter writeToFile=null;
        //sending following statements to client
        pr.println("Connection established with server! Give a file name");
        pr.flush();

        String response;

        while(true){

            while((response=input.readLine()) != null)
            {
            if(response==null)
                break;
            else{

            System.out.println(response);
            if(response.contains(".txt")){

                //file=new File("FromClient.txt");
                  file=new File("FromClient"+response);
                if(!file.exists())
                    file.createNewFile();
                writeToFile=new PrintWriter(file);
            }
            else{

                //writeToFile=new PrintWriter(file);
                writeToFile.println(response);
            }

            pr.println("Echo from server -> " + response);

            //System.out.println("Adding these contents to a file");
            writeToFile.flush();
            pr.flush();
            }
            }
        }


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


}

}

1 个答案:

答案 0 :(得分:0)

它在这一行中陷入了客户端:

 while ((str = br.readLine()) != null) {
     System.out.println(str);
 }

等待更多服务器线。

您必须告诉客户端何时停止从套接字流中读取文件数据并跳转到下一个文件。例如,您可以在文件内容之前发送要传输的字节数,从流中读取该字节数,然后转到下一个文件。

我发布您的代码修改作为示例,作为我想表达的想法。 首先,我获得文件的总数(我想文本文件),然后将其发送到服务器,服务器确切地知道在跳转到下一个文件之前要读取多少行。 例如,可以将其更改为使用文件的总大小(以字节为单位)。 这段代码不是100%正确,它可以作为一个例子。

客户端:

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

    public class EchoClient {

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

            try {
                final Socket clientSock = new Socket("localhost", 8888);

                // to read from server
                final BufferedReader br = new BufferedReader(new InputStreamReader(clientSock.getInputStream())); // to
                // read

                // to write to server
                final PrintWriter pw = new PrintWriter(clientSock.getOutputStream(), true);

                // for user input
                final BufferedReader userIn = new BufferedReader(new InputStreamReader(System.in));
                BufferedReader fileContent = null;
                String str = null;
                String fileContentLine = null;
                while (true) {
                    System.out.println("Print filename");
                    str = userIn.readLine(); // read user input
                    fileContent = new BufferedReader(new FileReader(str));
                    pw.println(str);
                    // first count the line number:
                    int lineno = 0;
                    while ((fileContentLine = fileContent.readLine()) != null) {
                        lineno++;
                    }
                    fileContent.close();
                    //
                    fileContent = new BufferedReader(new FileReader(str));
                    pw.println(String.valueOf(lineno));
                    while ((fileContentLine = fileContent.readLine()) != null) {
                        pw.println(fileContentLine);
                    }

                    for (int i = 0; i < lineno; i++) {
                        str = br.readLine();
                        System.out.println(str);
                    }

                    pw.flush();

                }

            } catch (final IOException e) {
                e.printStackTrace();
            }

        }
    }

服务器:

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;

    public class EchoServer {

        public static void main(final String[] args) {
            try {
                final ServerSocket serverSock = new ServerSocket(8888);
                System.out.println("Waiting for client");
                final Socket connectFromClient = serverSock.accept();

                File file = null;
                // reading data from client
                final BufferedReader input = new BufferedReader(new InputStreamReader(connectFromClient.getInputStream()));

                // will write back to client
                final PrintWriter pr = new PrintWriter(new OutputStreamWriter(connectFromClient.getOutputStream()));

                PrintWriter writeToFile = null;
                // sending following statements to client
                pr.println("Connection established with server! Give a file name");
                pr.flush();

                String response;

                while (true) {
                    final String fileName = input.readLine();
                    file = new File("FromClient" + fileName);
                    if (!file.exists()) {
                        file.createNewFile();
                    }
                    writeToFile = new PrintWriter(file);
                    final String sLineNo = input.readLine();
                    final int lineno = Integer.parseInt(sLineNo);
                    for (int i = 0; i < lineno; i++) {
                        response = input.readLine();
                        System.out.println(response);

                        // writeToFile=new PrintWriter(file);
                        writeToFile.println(response);

                        pr.println("Echo from server -> " + response);

                        // System.out.println("Adding these contents to a file");
                        writeToFile.flush();
                        pr.flush();
                    }
                    writeToFile.close();
                }

            } catch (final IOException e) {
                e.printStackTrace();
            }

        }
    }

我希望能提供帮助。