服务器:
package zepc.web;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
public class MainFrame extends Thread implements ActionListener{
JScrollPane spmessage,spsend;
JDialog d;
JPanel pnl;
JTextArea tamessage,tasend;
JButton bsend,breset;
ServerSocket server;
Socket socket=null;
DataInputStream in;
DataOutputStream out;
public void talk(){
d=new JDialog(frm, "服务器聊天室");
pnl=new JPanel();
tamessage=new JTextArea();
tamessage.setEditable(false);
tasend=new JTextArea();
spmessage=new JScrollPane(tamessage);
spsend=new JScrollPane(tasend);
bsend=new JButton("发送");
breset=new JButton("清除");
d.setLayout(new BorderLayout());
pnl.setLayout(null);
spmessage.setBounds(0, 0, 400, 350);
spsend.setBounds(0, 350, 400, 200);
bsend.setBounds(100, 560, 60, 30);
breset.setBounds(170,560,60,30);
bsend.addActionListener(this);
pnl.add(spmessage);
pnl.add(spsend);
pnl.add(bsend);
pnl.add(breset);
pnl.setBackground(Color.CYAN);
d.add(pnl,BorderLayout.CENTER);
d.setLocation(500,20);
d.setSize(600, 630);
d.setVisible(true);
d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
}
public void server(){
try{
server=new ServerSocket(7777);
System.out.println("服务器创建成功");
while(true){
socket=server.accept();
System.out.println("套接字链接成功");
this.talk();
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
this.start();
}
}catch(Exception e){
e.printStackTrace();
}
}
public void run(){
try{
String str=in.readUTF();
tamessage.append(str+"\n");
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String args[]){
MainFrame mf=new MainFrame();
mf.server();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==bsend){
try{
String strsend=tasend.getText();
out.writeUTF("服务器:"+strsend);
tamessage.append("服务器:"+strsend+"\n");
tasend.setText(null);
out.flush();
}catch(Exception e1){
e1.printStackTrace();
}
}
}
}
客户端:
package zepc.web;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class ClientFrame extends Thread implements ActionListener{
JFrame frm;
JPanel pnl;
JTextArea tamessage,tasend;
JScrollPane spmessage,spsend;
JButton bsend,breset;
DataOutputStream out;
DataInputStream in;
public void frm(){
frm=new JFrame("客户端");
pnl=new JPanel();
tamessage=new JTextArea();
tamessage.setEditable(false);