本帖最后由 贾振凯 于 2013-4-7 11:38 编辑
- public class InetAddressDemo {
- public static void main(String[] args) throws IOException, InterruptedException{
- System.out.println(InetAddress.getLocalHost().toString());
- ServerSocket ss = new ServerSocket(11100);
- Socket s = ss.accept();
- PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
- pw.println("<font size='18' color='red'>你好</font>");
- BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
- String line = null;
- while((line = br.readLine()) != null){
- if(line.contains("Connection")){
- break;
- }
- System.out.println(line);
- }
- /*InputStream is = s.getInputStream();
- byte[] buf = new byte[1024];
- int len = is.read(buf);
- System.out.println(new String(buf,0,len));*/
- s.close();
- ss.close();
- }
- }
复制代码 当用浏览器对这个自定义服务端发出请求时,如果没有这句话
if(line.contains("Connection")){
break;
}
那么程序就会阻塞,浏览器也能不收到"你好"字样
但是PrintWriter pw = new PrintWriter(s.getOutputStream(),true);定义的是自动刷新啊
为什么每次都要等到s.close()的时候才会把打印内容输出给浏览器??????????????
|
|