⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 java写的聊天工具源码.txt

📁 可以在局域网中实现两计算机间的通讯
💻 TXT
📖 第 1 页 / 共 2 页
字号:
XYLayout xYLayout1 = new XYLayout();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
JButton send = new JButton();
JButton exit = new JButton();
JButton link = new JButton();
BorderLayout borderLayout1 = new BorderLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JPanel jPanel3 = new JPanel();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
PaneLayout paneLayout1 = new PaneLayout();
List list1 = new List();
BorderLayout borderLayout2 = new BorderLayout();
JScrollPane jScrollPane2 = new JScrollPane();
JPanel jPanel4 = new JPanel();
JPanel jPanel5 = new JPanel();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel jLabel6 = new JLabel();
PaneLayout paneLayout2 = new PaneLayout();
JTextArea jTextArea1 = new JTextArea();

//定义变量
ServerSocket serverSocket = null; //建立服务器接口
Socket socket = null; //存储连接套字节
//BufferedReader cin = null; //接收客户端信息流
//PrintWriter cout = null; //实现向客户端发送信息流
Vector clients = new Vector(20);

//向亮数组存储客户连接变量
// int active_connects = 0; //记录当前连接人数

//Construct the frame
public SreverFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}

//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(410, 333));
this.setTitle("Chat System Server");
jTextField2.setSelectionStart(11);
jTextField2.setText("");
send.setFont(new java.awt.Font("Serif", 0, 12));
send.setForeground(new Color(7, 0, 255));
send.setText("send");
send.addActionListener(new SreverFrame_send_actionAdapter(this));
exit.setFont(new java.awt.Font("Serif", 0, 12));
exit.setForeground(new Color(7, 0, 255));
exit.setText("exit");
exit.addActionListener(new SreverFrame_exit_actionAdapter(this));
link.setFont(new java.awt.Font("Serif", 0, 12));
link.setForeground(new Color(7, 0, 255));
link.setText("link");
link.addActionListener(new SreverFrame_link_actionAdapter(this));
jPanel1.setLayout(borderLayout1);
jLabel1.setFont(new java.awt.Font("Serif", 0, 12));
jLabel1.setForeground(new Color(7, 0, 255));
jLabel1.setText("user name");
jLabel2.setFont(new java.awt.Font("Serif", 0, 12));
jLabel2.setForeground(new Color(7, 0, 255));
jLabel2.setRequestFocusEnabled(true);
jLabel2.setText("IP Address");
jLabel3.setFont(new java.awt.Font("Serif", 0, 12));
jLabel3.setForeground(new Color(7, 0, 255));
jLabel3.setText("state of link");
jPanel3.setLayout(paneLayout1);
jPanel2.setLayout(borderLayout2);
jLabel4.setFont(new java.awt.Font("Serif", 0, 12));
jLabel4.setForeground(new Color(7, 0, 255));
jLabel4.setText("information type");
jLabel5.setFont(new java.awt.Font("Serif", 0, 12));
jLabel5.setForeground(new Color(7, 0, 255));
jLabel5.setText("information source");
jLabel6.setFont(new java.awt.Font("Serif", 0, 12));
jLabel6.setForeground(new Color(7, 0, 255));
jLabel6.setText("informaition content");
jPanel4.setLayout(paneLayout2);
jTextArea1.setText("");
jTextArea1.setTabSize(8);


jTextField1.setEditable(false);
jTextField1.setText("");
contentPane.add(jPanel1, new XYConstraints(3, 3, 395, 120));
jPanel1.add(jScrollPane1, BorderLayout.CENTER);
jScrollPane1.getViewport().add(list1, null);
jPanel1.add(jPanel3, BorderLayout.NORTH);
contentPane.add(jPanel2, new XYConstraints(1, 127, 398, 108));
jPanel2.add(jScrollPane2, BorderLayout.CENTER);
jScrollPane2.getViewport().add(jTextArea1, null);
jPanel2.add(jPanel4, BorderLayout.NORTH);
jPanel2.add(jPanel5, BorderLayout.SOUTH);
contentPane.add(send, new XYConstraints(335, 238, -1, -1));
contentPane.add(exit, new XYConstraints(335, 266, 58, -1));
contentPane.add(link, new XYConstraints(261, 266, 58, -1));
contentPane.add(jTextField2, new XYConstraints(1, 239, 318, -1));
contentPane.add(jTextField1, new XYConstraints(2, 268, 233, 24));
jPanel4.add(jLabel6,
new PaneConstraints("jLabel6", "jLabel6", PaneConstraints.ROOT,
0.5f));
jPanel4.add(jLabel4,
new PaneConstraints("jLabel4", "jLabel6", PaneConstraints.LEFT,
0.6482412f));
jPanel4.add(jLabel5,
new PaneConstraints("jLabel5", "jLabel4", PaneConstraints.RIGHT,
0.5232558f));
jPanel3.add(jLabel1, new PaneConstraints("jLabel1", "jLabel1", PaneConstraints.ROOT, 0.5f));
jPanel3.add(jLabel2, new PaneConstraints("jLabel2", "jLabel1", PaneConstraints.RIGHT, 0.6835443f));
jPanel3.add(jLabel3, new PaneConstraints("jLabel3", "jLabel2", PaneConstraints.RIGHT, 0.5244755f));
}

//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
sendClients(new StringBuffer("QUIT"+"SERVER"));
closeAll();
System.exit(0);
}
}

