就是一个很简单的统计访问次数的程序,以下是服务器端和客户端:有两个问题:
(1)点击关闭按钮时,会出现异常,貌似是服务器accept()方法那地方抛出的,不知道怎么改
(2)将applet嵌入html文件中,打开不显示访客次序,就空白的标签,而我把applet改成应用程序时,在dos下运行正常,  
     不知道是怎么回事。
服务器端:import public class VisitOrderServer  extends JFrame implements ActionListener
{
  //data field
  private int count;                           //累计访问次数
  private RandomAccessFile records;           //存储访问次数
  private JTextArea jtaLog;                    //显示服务器日志
  private JButton jbtStop;            //服务器"结束"按钮
  private JPanel jpanelSouth;                  //盛装jbtStop
  private ServerSocket serverSocket;           //服务器端套接字
  
  //method field
  VisitOrderServer()
  {
     jtaLog = new JTextArea();
     jtaLog.setEditable(false);
     jbtStop = new JButton("关闭服务器");
     jpanelSouth = new JPanel(new FlowLayout());
     jbtStop.addActionListener(this);
     //排布组件
     jpanelSouth.add(jbtStop);
     add(jtaLog,BorderLayout.CENTER);
     add(jpanelSouth,BorderLayout.SOUTH);
  }
  
  public void start()
  {
    try
   {
   records =  new RandomAccessFile("count.dat","rw");
              serverSocket = new ServerSocket(8000);
              jtaLog.append("服务器开启\n");
            if( records.length() == 0 )
               count = 0;
            else
               count = records.readInt();
            while(true)
          {
           Socket customSocket = serverSocket.accept();
           count++;
           DataOutputStream output = new DataOutputStream(customSocket.getOutputStream());
           output.writeInt(count);
           }
   }
   catch(IOException ex)
   {
    ex.printStackTrace();
   }
  }
  
  public void actionPerformed(ActionEvent event)
  {
    if( event.getSource() == jbtStop )
    { 
      try
      {
        if(serverSocket != null) serverSocket.close();
        records.seek(0);
        records.writeInt(count);
        jtaLog.append("服务器关闭\n");
      }
      catch(IOException ex)
      {
        ex.printStackTrace();
      }
    }
  }
  
  public static void  main(String[] args)
  {
    VisitOrderServer frame = new VisitOrderServer();
    frame.setTitle("服务器端");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400,300);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.start();
  }
}客户端applet:
import public  class VisitOrderClient extends JApplet
{
//data  field
public JLabel  jlblDisplay = new JLabel();       //显示访问次序的标签

//mehod field

public void init()
{
add(jlblDisplay);
}

public void start()
{
 try
 {
    Socket customSocket = new Socket(getCodeBase().getHost(),8000);
    DataInputStream input = new DataInputStream(customSocket.getInputStream());
    jlblDisplay.setText("  你是本站的第 "  + input.readInt() + " 位访客" );
    customSocket.close();
 }
 catch(IOException ex)
 {
   ex.printStackTrace();
 }
}

解决方案 »

  1.   


    额,貌似没触犯applet的安全机制啊
      

  2.   

    第一个问题,javadoc明确指出了,关闭serversocket,那么所有使用调用该serversocket的accept中线程都会抛出异常close
    public void close()
               throws IOException
    Closes this socket. Any thread currently blocked in accept() will throw a SocketException. 
    If this socket has an associated channel then the channel is closed as well. 
    Throws: 
    IOException - if an I/O error occurs when closing the socket.第二个问题,查看一下是否出现了异常,比如,异常时lbl设置一下异常信息看看
    try
             {
                   Socket customSocket = new Socket(getCodeBase().getHost(),8000);
                   DataInputStream input = new DataInputStream(customSocket.getInputStream());
                   jlblDisplay.setText("  你是本站的第 "  + input.readInt() + " 位访客" );
                   customSocket.close();
             }
             catch(IOException ex)
             {
                  //ex.printStackTrace();
                  jlblDisplay.setText(e.getMessage()); //查看时候有异常
             }
      

  3.   

    1) close()确实会导致accept()抛出异常,这是正常的,正确地捕获就好。
    2) applet只能与下载此applet的服务器的端口建立连接,你网页所在的服务器是否是提供连接服务的服务器