收藏 分销(赏)

实验七:综合性实验.doc

上传人:pc****0 文档编号:9213332 上传时间:2025-03-17 格式:DOC 页数:9 大小:84.50KB
下载 相关 举报
实验七:综合性实验.doc_第1页
第1页 / 共9页
实验七:综合性实验.doc_第2页
第2页 / 共9页
点击查看更多>>
资源描述
实验七:综合性实验 班级:计科F1202 姓名: 肖明亮 学号: 201216010119 实验目的:通过一个网络聊天程序设计,综合应用多种Java API,使学生掌握设计一个综合性的应用程序的方法。 实验内容:GUI设计;数据库操作实现;网络通信机制设计。 实验步骤: import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import .Socket; import java.util.List; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; /*这个类是服务器端的UI*/ public class ServerUI extends JFrame { public static void main(String[] args) { ServerUI serverUI = new ServerUI(); } public JButton btStart;//启动服务器 public JButton btSend;//发送信息按钮 public JTextField tfSend;//需要发送的文本信息 public JTextArea taShow;//信息展示 public Server server;//用来监听客户端连接 static List<Socket> clients;//保存连接到服务器的客户端 public ServerUI() { super("服务器端"); btStart = new JButton("启动服务"); btSend = new JButton("发送信息"); tfSend = new JTextField(10); taShow = new JTextArea(); btStart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { server = new Server(ServerUI.this); } }); btSend.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { server.sendMsg(tfSend.getText()); tfSend.setText(""); } }); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { int a = JOptionPane.showConfirmDialog(null, "确定关闭吗?", "温馨提示", JOptionPane.YES_NO_OPTION); if (a == 1) { server.closeServer(); System.exit(0); // 关闭 } } }); JPanel top = new JPanel(new FlowLayout()); top.add(tfSend); top.add(btSend); top.add(btStart); this.add(top, BorderLayout.SOUTH); final JScrollPane sp = new JScrollPane(); sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); sp.setViewportView(this.taShow); this.taShow.setEditable(false); this.add(sp, BorderLayout.CENTER); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(400, 300); this.setLocation(100, 200); this.setVisible(true); } } import java.io.BufferedReader;import java.io.IOException;import java.io.PrintWriter;import .ServerSocket;import .Socket;import java.util.ArrayList;/*这个类是服务器端的等待客户端连接*/public class Server extends Thread { ServerUI ui; ServerSocket ss; BufferedReader reader; PrintWriter writer; public Server(ServerUI ui) { this.ui = ui; this.start(); } public void run() { try { ss = new ServerSocket(1228); ui.clients=new ArrayList<Socket>(); println("启动服务器成功:端口1228"); while (true) { println("等待客户端"); Socket client = ss.accept(); ui.clients.add(client); println("连接成功" + client.toString()); new ListenerClient(ui, client); } } catch (IOException e) { println("启动服务器失败:端口1228"); println(e.toString()); e.printStackTrace(); } } public synchronized void sendMsg(String msg) { try { for (int i = 0; i < ui.clients.size(); i++) { Socket client = ui.clients.get(i); writer = new PrintWriter(client.getOutputStream(), true); writer.println(msg); } } catch (Exception e) { println(e.toString()); } } public void println(String s) { if (s != null) { this.ui.taShow.setText(this.ui.taShow.getText() + s + "\n"); System.out.println(s + "\n"); } } public void closeServer() { try { if (ss != null) ss.close(); if (reader != null) reader.close(); if (writer != null) writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import .Socket;/*这个类是服务器端的等待客户端发送信息*/public class ListenerClient extends Thread { BufferedReader reader; PrintWriter writer; ServerUI ui; Socket client; public ListenerClient(ServerUI ui, Socket client) { this.ui = ui; this.client=client; this.start(); } //为每一个客户端创建线程等待接收信息,然后把信息广播出去 public void run() { String msg = ""; while (true) { try { reader = new BufferedReader(new InputStreamReader( client.getInputStream())); writer = new PrintWriter(client.getOutputStream(), true); msg = reader.readLine(); sendMsg(msg); } catch (IOException e) { println(e.toString()); // e.printStackTrace(); break; } if (msg != null && msg.trim() != "") { println(">>" + msg); } } } //把信息广播到所有用户 public synchronized void sendMsg(String msg) { try { for (int i = 0; i < ui.clients.size(); i++) { Socket client = ui.clients.get(i); writer = new PrintWriter(client.getOutputStream(), true); writer.println(msg); } } catch (Exception e) { println(e.toString()); } } public void println(String s) { if (s != null) { this.ui.taShow.setText(this.ui.taShow.getText() + s + "\n"); System.out.println(s + "\n"); } } } 客户端源代码: import java.awt.BorderLayout;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent; import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField; public class ClientUI extends JFrame { public static void main(String[] args) { ClientUI client = new ClientUI(); } public ClientUI() { super("客户端"); btStart = new JButton("启动连接"); btSend = new JButton("发送信息"); tfSend = new JTextField(10); tfIP = new JTextField(10); tfPost = new JTextField(5); taShow = new JTextArea(); btStart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { server = new ClientThread(ClientUI.this); } }); btSend.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { server.sendMsg(tfSend.getText()); tfSend.setText(""); } }); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { int a = JOptionPane.showConfirmDialog(null, "确定关闭吗?", "温馨提示", JOptionPane.YES_NO_OPTION); if (a == 1) { System.exit(0); // 关闭 } } }); JPanel top = new JPanel(new FlowLayout()); top.add(tfSend); top.add(btSend); top.add(btStart); this.add(top, BorderLayout.SOUTH); final JScrollPane sp = new JScrollPane(); sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); sp.setViewportView(this.taShow); this.taShow.setEditable(false); this.add(sp, BorderLayout.CENTER); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(400, 300); this.setLocation(600, 200); this.setVisible(true); } public JButton btStart; public JButton btSend; public JTextField tfSend; public JTextField tfIP; public JTextField tfPost; public JTextArea taShow; public ClientThread server; } import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import .Socket; public class ClientThread extends Thread { ClientUI ui; Socket client; BufferedReader reader; PrintWriter writer; public ClientThread(ClientUI ui) { this.ui = ui; try { client = new Socket("127.0.0.1", 1228);//这里设置连接服务器端的IP的端口 println("连接服务器成功:端口1228"); reader = new BufferedReader(new InputStreamReader( client.getInputStream())); writer = new PrintWriter(client.getOutputStream(), true); // 如果为 true,则 println、printf 或 format 方法将刷新输出缓冲区 } catch (IOException e) { println("连接服务器失败:端口1228"); println(e.toString()); e.printStackTrace(); } this.start(); } public void run() { String msg = ""; while (true) { try { msg = reader.readLine(); } catch (IOException e) { println("服务器断开连接"); break; } if (msg != null && msg.trim() != "") { println(">>" + msg); } } } public void sendMsg(String msg) { try { writer.println(msg); } catch (Exception e) { println(e.toString()); } } public void println(String s) { if (s != null) { this.ui.taShow.setText(this.ui.taShow.getText() + s + "\n"); System.out.println(s + "\n"); } } } 实验结果:
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 百科休闲 > 社会民生

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服