我自己做了一个小聊天室,用的APPLET,但是不知道怎么传参数进去请大侠帮忙改改,20分
chatServer文件:import java.net.*;
import java.io.*;
public class chatServer implements Runnable{
public static final int PORT=1234;
protected ServerSocket listen;
Thread connect;
clientThread cThread[]=new clientThread[5];
int num=0;
public chatServer()  {
try{
listen=new ServerSocket(PORT);
}
catch(UnknownHostException e){}
catch(IOException e2){}
connect=new Thread(this);
connect.start();

public void run() {
try {
  while(true) {
   Socket client=listen.accept();
   cThread[num]=new clientThread(this,client);
   cThread[num].start();
  num++;
    }
  }
catch(IOException e){}
}public void broadcast(String msg) {
  for(int i=0;i<num;i++) {
  try {
     cThread[i].out.writeUTF(msg);
      }
   catch(IOException e) {}
  }
 }public static void main(String args[]) {
  new chatServer();
  System.out.println("Chat Server is starting!.....");
  }
}
class clientThread extends Thread {
 protected Socket client;
 protected DataOutputStream out;
 protected DataInputStream  in;
 protected chatServer server;public clientThread(chatServer server,Socket client) {
  this.server=server;
  this.client=client;
    try{
    in=new DataInputStream(client.getInputStream());
    out=new DataOutputStream(client.getOutputStream());
  }
 catch(IOException e) {}
}
public void run() {
  try{
  while(true) {
  server.broadcast(in.readUTF());
 }
}
 catch(IOException e) {}
 }
}
客户端文件:import java.net.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.*;
//import java.awt.Panel;
class Spanel extends Panel{
TextField msg_txt;
JButton button;
Spanel(){
setLayout(new FlowLayout());
msg_txt=new TextField(20);
button=new JButton("送出");add(new JLabel("你说的话"));
add(msg_txt);
add(button);}
}public class chatClient extends Applet implements Runnable,ActionListener{
public static final int PORT=1234;
String name;
Socket socket;
DataOutputStream out;
DataInputStream in;
Thread thread;
TextArea chat_txt;
Spanel sp;public void init(){
setBackground(new Color(230,230,200));
setLayout(new BorderLayout());name=getParameter("Chatname");
chat_txt=new TextArea(10,45);
chat_txt.setEditable(false);
Spanel sp=new Spanel();
add("Center",chat_txt);add("South",sp);
sp.button.addActionListener(this);
chat_txt.setBackground(new Color(200,185,220));
}public void start(){
try{
socket=new Socket(this.getCodeBase().getHost(),PORT);
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}
catch(IOException e){}
chat_txt.append("★★★"+" "+name+",欢迎你来到聊天室 "+"★★★\n");
if(thread==null){
thread=new Thread(this);
thread.start();
}
}
public void run(){
try{
while(true){
chat_txt.append(in.readUTF()+'\n');
}
}
catch(IOException e){}
}public void actionPerformed(ActionEvent e){
if((name!=null)){
try{
out.writeUTF(name+"说→"+":"+sp.msg_txt.getText());
sp.msg_txt.setText(null);
}
catch(IOException e1){}
}
}
public static void main(String args[]){
new Spanel();
new chatClient();
}
}

解决方案 »

  1.   

    楼主我教你怎么贴代码:
    1、将代码进行良好的格式化,以方便阅读。
    2、在发帖文本框的上方单击“#”按钮,选择 Java
    3、将代码粘贴到【code=Java】和【/code】之间。发出来的帖子就会是下面的效果:public class Hello {    // 程序入口
        public static void main(String[] args) {
            System.out.println("Hello!");
        }
    }
      

  2.   

    import java.net.*; 
    import java.io.*; 
    public class chatServer implements Runnable{ 
    public static final int PORT=1234; 
    protected ServerSocket listen; 
    Thread connect; 
    clientThread cThread[]=new clientThread[5]; 
    int num=0; 
    public chatServer()  { 
    try{ 
    listen=new ServerSocket(PORT); 

    catch(UnknownHostException e){} 
    catch(IOException e2){} 
    connect=new Thread(this); 
    connect.start(); 

    public void run() { 
    try { 
      while(true) { 
      Socket client=listen.accept(); 
      cThread[num]=new clientThread(this,client); 
      cThread[num].start(); 
      num++; 
        } 
      } 
    catch(IOException e){} 
    } public void broadcast(String msg) { 
      for(int i=0;i <num;i++) { 
      try { 
        cThread[i].out.writeUTF(msg); 
          } 
      catch(IOException e) {} 
      } 
    } public static void main(String args[]) { 
      new chatServer(); 
      System.out.println("Chat Server is starting!....."); 
      } 

    class clientThread extends Thread { 
    protected Socket client; 
    protected DataOutputStream out; 
    protected DataInputStream  in; 
    protected chatServer server; public clientThread(chatServer server,Socket client) { 
      this.server=server; 
      this.client=client; 
        try{ 
        in=new DataInputStream(client.getInputStream()); 
        out=new DataOutputStream(client.getOutputStream()); 
      } 
    catch(IOException e) {} 

    public void run() { 
      try{ 
      while(true) { 
      server.broadcast(in.readUTF()); 


    catch(IOException e) {} 


    客户端文件: import java.net.*; 
    import java.io.*; 
    import java.awt.*; 
    import javax.swing.*; 
    import java.awt.event.*; 
    import java.applet.*; 
    //import java.awt.Panel; 
    class Spanel extends Panel{ 
    TextField msg_txt; 
    JButton button; 
    Spanel(){ 
    setLayout(new FlowLayout()); 
    msg_txt=new TextField(20); 
    button=new JButton("送出"); add(new JLabel("你说的话")); 
    add(msg_txt); 
    add(button); } 
    } public class chatClient extends Applet implements Runnable,ActionListener{ 
    public static final int PORT=1234; 
    String name; 
    Socket socket; 
    DataOutputStream out; 
    DataInputStream in; 
    Thread thread; 
    TextArea chat_txt; 
    Spanel sp; public void init(){ 
    setBackground(new Color(230,230,200)); 
    setLayout(new BorderLayout()); name=getParameter("Chatname"); 
    chat_txt=new TextArea(10,45); 
    chat_txt.setEditable(false); 
    Spanel sp=new Spanel(); 
    add("Center",chat_txt);add("South",sp); 
    sp.button.addActionListener(this); 
    chat_txt.setBackground(new Color(200,185,220)); 
    } public void start(){ 
    try{ 
    socket=new Socket(this.getCodeBase().getHost(),PORT); 
    in=new DataInputStream(socket.getInputStream()); 
    out=new DataOutputStream(socket.getOutputStream()); 

    catch(IOException e){} 
    chat_txt.append("★★★"+" "+name+",欢迎你来到聊天室 "+"★★★\n"); 
    if(thread==null){ 
    thread=new Thread(this); 
    thread.start(); 


    public void run(){ 
    try{ 
    while(true){ 
    chat_txt.append(in.readUTF()+'\n'); 


    catch(IOException e){} 
    } public void actionPerformed(ActionEvent e){ 
    if((name!=null)){ 
    try{ 
    out.writeUTF(name+"说→"+":"+sp.msg_txt.getText()); 
    sp.msg_txt.setText(null); 

    catch(IOException e1){} 


    public static void main(String args[]){ 
    new Spanel(); 
    new chatClient();