ChatServer源代码如下:
import java.io.*;
import java.io.IOException;
import java.net.*;
import java.util.ArrayList;
import java.util.List;public class ChatServer  {

boolean started=false;
ServerSocket ss = null;
DataInputStream dis = null;
// DataOutputStream dos =null;

List<Client> clients=new ArrayList<ChatServer.Client>(); 

public static void main (String[] args) {
//System.out.println("客户端已经启动完毕");
new ChatServer().start();
System.out.println("客户端已经启动完毕");
}

public void start(){
try {  
ss=new ServerSocket(8888);
started=true;
} catch(BindException e){
System.out.println("端口已经被使用!!!!不用继续开启!!");
System.exit(0);
}
catch (IOException e){
e.printStackTrace();
System.out.println("系统没有连接上");
}
try{

while(started){
Socket s=ss.accept();
Client c=new Client(s);
new Thread(c).start(); 
clients.add(c); 
System.out.println("a client connetcted!:");

//dis.close();
}
}catch(NullPointerException e){
System.exit(0);
} catch (IOException e) { 
System.out.println("客户端已经关闭!!!!");
}
finally {
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

class Client implements Runnable{ private Socket s; 
private DataInputStream dis = null;
private DataOutputStream dos = null;
private boolean bConneted = true; 

public Client(Socket s){
this.s =s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void sent(String str){
  try {
dos.writeUTF(str);
} catch (IOException e) {
 e.printStackTrace();
}
}

public void run() {
  
  try{
while(bConneted){  

String str = null;
try {
str = dis.readUTF();

} catch (IOException e) {
System.out.println("已经关闭");
System.exit(0);
for (int i =0;i<clients.size();i++){
Client c= clients.get(i);
c.sent(str); 
System.out.println("sjdhja kfwhjeh jke h");
}

}

System.out.println(str);
}

  }finally {
try{
if(dis!=null) dis.close();
if(dos != null) dos.close();
if(s !=null)   s.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
}
========================================================================================================
 ChatClient代码 如下:
import java.awt.event.*;
import java.awt.*;
import java.io.IOException;
import java.net.*;
import java.io.*;public class ChatClient extends Frame {
/**
 * 
 */
//private static final long serialVersionUID = 1877112402334898563L;
Socket s = null;
DataOutputStream dos = null;
DataInputStream dis  = null;
private boolean bConnected = false; TextField tfTxt = new TextField(); TextArea taContent = new TextArea(); public static void main(String[] args) {
new ChatClient().launchFrame();
} public void launchFrame() {
this.setLocation(200, 200);
this.setSize(500, 300);
this.add(tfTxt, BorderLayout.SOUTH);
add(taContent, BorderLayout.NORTH);
pack();
this.addWindowListener(new WindowAdapter() { @Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
disconnet();
System.exit(0);
}
});

tfTxt.addActionListener(new TFListener());
this.setVisible(true);
connet();

new Thread(new RecvThread()).start();

} public void connet() {
try {
s = new Socket("127.0.0.1", 8888);
dos = new DataOutputStream(s.getOutputStream());
dis =new DataInputStream(s.getInputStream());
bConnected = true;
System.out.print("had conneted!");// 测试的
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } public void disconnet() {
try {
dos.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} private class TFListener implements ActionListener { @Override
public void actionPerformed(ActionEvent e) { String str = tfTxt.getText().trim();
taContent.setText(str);
tfTxt.setText(null); try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} } private class RecvThread implements Runnable{ @Override
public void run() {
try{
while(bConnected){
String str = dis.readUTF();
System.out.println("fakfkhaklsdhajjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj");
taContent.setText(taContent.getText() + str + '\n');
}
}catch (IOException e){
e.printStackTrace();
}
}

}


}