一、概述
1.目标:建立基于tcp协议的聊天系统
2.思路:用java socket编程
二、代码
1.ChatServer.java
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException; public class ChatServer{ public static void main(String[] args) { DataInputStream dis = null;
Socket s = null;
ServerSocket ss = null;
boolean started =false; try {
ss = new ServerSocket(8888);
} catch (IOException e1) {
e1.printStackTrace();
} try {
started = true;
while(started){
boolean bConnect = false;
//!!!!!注意accept()是阻塞性方法,当被readUTF()阻塞时它不会被执行
s = ss.accept();
System.out.println("a client connect------"+s);
dis = new DataInputStream(s.getInputStream());
bConnect = true;
while(bConnect){
//!!!!!注意readUTF()是阻塞性方法
System.out.println(dis.readUTF());
}
}
} catch (EOFException e) {
System.out.println("客户端已关闭!");
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(dis != null ) dis.close();
if(s != null ) s.close();
if(ss != null ) ss.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
2.Chatclient.java
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException; public class ChatClient extends Frame{ private TextField tfText;
private TextArea taContent;
// private Button btnSend;
private Socket socket;
private DataOutputStream dos; public void launchFrame(){ addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
disconnect();
System.exit(0);
}
}); tfText = new TextField();
taContent = new TextArea();
// btnSend = new Button("发送");
//
// btnSend.addActionListener(new ActionListener() {
// @Override
// public void actionPerformed(ActionEvent e) {
// //taContent.setText(taContent.getText()+"\n\r"+tfText.getText());
// //tfText.setText("");
// try {
// DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
// dos.writeUTF(tfText.getText());
// dos.close();
// socket.close();
// } catch (IOException e1) {
// e1.printStackTrace();
// }
// }
// }); tfText.addActionListener(new TFListener()); add(taContent,BorderLayout.NORTH);
add(tfText,BorderLayout.CENTER);
// add(btnSend,BorderLayout.SOUTH);
setLocation(300, 100);
pack();
setVisible(true);
connect("localhost", 8888);
} //当调用了此方法,会自动把成员变量给socket连接上server
public void connect(String address, int port){
try {
socket = new Socket(address, port);
dos = new DataOutputStream(socket.getOutputStream());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} //退出时释放资源
public void disconnect(){
try {
if(dos != null ) dos.close();
if(socket != null ) socket.close();
} catch (IOException e) {
e.printStackTrace();
}
} class TFListener implements ActionListener{ @Override
public void actionPerformed(ActionEvent e) {
taContent.setText(taContent.getText().trim()+"\n\r"+tfText.getText()); try {
dos.writeUTF(tfText.getText());
dos.flush();
tfText.setText("");
//dos.close();
//socket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
} public static void main(String[] args) {
new ChatClient().launchFrame();
}
}
三、运行结果