JAVA8中使用BufferedWriter的flush()失效的原因?
程序如下:TestServer.javapackagecom.mars.tcp;importjava.io.BufferedReader;importjava.io.Bu...
程序如下:
TestServer.java
package com.mars.tcp;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 为何flush失效
* @author Anonymous
*
*/
public class TestServer {
public static void main(String[] args) throws IOException {
System.out.println("server");
ServerSocket server = new ServerSocket(9999);
Socket client = server.accept();
BufferedReader br = new BufferedReader(
new InputStreamReader(
client.getInputStream()));
String data = br.readLine();
System.out.println(data);
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(
client.getOutputStream()));
bw.write("服务端响应");
bw.flush();
bw.close();
br.close();
System.out.println("服务端关闭");
client.close();
server.close();
}
}
#####################################################
TestClient.java
package com.mars.tcp;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class TestClient {
public static void main(String[] args) throws IOException {
System.out.println("client");
Socket client = new Socket("localhost",9999);
String str = "客户端发送第一次";
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(
client.getOutputStream()));
bw.write(str);
bw.flush();//为何刷新没用??
//bw.close();//换成close()就能传过去???
System.out.println("数据传出");
BufferedReader br = new BufferedReader(
new InputStreamReader(
client.getInputStream()));
String data = br.readLine();
System.out.println(data);
bw.close();
br.close();
client.close();
}
}
其它不变情况下TestClient的flush()换成close()能把字符串传过去,但会关掉Socket 展开
TestServer.java
package com.mars.tcp;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 为何flush失效
* @author Anonymous
*
*/
public class TestServer {
public static void main(String[] args) throws IOException {
System.out.println("server");
ServerSocket server = new ServerSocket(9999);
Socket client = server.accept();
BufferedReader br = new BufferedReader(
new InputStreamReader(
client.getInputStream()));
String data = br.readLine();
System.out.println(data);
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(
client.getOutputStream()));
bw.write("服务端响应");
bw.flush();
bw.close();
br.close();
System.out.println("服务端关闭");
client.close();
server.close();
}
}
#####################################################
TestClient.java
package com.mars.tcp;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class TestClient {
public static void main(String[] args) throws IOException {
System.out.println("client");
Socket client = new Socket("localhost",9999);
String str = "客户端发送第一次";
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(
client.getOutputStream()));
bw.write(str);
bw.flush();//为何刷新没用??
//bw.close();//换成close()就能传过去???
System.out.println("数据传出");
BufferedReader br = new BufferedReader(
new InputStreamReader(
client.getInputStream()));
String data = br.readLine();
System.out.println(data);
bw.close();
br.close();
client.close();
}
}
其它不变情况下TestClient的flush()换成close()能把字符串传过去,但会关掉Socket 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询