我用JAVA写了一个简单的聊天程序,大概的功能为,一个服务器端,多个客户端可以去链接,并且,客户端向服务端发送消息,服务端接收之后将这个消息显示并且发到每一个客户端上,但是不知道该怎么实现发送到每一个客户端的功能,代码如下,请指教-------------------------服务器端---------------------------------package src;import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class chatServer extends Frame{

TextArea ta = new TextArea("Server online");
ServerSocket ss = null;
//DataInputStream dis = null;
String sendStr = null;
int number = 0;


public static void main(String[] args) {
new chatServer().launchFrame();
} void launchFrame()
{
setLocation(300,300);
setSize(300,300);
setVisible(true);
add(ta);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
conneted();
}
//此方法用于连接客服端,并实现数据的传输
void conneted()
{
try 
{
ss = new ServerSocket(8888);
while(true)
{
Socket s = ss.accept();
new Thread(new threadClient(s)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}

//设置多线程以实现多个客户端同时连接服务器
class threadClient implements Runnable
{
private Socket c =null;
public threadClient(Socket s)
{
this.c = s;
}
public void run()
{
try 
{
String tstr = null;
ta.append(++number+"computer is conneted"+"\n");
new Thread(new threadsend(c,tstr)).start();
DataInputStream dis = new DataInputStream(c.getInputStream());
while(true)
{
tstr = dis.readUTF();
ta.append(tstr);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//设置多线程实现对多个客户端的消息发送
class threadsend implements Runnable
{
private Socket c =null;
private String s = null;
private String temp = null;
public threadsend(Socket s,String str)
{
this.c = s;
this.s=str;
}
public void run() {
try {
DataOutputStream dos = new DataOutputStream(c.getOutputStream());
while(true)
{
if(temp!=s)
{
dos.writeUTF(s);
temp=s;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}

}
}
--------------------------------客户端--------------------------------package src;import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class chatServer extends Frame{

TextArea ta = new TextArea("Server online");
ServerSocket ss = null;
//DataInputStream dis = null;
String sendStr = null;
int number = 0;


public static void main(String[] args) {
new chatServer().launchFrame();
} void launchFrame()
{
setLocation(300,300);
setSize(300,300);
setVisible(true);
add(ta);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
conneted();
}
//此方法用于连接客服端,并实现数据的传输
void conneted()
{
try 
{
ss = new ServerSocket(8888);
while(true)
{
Socket s = ss.accept();
new Thread(new threadClient(s)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}

//设置多线程以实现多个客户端同时连接服务器
class threadClient implements Runnable
{
private Socket c =null;
public threadClient(Socket s)
{
this.c = s;
}
public void run()
{
try 
{
String tstr = null;
ta.append(++number+"computer is conneted"+"\n");
new Thread(new threadsend(c,tstr)).start();
DataInputStream dis = new DataInputStream(c.getInputStream());
while(true)
{
tstr = dis.readUTF();
ta.append(tstr);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//设置多线程实现对多个客户端的消息发送
class threadsend implements Runnable
{
private Socket c =null;
private String s = null;
private String temp = null;
public threadsend(Socket s,String str)
{
this.c = s;
this.s=str;
}
public void run() {
try {
DataOutputStream dos = new DataOutputStream(c.getOutputStream());
while(true)
{
if(temp!=s)
{
dos.writeUTF(s);
temp=s;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}

}
}