Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 客户端服务器套接字编程未输出到txt_Java_Sockets_Io - Fatal编程技术网

Java 客户端服务器套接字编程未输出到txt

Java 客户端服务器套接字编程未输出到txt,java,sockets,io,Java,Sockets,Io,我不熟悉套接字编程。我有一个客户端,它从桌面上的txt文件中检索url,并通过套接字将其传递给服务器。服务器检索网页的html代码作为sting,替换字符串中的一些标记,并将字符串(html代码)发送回客户端。当我将字符串输出到控制台时,我能够看到它,但是当我试图将它传递到本地txt/html文件时,我得到一个空白文件/页面。有人能指出我做错了什么吗 服务器: public class Server { public static void main(String args[]) {

我不熟悉套接字编程。我有一个客户端,它从桌面上的txt文件中检索url,并通过套接字将其传递给服务器。服务器检索网页的html代码作为sting,替换字符串中的一些标记,并将字符串(html代码)发送回客户端。当我将字符串输出到控制台时,我能够看到它,但是当我试图将它传递到本地txt/html文件时,我得到一个空白文件/页面。有人能指出我做错了什么吗

服务器:

 public class Server {
   public static void main(String args[]) {
    String fromClient = "";
    StringBuilder htmlCode = new StringBuilder();
    StringBuilder fClient = new StringBuilder();
    String str = null;
    try(ServerSocket welcomeSocket = new ServerSocket(8082)){
        System.out.println("Server started, waiting for clients...");
        while(true){
            try(Socket connectionSocket = welcomeSocket.accept();
                BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
                DataOutputStream toClient = new DataOutputStream(connectionSocket.getOutputStream())){
                for(String line = inFromClient.readLine(); line != null && line.length()>0; line = inFromClient.readLine()){
                    fClient.append(line);
                }
                if(fClient.toString().contains("www.mmix.cs.hm.edu")){
                    fromClient = "www.mmix.cs.hm.edu";
                }
                try(InputStream url = new URL("http://"+fromClient+"/index.html").openStream();
                    BufferedReader getUrl = new BufferedReader(new InputStreamReader(url))){
                        for(String line = getUrl.readLine(); line != null; line = getUrl.readLine()){
                            htmlCode.append(line);
                        }
                    str = htmlCode.toString();
                    String imgReplaced = replaceImgs(str);
                    String replaced = replaceYou(imgReplaced);
                    toClient.writeBytes(replaced);  
                }
            }
        }
    }
    catch(IOException io){
        io.printStackTrace();
    }
}
客户:

 public class Client {
  public static void main(String[] args) {
    String url;
    String returned;
    try(Socket clientSocket = new Socket("localhost", 8082);
        BufferedReader inFromUser = new BufferedReader(new FileReader("C:\\Users\\Anthony\\Desktop\\client.txt"));
        BufferedWriter toServer = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        BufferedWriter toBrowser = new BufferedWriter(new FileWriter("C:\\Users\\Anthony\\Desktop\\MMIX.txt"))){

        url = inFromUser.readLine();
        toServer.write("GET /index.html HTTP/1.0\r\n");
        toServer.write("Host: "+url+"\r\n");
        toServer.write("\r\n");
        toServer.flush();   

        returned = inFromServer.readLine();
        //Output to the console works
        System.out.println("FROM SERVER: " + returned);
        //Output to the stream toBrowser which writes to a txt file, not working
        for(returned = inFromServer.readLine(); returned != null && returned.length() > 0; returned = inFromServer.readLine()){
            toBrowser.write(returned);
            System.out.println(returned);
        }
    }
    catch(IOException io){
        io.printStackTrace();
    }
}

}

“当我试图将其传递到本地txt/html文件时”-您具体是怎么做的?客户端上的最后一个for循环。您是否只是执行shell重定向,如
java client>some_file.txt
?您可以在控制台上看到文本,但不能在文件中看到?如果是这样的话,很可能你的代码是好的,而问题就在别处。你没有关闭任何东西。服务器必须关闭接受的套接字,客户端必须关闭其套接字和
BufferedWriter
。为什么要在空行处停止循环?这是一个try-with-resources语句,一旦try语句完成,资源将自动关闭。我在空行处停了哪个循环?