public class Client
extends Thread {
String name;
String ip;
DataInputStream dis;
DataOutputStream ps;
Socket socket;
public Client(Socket s) {
socket = s;
try {
dis = new DataInputStream(s.getInputStream()); //储存特定用户的SOCKET输出流,接收S客户发送到服务器的信息
ps = new DataOutputStream(s.getOutputStream()); //存储特定用户SOCKET的输出流,发送服务器给S客户的信息
String infor = dis.readUTF(); // read information

StringTokenizer stinfo = new StringTokenizer(infor, ":"); //读取用“:”隔开的的分段字符
String head = stinfo.nextToken(); //用HEAD存储关键字的头信息

if (stinfo.hasMoreTokens()) {
name = stinfo.nextToken(); //关键字的第2个数据段是客户名称
}
if (stinfo.hasMoreTokens()) {
ip = stinfo.nextToken(); //第3个数据段是客户IP地址
}

}
catch (IOException ex) {
jTextArea1.append("系统消息: 服务器错误 " + "Errro " +ex);
}

}

public void send(String msg) {
try {
ps.writeUTF(msg.toString());
ps.flush();
}
catch (IOException ex) {
ex.printStackTrace();
}
}

public void run() {
while (true) {
String line = null;
try {
line = dis.readUTF();
}
catch (IOException ex) {
jTextArea1.append("系统信息:" + " 服务器" + "Error: " + ex);
disconnect(this);
notifyRoom();
return;
}
if (line == null) {
disconnect(this);
notifyRoom();
return;
}
StringTokenizer st = new StringTokenizer(line, ":");
String keyword = st.nextToken();
String broadcast = st.nextToken();

if (keyword.equalsIgnoreCase("MSG")) {

StringBuffer message = new StringBuffer("MSG:");
message.append(broadcast + ":");
message.append(st.nextToken(""));

if (broadcast.equalsIgnoreCase("BROAD")) {
sendClients(message);
}
else {
sendtoClient(message, broadcast);
}
}
else if (keyword.equalsIgnoreCase("QUIT")) {
list1.removeAll();
disconnect(this);
for (int i = 0; i < clients.size(); i++) {
Client listdata1 = (Client) clients.elementAt(i);
list1.add(listdata1.name + " " + listdata1.ip +
" 已连接", i);
}/////////////////////////////////////////////
notifyRoom();
this.stop();
}
}
}
}

public synchronized void sendClients(StringBuffer msg) {
for (int i = 0; i < clients.size(); i++) {
Client c = (Client) clients.elementAt(i);
c.send(msg.toString());
}
}

public void sendtoClient(StringBuffer msg, String name) {
for (int i = 0; i < clients.size(); i++) {
Client c = (Client) clients.elementAt(i);
if (c.name.equalsIgnoreCase(name)) {
c.send(msg.toString());
break;
}
}
}

public void disconnect(Client c) {
try {
jTextArea1.append("系统消息: " + c.name + " 断开连接n");
clients.removeElement(c);
sendClients(new StringBuffer("QUIT" + ":" + c.name));

c.socket.close();
}
catch (IOException ex) {
jTextArea1.append("系统消息: " + "服务器" + " Eroor: " + ex);
}
finally {
jTextField1.setText("用户过多,请稍后在试");
}
}

public void notifyRoom() {
if (clients.size() > 0) {
StringBuffer people = new StringBuffer("PEOPLE");
for (int i = 0; i < clients.size(); i++) {
Client c = (Client) clients.elementAt(i);
people.append(":" + c.name);
}
sendClients(people);
}
}
class watch
extends Thread {
public void run() {

while (true) {
if (clients.size() < 20) {
try {
socket = serverSocket.accept();
}
catch (IOException ex) {
jTextArea1.append("系统消息 服务器 " + ex.toString() + " 用户连接失败!n");
}
if (socket != null) {
Client c = new Client(socket);
if (checkName(c)) {
clients.addElement(c);
list1.add(c.name + " " + c.ip + " 已经连接",
clients.size());
jTextArea1.append("系统消息: " + c.name + "/" + c.ip +
"已经连接n");
jTextField1.setText("目前有" + clients.size() + "用户连接");
c.start(); //启动线程
c.send("welcome");
notifyRoom(); //监视聊天窗口的连接情况
}
else {
try {
c.send("FaultName");
c.socket.close();
}
catch (IOException ex1) {
ex1.printStackTrace();
}
}
}
}
else {
try {
Thread.sleep(400);
}
catch (InterruptedException ex2) {
}
}

}

}
}
public boolean checkName(Client newClient) {
for (int i = 0; i < clients.size(); i++) {
Client c = (Client) clients.elementAt(i);
if ( c.name.equalsIgnoreCase(newClient.name)) {
return false;
}
}
return true;
}

void link_actionPerformed(ActionEvent e) {
//初始化连接

if (serverSocket == null) {

try {
serverSocket = new ServerSocket(4000);
//建立一个服务器,用4000端口
jTextArea1.append("系统信息: 服务器 "+" "+"正在启动......n");
}
catch (IOException ex) {
jTextArea1.append("系统信息: 服务器 " +" "+"启动出错n");
}
}
if (serverSocket != null) {
jTextArea1.append("系统消息: 服务器 已经启动.n");
Thread a = new watch();
a.start();
}

}


public synchronized void closeAll(){
while(clients.size()>0)
{
Client c=(Client)clients.firstElement();
try {
c.socket.close();
}
catch (IOException ex) {
jTextArea1.append("系统消息:"+" 服务器"+" Error "+ex);
}
finally
{
clients.removeElement(c);
}
}
}
void send_actionPerformed(ActionEvent e) {

StringBuffer info=new StringBuffer("MSG");
info.append(":"+"BROAD:"+"SERVER"+":");
info.append(jTextField2.getText());
sendClients(info);
jTextField2.setText("");
}

void exit_actionPerformed(ActionEvent e) {
sendClients(new StringBuffer("QUIT:"+"SERVER"));
closeAll();
System.exit(0);
}

}

class SreverFrame_link_actionAdapter

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -