首先运行Server端,然后运行多个客户端
源代码:http://pan.baidu.com/s/1bnb4Mq7
聊天室Server端
import java.io.IOException;
import java.net.*;
import java.io.*;
import java.util.*;
public class ChatServer {
ServerSocket ss=null;
boolean started=false;
Socket s=null;
List<Client> clients=new ArrayList<Client>();//接收客户端连接
public static void main(String[] args) {
new ChatServer().start();
}
public void start(){
int counter=1;
try {
ss=new ServerSocket(7777);
started=true;
}catch(BindException e){
System.out.println("端口正在使用。。。。");
System.out.println("请先关闭相关的程序,在重新运行服务器");
System.exit(0);
}catch(IOException e){
e.printStackTrace();
}
try{
while(started){
s=ss.accept();
Client c=new Client(s);
System.out.println("客户端:"+counter+++" 已经连上,"+"来自:"+
s.getInetAddress().getHostName()+"端口号为:"+s.getPort());
new Thread(c).start();
clients.add(c);//保存下来
}
}catch(IOException e){
e.printStackTrace();
}finally{
try {
ss.close();
}catch (Exception e) {
}
}
}
class Client implements Runnable{
private Socket s=null;
private DataInputStream dis=null;
private DataOutputStream dos=null;
boolean bContected=false;
public Client(Socket s){
this.s=s;
try {
dis=new DataInputStream(s.getInputStream());
dos=new DataOutputStream(s.getOutputStream());
bContected=true;
} catch (IOException e) {
e.printStackTrace();
}
}
public void send(String str){
try {
dos.writeUTF(str);
} catch (IOException e){
clients.remove(this);
System.out.println("对方退出了,也在clients中去掉了");
}
}
public void run() {
Client c=null;
try{
while(bContected)
{
String str=dis.readUTF();
for(int i=0;i<clients.size();i++)
{
c=clients.get(i);
c.send("来自:"+s.getInetAddress().getHostName()+s.getPort()+" : "+str);
}
/*也行,但是慢,一下会锁定,效率低
for(Iterator<Client> it=clients.iterator();it.hasNext()){
Client c=it.next();
c.send(str);
}
*/
System.out.println("来自:"+s.getInetAddress().getHostName()+s.getPort()+" : "+str);
}
}catch(SocketException e){
System.out.println("来自:"+s.getInetAddress().getHostName()
+"端口号为:"+s.getPort()+" 的客户端关闭了!");
}catch(EOFException e){
System.out.println("来自:"+s.getInetAddress().getHostName()
+"端口号为:"+s.getPort()+" 的客户端关闭了!");
}catch(IOException e){
System.out.println("来自:"+s.getInetAddress().getHostName()
+"端口号为:"+s.getPort()+" 的客户端关闭了!");
}finally{
try {
if(dis!=null) dis.close();
if(s!=null) s.close();
if(dos!=null) dos.close();
//clients.remove(this);
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
客户端:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.net.*;
public class ChatClient extends Frame {
Socket s=null;
DataOutputStream dos=null;
DataInputStream dis=null;
boolean beContected=false;
private static final long serialVersionUID = 1L;
TextField tfTxt = new TextField();
TextArea taContent = new TextArea();
public static void main(String[] args) {
new ChatClient().launchFrame();
}
public void launchFrame() {
setLocation(300, 200);
setSize(100, 100);
add(tfTxt, BorderLayout.SOUTH);
add(taContent, BorderLayout.NORTH);
pack();
tfTxt.addActionListener(new TfListener());
taContent.setEditable(false);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
discontect();
System.exit(0);
}
});
contect();
new Thread(new RevThread()).start();
setVisible(true);
}
public void contect(){
try {
s=new Socket("127.0.0.1",7777);
this.setTitle("已连接");
dos=new DataOutputStream(s.getOutputStream());
dis=new DataInputStream(s.getInputStream());//接收来自由服务器的信息()
//System.out.println("==Contected!==");
beContected=true;
} catch (UnknownHostException e) {
System.out.println("请确保服务器已经开启!!!");
} catch (IOException e) {
System.out.println("请检查设置!!!");
}
}
public void discontect(){
try {
dos.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private class TfListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String str=tfTxt.getText().trim();
//taContent.setText(str); //最后才注释
tfTxt.setText("");
try {
dos.writeUTF(str);
dos.flush();
}catch(IOException e1){
}
}
}
public class RevThread implements Runnable{
public void run() {
try {
while(beContected){
String str;
str = dis.readUTF();
//System.out.println(str);
taContent.setText(taContent.getText()+str+'\n');
}
}catch(SocketException e){
System.out.println("退出了,拜拜--bye");
}catch(EOFException e){
System.out.println("退出了,拜拜");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
运行效果如图: