server代码:import java.io.IOException; 
import java.net.*; 
import java.io.*; 
public class ChatServer { 
 boolean started = false; 
 ServerSocket ss = null; 
 public static void main(String[] args) { 
  new ChatServer().start(); 
 } 
  
 public void start(){ 
  try { 
   ss = new ServerSocket(7000); 
   started = true; 
  }catch(BindException e){ 
   System.out.println("断口使用中"); 
   System.out.println("请重新登陆"); 
   System.exit(0); 
  } 
  catch (IOException e) { 
   e.printStackTrace(); 
  } 
  try { 
   while(started){ 
    Socket s = ss.accept(); 
    System.out.println("a client connected"); 
    Client c = new Client(s); 
    new Thread(c).start(); 
    } 
   }catch (IOException e) { 
   e.printStackTrace(); 
   }finally { 
    try { 
    ss.close(); 
    } catch (IOException e) { 
    e.printStackTrace(); 
    } 
   } 
  } 
  
   
 class Client implements Runnable{ 
  private Socket s; 
  private DataInputStream dis = null; 
  boolean bconnected = false; 
   
  Client(Socket s){ 
   this.s = s; 
   try { 
    dis = new DataInputStream(s.getInputStream()); 
    bconnected = true; 
   } catch (IOException e) { 
    e.printStackTrace(); 
   } 
  } 
  public void run() { 
   try{ 
    while(bconnected){ 
    System.out.println(dis.readUTF()); 
       } 
   }catch (EOFException e) { 
    System.out.println("Client closed!"); 
   }catch (IOException e) { 
    e.printStackTrace(); 
   }finally { 
    try { 
         if(dis != null) dis.close(); 
      if(s != null) s.close(); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
         } 
       } 
   } 
 } 
} client 代码: 
import java.awt.*; 
import java.awt.event.*; 
import java.io.IOException; 
import java.net.*; 
import java.io.*; public class ChatClient { 
 public static void main(String[] args) { 
  new MyFrame("chat"); 
  } 

class MyFrame extends Frame{ 
 Button b = new Button("send"); 
 Socket s = null; 
 DataOutputStream dos = null; 
 TextField tf = new TextField(); 
 TextArea ta = new TextArea(); 
 DataInputStream dis = null; 
 boolean bconnected = false; 
  
 MyFrame(String s){ 
  super(s); 
  setLocation(400,300); 
  setVisible(true); 
  setSize(300,300); 
  setLayout(new BorderLayout()); 
  add("East",b); 
  b.addActionListener(new Monitor1()); 
  addWindowListener(new WindowAdapter() { 
   public void windowClosing(WindowEvent e) { 
              setVisible(false); 
              disconnected(); 
              System.exit(0); 
      } 
     }); 
  add("South",tf); 
  add("North",ta); 
  pack(); 
  connect(); 
  new Thread(new RecvThread()).start(); 
 } 
  
 public void connect(){ 
  try { 
   s = new Socket("127.0.0.1",7000); 
   dos = new DataOutputStream(s.getOutputStream()); 
   dis = new DataInputStream(s.getInputStream()); 
   System.out.println("connected"); 
   bconnected = true; 
  } catch (UnknownHostException e) { 
    
   e.printStackTrace(); 
  } catch (IOException e) { 
    
   e.printStackTrace(); 
  } 
   
   
    
  } 
  
 public void disconnected() { 
  try { 
   dos.close(); 
  } catch (IOException e) { 
    
   e.printStackTrace(); 
  } 
  try { 
   s.close(); 
  } catch (IOException e) { 
    
   e.printStackTrace(); 
  } 
 } class Monitor1  implements ActionListener { public void actionPerformed(ActionEvent e){ 
if(e.getSource()==b){ 
String str = tf.getText().trim(); 
ta.setText(tf.getText().trim()); 
System.out.println(); 
tf.setText(null); 
try { 
//DataOutputStream dos = new DataOutputStream(s.getOutputStream()); 
dos.writeUTF(str); 
dos.flush(); 
//dos.close(); 
} catch (IOException e1) { e1.printStackTrace(); 
} } 

} private class RecvThread implements Runnable{ public void run() { 
try{ 
while(bconnected){ 
String str = dis.readUTF(); 
System.out.println(str); 
//ta.setText(str); 

}catch(IOException e){ 
e.printStackTrace(); 
} } } } 
开启多个CLIENT端 为什么每个CLIENT端不能看到别人说的话啊  我不是已经用了一个线程来去启动了啊 

解决方案 »

  1.   

    以前做过,可能有点遗漏
    基本是这样的:
    服务器
      代开本地端口,等待连接
      当有客户端连接后,把客户端的IP,端口信息保存到Vector里
      当客户端发送消息后,把消息接住,并遍历Vector,得到所有客户端的地址信息,转发给客户端
    客户端
      基本都查不多
      

  2.   

    那你看下你的端口是不是打开
    运行->cmd->netstat -an
      

  3.   

    你用cmd打开,先启动chatserver,不要关,然后在启动2个chatclient,你都不要关,然后分别在每个chatclient上
    说话,你看看是不是在启动了那个chatserver全部显示出来了,不知道你是不是要的这个效果呵呵
      

  4.   

    我粗粗看了代码,有2处疑问
    1 服务器端你叫Client的类里面,只有
    System.out.println(dis.readUTF());   我没看到你把信息发送给其他连接的代码,甚至回复信息的代码都没有2 服务器端需要记录每个连接,并把它们发来的信息转发回去,这个数据结构我也没看到。