打算用java做一个聊天室的服务器,从网上找了下面的程序,改动了一下,但是页面不能显示。如果不采用线程,在得到socket连接后,直接write,然后关闭,页面可以显示。高人解释一下啊,急!!!
代码如下:// Server.java
import java.io.*;
import java.net.*;
import java.util.*;public class Server
{
  private ServerSocket ss;
  private Hashtable outputStreams = new Hashtable();
  public Server(int port) throws IOException
  {
    listen(port);
  }  private void listen(int port) throws IOException
  {
    ss = new ServerSocket(port);
    System.out.println("Listening on " + ss);
    while (true)
    {
      Socket s = ss.accept();
      System.out.println("Connection from " + s);      DataOutputStream pw = new DataOutputStream(s.getOutputStream());
      //PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream())));
      Date date = new Date();
      //java.util.Calendar
      String headers = "HTTP/1.1 200 OK\nDate: " + date.toString() + "\n"
          + "Content-type:text/html\n" + "\n<html>\n<head>\n"
          + "<META http-equiv=Content-Type content=\"text/html; charset=gb2312\">"
          + "\n<title>TEST</title>\n</head>\n<body>\n";
      //dout.write(headers.getBytes());
      System.out.println(headers);
      String msg = "<p>世界你好!</p>";      //pw.println(headers);
      //pw.write(headers);
      pw.write(headers.getBytes());
      //pw.flush();
      //pw.close();
      outputStreams.put(s, pw);
      new ServerThread(this, s).start();
    }
  }  Enumeration getOutputStreams()
  {
    return outputStreams.elements();
  }  void sendToAll(String message)
  {
    synchronized (outputStreams)
    {
      for (Enumeration e = getOutputStreams(); e.hasMoreElements(); )
      {
        DataOutputStream pw = (DataOutputStream) e.nextElement();
        //PrintWriter pw = (PrintWriter) e.nextElement();
        try
        {
          //pw.flush();
          //pw.println(message);
          pw.write(message.getBytes());
          pw.flush();
          //pw.close();
        }
        catch (Exception ie)
        {
          System.out.println(ie);
        }
      }
    }
  }  void removeConnection(Socket s)
  {
    synchronized (outputStreams)
    {
      System.out.println("Removing connection to " + s);
      outputStreams.remove(s);
      try
      {
        s.close();
      }
      catch (IOException ie)
      {
        System.out.println("Error closing " + s);
      }
    }
  }  static public void main(String args[]) throws Exception
  {
    try
    {
      int port = 8080;
      new Server(port);
    }catch(Exception ex)
    {
      System.out.println(ex.getMessage());
    }
  }
}// ServerThread.java
import java.io.*;
import java.net.*;public class ServerThread
    extends Thread
{
  private Server server;  private Socket socket;  public ServerThread(Server server, Socket socket)
  {
    this.server = server;
    this.socket = socket;
  }  public void run()
  {
    try
    {
      String message = "<p>世界你好!</p>";
      //for (int i = 0; i < 10; i++)
      //{
      System.out.println("Sending " + message);
      server.sendToAll(message);
      //}
    }
    catch (Exception ie)
    {
    }
  }
}

解决方案 »

  1.   

    listen()与 sendToAll()中
    DataOutputStream pw = new DataOutputStream(s.getOutputStream());
    改为:
    PrintStream pw = new PrintStream (s.getOutputStream());
    看看
      

  2.   

    你的客户端接收的socket能够收到server传来的信息么
      

  3.   

    我没有写客户端程序啊,客户端就只有浏览器,像上面的代码,我希望在浏览器输入http://localhost:8080/ ,页面显示一行“世界你好!”。然后我再打开一个页面,输入地址,第一个页面上会再显示一行“世界你好!”,第二个页面显示一行“世界你好”,以此类推。上面的代码不能实现这个功能吗?