客户端
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;public class ChatClient extends JFrame{
JPanel contentPane;
Label label1 = new Label();
Label label2 = new Label();
Label label3 = new Label();
TextField textField1 = new TextField(); //输入IP
TextField textField2 = new TextField(); //输入要发送的信息
TextField textField3 = new TextField(); //输入端口
Button button1 =new Button();
Button button2 = new Button();
TextArea textArea1 = new TextArea();
Socket s = null;
InputStream is = null;
OutputStream os = null;
DataOutputStream dos = null;
DataInputStream dis = null;
boolean bConnected = false;

public ChatClient() {
contentPane = (JPanel) this.getContentPane(); //返回此窗体的 contentPane 对象(Container类型),并强制转换成JPanel类型(JPanel是Container的子类)
label1.setText("请输入对方IP地址:");
label2.setText("请输入对方端口号:");
label3.setText("请输入信息:");
button1.setLabel("连接");
button2.setLabel("发送");
label1.setBounds(new Rectangle(10,19,120,21));
label2.setBounds(new Rectangle(10,59,120,21));
label3.setBounds(new Rectangle(10,119,89,23));
textField1.setBounds(new Rectangle(135,23,134,19));
textField2.setBounds(new Rectangle(135,57,121,19));
textField3.setBounds(new Rectangle(126,125,191,21));
textArea1.setBounds(new Rectangle(11,259,333,135));

button1.setBounds(new Rectangle(280,55,59,21));
button2.setBounds(new Rectangle(140,189,59,21));

button1.addActionListener(listener);
button2.addActionListener(listener);
contentPane.add(label1);
contentPane.add(label2);
contentPane.add(label3);
contentPane.add(textField1);
contentPane.add(textField2);
contentPane.add(textField3);
contentPane.add(button1);
contentPane.add(button2);
contentPane.add(textArea1);
this.setSize(new Dimension(363,475));
this.setLocation(300,200);
this.setTitle("网络聊天");
this.setLayout(new BorderLayout());
this.setVisible(true);
try {
while(true){
textArea1.append("到了");
String string = dis.readUTF();
textArea1.append(string+"\n");
//System.out.println(string);
if(string.equals("88")){
dos.close();
os.close();
dis.close();
is.close();
s.close();
break;
}
}
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}

protected void processWindowEvent(WindowEvent e){
super.processWindowEvent(e);
if(e.getID()==WindowEvent.WINDOW_CLOSING){
System.exit(0);
}
}

ActionListener listener = new ActionListener(){
public void actionPerformed(ActionEvent e) {   //事件按扭处理代码,发送数据
if(e.getActionCommand()=="连接")
button1_Performed();
if(e.getActionCommand()=="发送")
button2_Performed();
}

private void button1_Performed(){
try{
s = new Socket(InetAddress.getByName(textField1.getText()),Integer.parseInt(textField2.getText()));//Socket(InetAddress address, int port) 
OutputStream os = s.getOutputStream();
dos = new DataOutputStream(os);
InputStream is = s.getInputStream();
dis = new DataInputStream(is);
textArea1.append("连接成功!\n");
//bConnected = true;
}catch(UnknownHostException e){
textArea1.append("连接失败");
}catch(IOException e){
textArea1.append("无法连接");
}
}

private void button2_Performed() {
String str = textField1.getText();   //获取文本框数据 
if(str.compareTo("")!=0){  //如果为空
try {
textArea1.append("\nto " + str +":  "+ textField3.getText());  //将给定文本(待发信息)追加到文本区(TextArea)的当前文本。
String s1 = textField3.getText();
textField3.setText(""); 
dos.writeUTF(s1);
} catch (IOException exc) {
textArea1.append(exc.toString()+"\n");
exc.printStackTrace();
}
}
else
textArea1.setText("please input IP first!");
}

};


public static void main(String[] args) {
new ChatClient();
}
}服务端
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;public class ChatServer extends JFrame{
JPanel contentPane;
Label label = new Label();
TextField textField = new TextField();
TextArea textArea = new TextArea();
Button button = new Button("发送");
boolean started = false;
ServerSocket ss = null;
Socket s = new Socket();
InputStream is = null;
OutputStream os = null;
DataOutputStream dos = null;
DataInputStream dis = null;
String str = null;

public ChatServer(){
contentPane = (JPanel) this.getContentPane();
label.setText("输入信息:");
label.setBounds(new Rectangle(10,19,120,21));
textField.setBounds(new Rectangle(135,23,134,19));
textArea.setBounds(new Rectangle(11,159,333,135));
button.setBounds(new Rectangle(280,55,59,21));
button.addActionListener(listener);
contentPane.add(label);
contentPane.add(textField);
contentPane.add(textArea);
contentPane.add(button);
this.setSize(new Dimension(363,375));
this.setLocation(300,200);
this.setTitle("网络聊天服务端");
this.setLayout(new BorderLayout());
this.setVisible(true);
init();
readMSG();
}

protected void processWindowEvent(WindowEvent e){
super.processWindowEvent(e);
if(e.getID()==WindowEvent.WINDOW_CLOSING){
System.exit(0);
}
}

ActionListener listener = new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getActionCommand()=="发送"){
String str = textField.getText();   //获取文本框数据 
if(str.compareTo("")!=0){  //如果为空
try {
dos.writeUTF(str);
textArea.append("已经发送给客户端信息:"+str+"\n");
textField.setText(""); 

} catch (IOException exc) {
textArea.append(exc.toString()+"\n");
exc.printStackTrace();
}
}
}
}
};
public void init(){
try {
ServerSocket ss = new ServerSocket(7777);
s = ss.accept();
//textArea.append("有一个客户连接"+"\n");
os = s.getOutputStream();
dos = new DataOutputStream(os);
is = s.getInputStream();
dis = new DataInputStream(is);
textArea.append("有一个客户已连上"+"\n");
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}

public void readMSG(){
try {
while(true){
String string = dis.readUTF();
textArea.append("客户说:"+string+"\n");
if(string.equals("88")){
dos.close();
os.close();
dis.close();
is.close();
s.close();
ss.close();
break;
}
}
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}

public static void main(String[] args) {
new ChatServer();

}
}