server:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class Frame1 extends JFrame {
static Socket servervector[]=new Socket[20];
JPanel contentPane;
 BorderLayout borderLayout1 = new BorderLayout();
JButton jButton1 = new JButton();
 JTextArea jTextArea1 = new JTextArea();
  //Construct the frame
 public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
 try {
   jbInit();
 }
catch(Exception e)
 {
 e.printStackTrace();
  } }
  //Component initialization private void jbInit() throws Exception  {   //setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));   contentPane = (JPanel) this.getContentPane();   jButton1.setText("作成サーバー");
   jButton1.addActionListener(new java.awt.event.ActionListener(){
   public void actionPerformed(ActionEvent e) {
   jButton1_actionPerformed(e);
   }
    }); contentPane.setLayout(borderLayout1);this.setSize(new Dimension(200, 200)); this.setTitle("Frame Title");jTextArea1.setText("");  contentPane.add(jButton1, BorderLayout.NORTH); contentPane.add(jTextArea1, BorderLayout.CENTER); } //Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) {   System.exit(0); } }public void jButton1_actionPerformed(ActionEvent e) {   server hh=new server();
     hh.start();
   jButton1.setEnabled(false);
 }/////////////////
 class server extends Thread{
  ServerSocket serversocket=null;
public server(){
try{
      serversocket=new ServerSocket(2000);
     jTextArea1.append(serversocket.toString()+"成功に作成されました\n");
}catch(Exception e)
{
jTextArea1.append(e.getMessage()+"\n");
}
}///
public void run()
{while(true)
{
try{
 Socket socket=serversocket.accept();
 int j=0;
for(;j<1;j++)
{
if(servervector[j]==null)
 {
 servervector[j]=socket;///////
 jTextArea1.append(servervector[j].toString()+"\n");
  new client(socket,j).start();
 break;
 }
}if(j==1)
  {
    BufferedWriter outWriter=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
    outWriter.write("no place for you"+"\n");
    outWriter.close();
   }
}catch(Exception e)
{
jTextArea1.setText(e.getMessage());
}
}}
////////
class client extends Thread{
 Socket clientsocket=null;
 int j=0;
 BufferedReader inputReader=null;
 BufferedWriter outputWriter=null;
 public client(Socket socket,int j)
 {
 clientsocket=socket;
 this.j=j;
  try{
  inputReader=new BufferedReader(new InputStreamReader(clientsocket.getInputStream()));
  }catch(Exception e)
{
}
 }
 public void  run()
   {
 try
{
     String resu=null;
       do
   {       resu=inputReader.readLine();
      if(resu!=null)
      {
         jTextArea1.append(resu+"\n");
         if(resu.substring(resu.indexOf(":")+1).equals("exit"))
         {
         servervector[j].close();
         servervector[j]=null;
         //clientsocket.close();
         }
         sendmessage(resu);
     }
       }while(resu!=null);   }
catch(Exception e){
    System.out.println(e.getMessage());
  }
}/////
 void sendmessage(String messagestring){
  BufferedWriter outputWriter1=null;
   for(int i=0;i<20;i++)
 {
 if(servervector[i]!=null)
  {
     try{
        outputWriter1=new BufferedWriter(new OutputStreamWriter(servervector[i].getOutputStream()));
        outputWriter1.write(messagestring+"\n");
        outputWriter1.flush();         }catch(Exception e)
    {
   System.out.println(e.getMessage());
   }
  }
  }
 }
}
/////////////inner class
}
////public  static void main(String []args)
{
  new Frame1().show();
}}

