代码中,红色标出的是使用flush的地方。后面两处flush,我大概知道是为了输出到内存。但第一个flush实在不可理解。
当删去第一个flush时,其上一行的println无法输出。
然而,就拿服务器端的程序来说,我在程序里标了橙色,也就是他写了
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
查阅API文档,对于PrintWriter,有:
public PrintWriter(OutputStream out,
boolean autoFlush)
Creates a new PrintWriter from an existing OutputStream. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will convert characters into bytes using the default character encoding.
Parameters:
out - An output stream
autoFlush - A boolean; if true, the println, printf, or format methods will flush the output buffer
也就是说,这个true会使得使用println自动flush。可结果却没有自动flush。似乎必须手动。这与API文档就相悖了啊??
求助,这是为啥呢???谢谢!
代码如下
/**
*
*/
package com.dnion.socket;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
***************************************************************
* 项目名称:JavaThread
* 程序名称:JabberServer
* 日期:2015-7-11 下午21:01:12
* 作者:Administrator
* @version
***************************************************************
*/
public class JabberServer {
public static int PORT = 8080;
public static void main(String[] agrs) {
ServerSocket s = null;
Socket socket = null;
BufferedReader br = null;
PrintWriter pw = null;
try {
//设定服务端的端口号
s = new ServerSocket(PORT);
System.out.println("ServerSocket Start:"+s);
//等待请求,此方法会一直阻塞,直到获得请求才往下走
socket = s.accept();
System.out.println("Connection accept socket:"+socket);
//用于接收客户端发来的请求
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//用于发送返回信息,可以不需要装饰这么多io流使用缓冲流时发送数据要注意调用.flush()方法
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
while(true){
String str = br.readLine();
if(str.equals("END")){
break;
}
System.out.println("Client Socket Message:"+str);
Thread.sleep(1000);
pw.println("Message Received");
pw.flush();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
System.out.println("Close.....");
try {
br.close();
pw.close();
socket.close();
s.close();
} catch (Exception e2) {
}
}
}
}
客户端:
/**
*
*/
package com.dnion.socket;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
/**
***************************************************************
* 项目名称:JavaThread 程序名称:JabberClient
* 作者:Administrator
* @version
***************************************************************
*/
public class JabberClient {
/**
* 方法名:main
*
* @param @param args
* @return void
*/
public static void main(String[] args) {
Socket socket = null;
BufferedReader br = null;
PrintWriter pw = null;
try {
//客户端socket指定服务器的地址和端口号
socket = new Socket("127.0.0.1", JabberServer.PORT);
System.out.println("Socket=" + socket);
//同服务器原理一样
br = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream())));
for (int i = 0; i < 10; i++) {
pw.println("howdy " + i);
pw.flush();
String str = br.readLine();
System.out.println(str);
}
pw.println("END");
pw.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
System.out.println("close......");
br.close();
pw.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
2 个解决方案
#1
求教啊 有大侠知道吗 搞了一天了
#2
我试了下,,,是不需要写的,,, pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);,这个可以自动刷新,,,,
#1
求教啊 有大侠知道吗 搞了一天了
#2
我试了下,,,是不需要写的,,, pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);,这个可以自动刷新,,,,