import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.Vector;
public class Server extends JFrame {
JTextArea area = new JTextArea(10,20);
JTextField field = new JTextField(20);
JButton button = new JButton("发送");
JButton button2 =new JButton("启动");
public JComboBox box = new JComboBox();
JLabel dui = new JLabel("对");
JLabel shuo = new JLabel("说");
Vector<connection> connections = new Vector<connection>();
public Server() {
this.setTitle("Server");
this.setSize(240,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
Container c = this.getContentPane();
c.setLayout(new FlowLayout());
area.setEditable(false);
JScrollPane scroll = new JScrollPane(area);
c.add(scroll);
c.add(field);
c.add(dui);
c.add(box);
c.add(shuo);
c.add(button);
c.add(button2);
box.addItem("大家");
// box.addItem(connections);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String b = field.getText();
area.append("系统消息:" + b + "\n");
for(connection i : connections) {
i.button("系统消息:" + b);
}
field.setText("");
field.requestFocus();
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
But r = new But();
r.start();
}
});
}
class But extends Thread {
public void run() {
try {
ServerSocket server = new ServerSocket(1111);
area.append("等待建立新连接……\n");
while(true) {
Socket s = server.accept();
area.append("新客户端连接成功!\n");
connection con = new connection(s,Server.this);
connections.add(con);
}
} catch (IOException e) {
JOptionPane.showMessageDialog(null,"此端口已存在!");
}
}
}
class connection {
BufferedReader in = null;
BufferedWriter out = null;
String name = null;
Server server = null;
Socket s = null;
public connection(Socket s,Server server) {
this.s = s;
this.server = server;
try {
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
name = in.readLine();
box.addItem(name);
out.write("欢迎" + name + "!\n");
out.flush();
Read read = new Read();
read.start();
} catch (IOException e) {
e.printStackTrace();
}
}
public void button(String s) {
try {
out.write(s + "\n");
out.flush();
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "向客户端发送失败!");
}
}
class Read extends Thread {
public void run() {
while(true) {
try {
String s = in.readLine();
// String[] temp = s.split(":");
// String from = temp[0];
// String to = temp[1];
// String content = temp[2];
// area.append(from + "对" + to + "说:" + content);
area.append(s + "\n");
} catch (IOException e) {
JOptionPane.showMessageDialog(null,"网络错误,读取数据失败!");
}
}
}
}
}
public static void main(String[] args) {
new Server().setVisible(true);
}
}
用java实现一个简单的QQ小程序
4星 · 超过85%的资源 需积分: 35 159 浏览量
2008-11-20
16:31:22
上传
评论 1
收藏 3KB RAR 举报
pichengzhang414
- 粉丝: 0
- 资源: 1
最新资源
- 2023-04-06-项目笔记 - 第一百十五阶段 - 4.4.2.113全局变量的作用域-113 -2024.04.26
- 2023-04-06-项目笔记 - 第一百十五阶段 - 4.4.2.113全局变量的作用域-113 -2024.04.26
- htmlzwbjq_downyi.com.zip
- 无头单向非循环链表的实现(Test.c)
- 无头单向非循环链表的实现(SList.c)
- 浏览器重定向插件更新文件
- SSA-BP麻雀算法优化BP神经网络多特征分类预测(Matlab实现完整源码和数据)
- 粒子群算法优化BP神经网络PSO-BP的MATLAB代码(数值预测)
- 基于Springboot的一起看书平台.zip
- 无头单向非循环链表的实现(SList.h)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