解决方案 »

  1.   

    applet:
    //<applet codebase="." code="Applet1.class" height =200 width=200>
    //<param name="username" value="yang">
    //</applet>
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import javax.swing.*;
    import java.net.*;
    import java.io.*;public class Applet1 extends JApplet {
      boolean isStandalone = false;
        Socket socket=null;
        BufferedReader inputReader=null;
        BufferedWriter outputWriter=null;
      JTextArea showmessage=new JTextArea(5,6);  ////////////////////////////
      JScrollPane upscrollpane=null;
      JButton sendbutton=new JButton("send");
      JButton canclebutton=new JButton("cancle");
      JButton connectbutton=new JButton("connect");
      JTextField senmessage=new JTextField(40);
       JPanel downscroll=new JPanel(new FlowLayout());
       JLabel lable1=new JLabel("あなたの発言:");
       /////   //////
         String username=null;
           //Get a parameter value
       //Initialize the applet
      public void init() {
        try {
             jbInit();
            username=Integer.toString((int)java.lang.Math.random()*25);
       }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      //Component initialization
      private void jbInit() throws Exception {
        this.setSize(new Dimension(200, 200));
        this.getContentPane().setLayout(new BorderLayout());
        showmessage.setEditable(false);
         showmessage.setBackground(new Color(45,34,54));
         Font  hhfont=new Font("TimesRoman",Font.BOLD+Font.ITALIC,33);
          showmessage.setFont(hhfont);
           showmessage.setForeground(new Color(0,0,255));    upscrollpane=new JScrollPane(showmessage,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        this.getContentPane().add(upscrollpane,BorderLayout.CENTER);
            downscroll.add(lable1);
        downscroll.add(senmessage);
        downscroll.add(sendbutton);
        downscroll.add(canclebutton);
        downscroll.add(connectbutton);
        sendbutton.addActionListener(new eventHandle());
        canclebutton.addActionListener(new eventHandle());
        connectbutton.addActionListener(new eventHandle());
           canclebutton.setEnabled(false);
           sendbutton.setEnabled(false);
        this.getContentPane().add(downscroll,BorderLayout.SOUTH);
         }
      public void  destroy()
      {
    try{
                outputWriter.write(username+":"+"exit"+"\n");
               outputWriter.flush();
       socket.close();
       inputReader.close();
      outputWriter.close();
    }catch(Exception e){}
      }
      class eventHandle implements java.awt.event.ActionListener{
      public void actionPerformed(ActionEvent e)
      {
        if(((JButton)e.getSource()).equals(connectbutton))///connect to server
       {
          try{
          socket=new Socket("Goodjjy",2000);
          inputReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
          outputWriter=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
          canclebutton.setEnabled(true);
             sendbutton.setEnabled(true);
           connectbutton.setText("connected--");
          connectbutton.setEnabled(false);
          sendbutton.setDefaultCapable(true);
           getthread haha=new getthread();
             haha.start();
             outputWriter.write("システムの通知が来ました*****:皆様:\n"+username+"様-----ここに入れました"+"\n");
             outputWriter.flush();     }catch(Exception e2)
         {
         connectbutton.setText("Reconnect");
         senmessage.setText(e2.getMessage());
          }
          }
      if(((JButton)e.getSource()).equals(canclebutton))////cancle the sending message
      {
         senmessage.setText("");
      }
      if(((JButton)e.getSource()).equals(sendbutton))///send the message
      {
           try{
              if(senmessage.getText().length()==0)
              {
              JOptionPane.showMessageDialog(null,"     内容ないので\n少しもいいですよ","内容がないです?",JOptionPane.INFORMATION_MESSAGE);
              return;
              }
               outputWriter.write(username+":"+senmessage.getText()+"\n");
               outputWriter.flush();
               senmessage.setText("");    }catch(Exception e1)
        {
        System.out.println(e1.getMessage());
        }  }
      }
      }/////////
      class getthread extends Thread{
      public void run(){
      String resu=null;
        do
      {
      try{
          resu= inputReader.readLine();
         if(resu!=null)     showmessage.append(resu+"\n");
         upscrollpane.getVerticalScrollBar().getModel().setValue(upscrollpane.getVerticalScrollBar().getModel().getMaximum());
          if(resu.equals("no place for you"))
          {
          canclebutton.setEnabled(false);
          sendbutton.setEnabled(false);
           connectbutton.setText("Reconnect");
          connectbutton.setEnabled(true);
          JOptionPane.showMessageDialog(null,"サーバーにクラインとが満員だので","注意",JOptionPane.INFORMATION_MESSAGE);
          break;
          }
          }catch(Exception e)  {  }
       }while(resu!=null);
      }
      }////////////以下はメッセージを出すためにの内部のクラス:
    }
      

  2.   

    我以前在网上下载了一个,不过,现在找不到了,不能给你,不过你自己在
    www.goolge.com   去search一下,应该可以的!
      

  3.   

    上面有日文。
     你改掉就是了
     还有一点就是
    你的applet的socket必须连接到提供该applet类的那一台机器上面
     这是applet的安全特性;
      

  4.   

    Applets are not allowed to open network connections to any computer, except for the host that provided the .class files. This is either the host where the html page came from, or the host specified in the codebase parameter in the applet tag, with codebase taking precendence. For example, if you try to do this from an applet that did not originate from the machine foo.com, it will fail with a security exception:  Socket s = new Socket("foo.com", 25, true);
      

  5.   

    到这里看看吧
    http://61.144.28.245/hjc/web/newest.html
    好东西多多
      

  6.   

    看里面那个java socket编程就可以了