- public class WebServerDeom
- {
- public static void main(String[] args) throws IOException
- {
- ServerSocket ss = new ServerSocket(8080);
-
- Socket s = ss.accept();
-
- PrintWriter netOut =
- new PrintWriter(new OutputStreamWriter(s.getOutputStream()), true);
-
- InputStream netIn = s.getInputStream();
- byte[] buf = new byte[1024];
- int len = 0;
- while(-1 != (len=netIn.read(buf))) //程序在此挂住了,如何才能使len==-1?
- {
- System.out.println(new String(buf, 0, len));
- }
-
- //向客户端返回信息
- netOut.println("<h1 style='color:red'>Hello client!</h1>");
- fileOut.close();
- s.close();
- ss.close();
- }
- }
复制代码 在IE中输入网址:http://127.0.0.1:8080 ,总出现不了结果。经检查,程序在netIn.read(buf)时,被阻塞,但不知怎样才能使程序继续向下执行? |