Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/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 在客户端通过套接字逐个接收多条线路_Java_File_Sockets - Fatal编程技术网

Java 在客户端通过套接字逐个接收多条线路

Java 在客户端通过套接字逐个接收多条线路,java,file,sockets,Java,File,Sockets,我使用while循环通过套接字逐行发送文件。在客户端应该编码什么?在服务器端的每次迭代中,当发送字符串时,在客户端的while循环的每次迭代中也会接收到字符串 我的服务器端代码: File file = new File("Text.txt"); Scanner scan = new Scanner(file); while(scan.hasNextLine()){ String str = scan.nextLine(); pw.println(str); } 使用PrintWriter的对

我使用while循环通过套接字逐行发送文件。在客户端应该编码什么?在服务器端的每次迭代中,当发送字符串时,在客户端的while循环的每次迭代中也会接收到字符串

我的服务器端代码:

File file = new File("Text.txt");
Scanner scan = new Scanner(file);
while(scan.hasNextLine()){
String str = scan.nextLine();
pw.println(str);
}
使用PrintWriter的对象pw,我通过套接字发送数据。请提及接收方的方法

以下是客户端:

String str = br.readLine(); 
System.out.println(str);

BufferedReader.readLine()
在流的末尾返回
null
,因此常用的习惯用法是在条件部分内部执行读取

String str = null;
while((str = br.readLine()) != null) {
    // do something with str
}

如果有一个方法
read
a
Line
。@Kayaman已经在使用br.readLine()函数,但却不知道如何在这个语句上应用循环,以便它被反复执行和接收行,那么问题中的代码是什么,它与套接字有什么关系?这是客户端代码吗?如果是,也显示服务器端代码。@Kayaman,否,其服务器端代码。println(str)在这里用于通过套接字发送字符串。我只显示了从文件中读取行并将其发送到客户端然后显示客户端代码的代码。请记住,
BufferedReader.readLine()
在流结束时返回
null