Socket 编程基础 - 航班显示系统客户端

更新时间:2023-10-21 17:50:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

《网络编程技术》实验报告

实验三 Socket 编程基础——航班显示系统客户端的设计与实现

一. 实验目的

1、 理解基于 TCP 的网络连接、套接字的基本概念 2、 熟悉并熟练掌握 Java 中 Socket 类的使用

3、 熟练掌握基于 TCP 的网络客户端的数据读写操作和客户端软件设计

二. 实验环境

1、 准备 SUN 的 JDK6 安装文件

2、 Java 程序开发软件一套——MyEclipse

三. 实验实际完成内容及结果分析(请先说明你在小组中所承担的任务)

import java.awt.EventQueue;

import javax.swing.JFrame; import javax.swing.JPanel;

import javax.swing.border.EmptyBorder; import javax.swing.table.DefaultTableModel; import javax.swing.JTable; import javax.swing.JButton; import javax.swing.JScrollPane;

import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket;

import java.net.UnknownHostException; import java.io.FileReader; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Enumeration; import java.util.LinkedList; import java.util.Hashtable; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.awt.BorderLayout; import java.awt.GridBagLayout;

《网络编程技术》实验报告

import java.awt.GridBagConstraints; import java.awt.Insets;

public class FlyFrame1 extends JFrame {

/**

* Create the frame. *

* @throws IOException */

public FlyFrame1() throws IOException {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 800, 400); contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); /**

* Launch the application. */

public static void main(String[] args) { }

EventQueue.invokeLater(new Runnable() { });

public void run() { }

try { }

FlyFrame1 frame = new FlyFrame1(); frame.setVisible(true); e.printStackTrace();

private JPanel contentPane; private JTable flyTable;

private static LinkedList processList = new LinkedList(); // 处理private static LinkedList showList = new LinkedList(); // 显示数private static Hashtable citycode = new Hashtable(); private static Hashtable tableindex = new Hashtable

数据列表(读入线程和处理线程共享资源) 据列表(处理线程和显示线程共享资源) // 城市代码

Integer>(); // 基于flid的表索引

} catch (Exception e) {

《网络编程技术》实验报告

0);

// 连接按钮动作

// 创建连接按钮

JButton connectButton = new JButton(\);

GridBagConstraints gbc_connectButton = new GridBagConstraints(); gbc_connectButton.insets = new Insets(3, 0, 0, 0); gbc_connectButton.ipadx = 50;

gbc_connectButton.fill = GridBagConstraints.VERTICAL; gbc_connectButton.gridx = 0; gbc_connectButton.gridy = 0;

buttonPanel.add(connectButton, gbc_connectButton);

JPanel buttonPanel = new JPanel();

contentPane.add(buttonPanel, BorderLayout.SOUTH); GridBagLayout gbl_buttonPanel = new GridBagLayout(); gbl_buttonPanel.columnWidths = new int[] { 772, 0 }; gbl_buttonPanel.rowHeights = new int[] { 27, 0 };

gbl_buttonPanel.columnWeights = new double[] { 0.0, Double.MIN_VALUE }; gbl_buttonPanel.rowWeights = new double[] { 0.0, Double.MIN_VALUE }; buttonPanel.setLayout(gbl_buttonPanel);

flyTable = new JTable(flyTableModel);

flyTableScrollPane.setViewportView(flyTable);

// 创建表

final DefaultTableModel flyTableModel = new DefaultTableModel(

new String[] { \航班号\, \前往\, \办票时间\, \预计起飞时间\, \登机口\ },

// 创建滚动条

JScrollPane flyTableScrollPane = new JScrollPane(); contentPane.add(flyTableScrollPane, BorderLayout.CENTER);

// 创建城市代码映射表

BufferedReader cityReader = new BufferedReader(new FileReader(

\));

String cityString = null;

while ((cityString = cityReader.readLine()) != null) { }

cityReader.close();

contentPane.setLayout(new BorderLayout(0, 0));

String[] aircodeArray = cityString.split(\); // citycode.put(aircodeArray[0], aircodeArray[2]); citycode.put(aircodeArray[0], aircodeArray[1]);

《网络编程技术》实验报告

connectButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

// 完成耗时的动作时必须开新线程,否则会导致更新出错 // 读入线程 new Thread() {

@Override

public void run() {

// TODO 自动生成的方法存根 try {

// 创建socket

Socket socket = new Socket(\, 9999); // 读入数据 try { }

BufferedReader flyReader = new BufferedReader(

new InputStreamReader(socket

.getInputStream()));

String processString = null;

while ((processString = flyReader.readLine()) != \

if (processString

// 共享资源processList同步块 synchronized (processList) {

processList.addLast(processString); processList.notify();

data!\) {

.matches(\)) {

}// end synchronized

}// end if

}// end while stopflog = true;

flyReader.close(); // 关闭流 // TODO 自动生成的 catch 块 e.printStackTrace(); try { }

if (socket != null)

socket.close();

} catch (IOException e) {

} finally {

} catch (IOException e) {

// TODO 自动生成的 catch 块 e.printStackTrace();

} catch (UnknownHostException e) {

《网络编程技术》实验报告

// 更新数据

if (tableindex.get(matcherFild.group(1)) != null) {

// 获取索引

index = tableindex.get(matcherFild.group(1));

// 设置对应flid的数据是否存在标记,默认值为-1 Integer index = -1;

// 记录航班ID

Matcher matcherFild = Pattern.compile(

\) /* 匹配并捕获flid */

.matcher(processString); matcherFild.find();

String processString = null; // 共享资源processList同步块 synchronized (processList) { }

while (processList.isEmpty()) { // 如果处理数据 }

processString = processList.removeFirst(); //

processList.wait();

// 处理线程 new Thread() {

public void run() {

while (true) {

try {

// 处理结束

if (stopflog && processList.isEmpty()) { }

break;

}

// TODO 自动生成的 catch 块 e.printStackTrace(); // TODO 自动生成的 catch 块 e.printStackTrace();

} catch (IOException e) {

};// end run

}.start();// end thread

列表中没有数据,则进入休眠状态

取出处理数据列表的第一项

本文来源:https://www.bwwdw.com/article/qigf.html

Top