//客户端程序
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Date;
import java.net.*;
import java.io.*;
public class ChatRoom extends JFrame
{
private JScrollPane sp;
private JTextArea ta;
private JTextField txt;
private JPanel p1,p2;
private JButton bt1,bt2;
private String words;//保存输入框内容
private boolean t=false;//用来控制输出
Socket socket=null;
BufferedReader br=null;
PrintWriter pw=null;
public ChatRoom(){
super("ChatRoom");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con=getContentPane();
p1=new JPanel();
bt1=new JButton("Send");
bt2=new JButton("Close");
p1.add(bt1);
p1.add(bt2);
p2=new JPanel();
ta=new JTextArea(10,40);
ta.setEditable(false);
sp=new JScrollPane(ta);
txt=new JTextField();
con.add(p1,BorderLayout.EAST);
con.add(sp,BorderLayout.WEST);
con.add(txt,BorderLayout.SOUTH);
this.pack();
setVisible(true);
ActionListener listener1=new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JButton bt=(JButton)e.getSource();
try{
if(bt==bt2){
if(socket != null)socket.close();
System.exit(0);}
}catch(Exception ee){ee.printStackTrace();}
}
};
bt2.addActionListener(listener1);
try{
socket = new Socket("localhost", 3000);
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
OutputStream os = socket.getOutputStream();
pw = new PrintWriter(os, true);
ActionListener listener2=new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==bt1||e.getSource()==txt){
words=txt.getText();
pw.println(socket.getInetAddress()+": "+words);
txt.setText("");
}
}
};
bt1.addActionListener(listener2);
txt.addActionListener(listener2);
while(true){
String data = br.readLine();
ta.setText(ta.getText()+"\n"+data);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{ if(pw !=null)pw.close();
if(br != null)br.close();
if(socket != null)socket.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void main(String args[]){
new ChatRoom();
}
}
//服务器端程序
import java.net.*;
import java.io.*;
import java.util.*;
public class ChatRoomServer
{
public static ArrayList<TcpThread> cl=new ArrayList<TcpThread>();
public static void main(String args[]){
ServerSocket ss=null;
Socket socket=null;
try{
ss = new ServerSocket(3000);
while(true){
socket = ss.accept();
TcpThread crc=new TcpThread(socket);
cl.add(crc);
crc.start();
System.out.println(socket.getInetAddress()+" "+cl.size());
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(socket != null)socket.close();
if(ss != null)ss.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
class TcpThread extends Thread
{
private Socket socket;
public static String data;
BufferedReader br=null;
PrintWriter pw = null;
InputStream is=null;
InputStreamReader isr=null;
OutputStream os =null;
public TcpThread(Socket socket){
this.socket=socket;
}
public void sendMess(){
pw.println(data);
}
public void run(){
try{
is = socket.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
os = socket.getOutputStream();
pw = new PrintWriter(os, true);
data="Welcome!: "+socket.getInetAddress();
for(Object ss:ChatRoomServer.cl)
((TcpThread)ss).sendMess();
while(true){
data = br.readLine();
if(data==null)break;
else if(data!=null){
System.out.println(data);
for(TcpThread ss:ChatRoomServer.cl)
ss.sendMess();
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(br!=null)br.close();
if(pw!=null)pw.close();
if(socket!=null)
socket.close();
ChatRoomServer.cl.remove(this);
System.out.println(socket.getInetAddress().toString()+"client quit\n"
+"linkings:"+ChatRoomServer.cl.size());
}catch(Exception e){
e.printStackTrace();
}
}
}
}
//ip地址要自己改,只要知道ip就可以了 。