当前位置:
文档之家› JAVA 简单聊天程序设计模式
JAVA 简单聊天程序设计模式
public static void main(String[] args) { new ChatClient().launchFrame();
}
public void launchFrame() { //创建客户端窗口 setSize(300, 300); setLocation(400, 300); add(tfTxt, BorderLayout.SOUTH); add(taContent, BorderLayout.NORTH); pack(); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { disconnect(); System.exit(0); } }); tfTxt.addActionListener(new TFMonitor()); setVisible(true); connect(); //调用连接方法 new Thread(new RecvThread()).start();//启动线程
4.项目介绍: 程序有两个部分完成:客户端和服务端。
由服务端开启服务,然后打开两个或者两个以上客户端,在客
户端输入信息,按 enter 键发送到服务端,然后再由服务端同时发 送到几个客户端,即实现了客户端之间的简单对话。
5.源代码及注解:
服务端:
import java.io.*; import .*; import java.util.*;
} catch (IOException e) {
} finally {
//使用完毕后关闭个端口
try {
if (s != null)
s.close();
if (dis != null)
dis.close();
if (dos != null)
dos.close();
} catch (IOException e) {
public Client(Socket s) { this.s = s; try { dis = new DataInputStream(s.getInputStream()); dos = new DataOutputStream(s.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } bConnected = true;
//引入相关的包
public class ChatServer {
ServerSocket ss = null; Boolean started = false;
//定义服务端端口
public static void main(String[] args) { new ChatServer().start();
public void run() { try { while (bConnect) { String str = dis.readUTF(); taContent.setText(taContent.getText() + str +
'\n'); }
} catch (SocketException e) { System.out.println("disconnec!");
} catch (IOException e) { e.printStackTrace();
} } }
class Client implements Runnable { //实现 Runnable 接口
private Socket s = null; private DataInputStream dis = null; private Boolean bConnected = false; private DataOutputStream dos = null;
} catch (BindException e) { //如果已开启服务捕捉错误
System.out.println("端口使用中...");
System.out.println("请关闭相应程序并重新运行!");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
public void send(String str) { //发送信息 try { dos.writeUTF(str); } catch (IOException e) { Clients.remove(this); System.out.println("一个用户出了,我把他从 list 里
dis = new DataInputStream(s.getInputStream());
} catch (UnknowБайду номын сангаасHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void disconnect() { //断开连接 try { dos.close(); s.close(); } catch (IOException e) { e.printStackTrace(); }
} catch (EOFException e) { System.out.println("退出了,bye - bye!");
} catch (IOException e) { e.printStackTrace();
}
} } }
}
public void connect() {
//建立连接的方法
try {
s = new Socket("127.0.0.1", 8888);
System.out.println("connect!");
bConnect = true;
dos = new DataOutputStream(s.getOutputStream());
e.printStackTrace();
}
}
}
}
}
客户端:
import java.awt.*; import java.awt.event.*; import java.io.*; import .*;
//引入相关包
public class ChatClient extends Frame {
}
while (started) { //连接开始,有一个客户连接并且添加 到容器内
try { Socket s = ss.accept(); Client c = new Client(s); System.out.println("a client connected!"); new Thread(c).start(); //启动线程 Clients.add(c);
try { dos.writeUTF(str); dos.flush(); // dos.close();
} catch (IOException e1) { e1.printStackTrace();
} }
}
private class RecvThread implements Runnable { //建立接口, 实现线程接受信息
}
private class TFMonitor implements ActionListener {//实现发 送的接口,并且对发送实现监听
public void actionPerformed(ActionEvent e) { String str = tfTxt.getText().trim(); // taContent.setText(str); tfTxt.setText("");
基于 java 开发的聊天程序
1.项目名称: 聊天程序
2.项目题外话: 这个聊天的小程序是基于 java 语言开发的,是在看马士兵老师
的视频带领下,独立完成的,虽然和马老师的出入不大,但是都是
自己一个字母一个字母敲出来的。
3.项目功能: 在本机一台上实现,能够同时开启几个(用户)窗口进行对话,
并且同时显示在几个(用户)窗口上,实现聊天功能。
}
List<Client> Clients = new ArrayList<Client>(); //为开启的几 个几个客户端创建一个容器
public void start() {
//定义服务端开启
try {
ss = new ServerSocket(8888);
started = true;
//建立窗口后 started 变为 true
Socket s = null; TextField tfTxt = new TextField(); TextArea taContent = new TextArea(); DataOutputStream dos = null; DataInputStream dis = null; private Boolean bConnect = false;
面移除了!"); //e.printStackTrace();
} }
public void run() { 端信息
//读取信息,并且发送给每个客户
try {
while (bConnected) {
String str = dis.readUTF();
System.out.println(str);
for (int i = 0; i < Clients.size(); i++) {