




已阅读5页,还剩18页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
课程设计说明书 名称_手持移动设备应用开发课程设计_ 2011年12月27日 至 2011年12月29日 共1 周院 系_计算机工程系_班 级_07计算机应用(对口)_姓 名_ _系主任_ _教研室主任_ _ 指导教师_ _目录一、课程设计目的与要求11.1 设计目的11.2 设计要求11.3 设计内容简介1二、开发环境与系统流程22.1开发平台构建22.1.1环境简介22.1.2安装步骤22.2系统流程图5三、详细设计与分析63.1主界面程序设计63.1.1 SocketMIDlet功能简介63.1.2界面截图63.1.3界面关键代码和描述63.2服务器端界面程序设计73.2.1Server功能简介73.2.2界面截图73.2.3界面关键代码和描述83.3服务器端接收信息程序设计83.3.1功能简介83.3.2界面截图83.3.3界面关键代码和描述93.4客户端主界面设计103.4.1功能简介103.4.2界面截图113.4.3界面关键代码和描述113.5客户端发送/接收信息设计113.5.1功能简介113.5.2界面截图123.5.3界面关键代码和描述12四、课程设计总结14五、参考文献14附录(源程序代码)15一、课程设计目的与要求1.1 设计目的本课程设计课题是为配合手持设备移动应用开发课程教学所开设的实践性环节课程,旨在要求学生综合掌握Eclipse集成开发环境中基于J2ME技术的手持移动设备应用开发的完整过程,特别是掌握用户界面设计、基于Socket网络连接的程序设计方法,进一步熟悉WTK文档的使用,强化学生面向职业应用的综合程序设计能力。1.2 设计要求(一)掌握Eclipse集成开发环境中基于J2ME技术的手持移动设备应用开发的完整开发流程;(二)掌握课程设计课题的工作原理,完成系统分析及相应的程序设计工作,包括:(1)指定服务器端、客户端及通信端口号功能的主界面程序设计;(2)服务器端界面程序设计、服务器端读取客户发送信息程线程设计;(3)客户端界面程序设计、客户端发送/接收信息线程设计。(三)完成课程设计说明书编写。1.3 设计内容简介本课程设计参照WTK文档实现一个基于Socket网络编程协议的智能手机J2ME网络通信应用程序,该系统同时包含服务器和客户端功能,可以根据通信需要启用服务器端功能或客户端功能,可以指定通信端口号,客户端与服务器之间的通信由独立的线程完成。二、开发环境与系统流程2.1开发平台构建2.1.1环境简介Sun公司为了使Java语言进入嵌入式系统和消费类电子产品领域,在Java2中推出了J2ME,并提供了一套完整的J2ME开发工具,可以在PC机上完成J2ME应用程序开发、调试、预校验和运行等工作。使用J2ME的CLDC/MIDP编写的Java程序被称为MIDlet。为了开发MIDlet应用程序,选用 Windows操作系统平台。在此平台上安装相关软件:JDK、SunWTK、Eclipse、EclipseME插件、WTK插件。2.1.2安装步骤EclipseME作为Eclipse一个插件,致力于帮助开发者开发J2ME应用程序。EclipseME并不为开发者提供无线设备模拟器,而将各手机厂商的实用模拟器紧密连接到Eclipse开发环境中,为开发者提供一种无缝统一的集成开发环境。在/downloads/index.php上下载Eclipse。在/technetwork/java/download-135801.html/上下载Wireless Toolkit。安装步骤:l 安装jdk-7u1-windows-i586.exe(设置好系统环境变量)l 安装WTK 2.5正式版sun_java_wireless_toolkit-2.5.2_01-win.exel 解压安装eclipse-SDK-3.7.1-win32.zipl 加载eclipseme.feature_1.7.9_site.zip插件和,具体步骤如下:通过菜单Window-Preferences的J2ME选项中add wireless toolkit整合eclipse和WTK。l 加载WTK插件l 安装Eclipse简体中文语言包2.2系统流程图主界面服务器端 客户端监听某个端口接受客户端连接获得与客户端连接的Socket打开输入流打开输出流通过输出流发送数据通过输入流接收数据连接服务器端口打开输入流打开输出流通过输入流接收数据通过输出流发送数据是否结束是否结束断开连接断开连接结束结束是是否否发送端三、详细设计与分析3.1主界面程序设计3.1.1 SocketMIDlet功能简介主界面定义了相关端口号,类型名与显示功能,指定服务器端、客户端及通信端口号的设计。3.1.2界面截图3.1.3界面关键代码和描述该程序段实现了主界面显示效果的功能,主要程序段如下: public SocketMIDlet() display = Display.getDisplay(this);/获得系统屏幕f = new Form(Socket Demo);/创建界面的标题 cg = new ChoiceGroup(Please select peer, Choice.EXCLUSIVE, names,null);/创建两个单选按钮f.append(cg);portField = new TextField(Port number:, String.valueOf(DEFAULT_PORT),6, TextField.NUMERIC);/创建一个不超过6个字符的文本框对象f.append(portField);f.addCommand(exitCommand);f.addCommand(startCommand);/把命令按钮添加到Form窗口中f.setCommandListener(this);/为当前窗口设置监听器display.setCurrent(f); 3.2服务器端界面程序设计3.2.1Server功能简介 服务器端界面程序设计、服务器端读取客户发送信息程线程设计。3.2.2界面截图3.2.3界面关键代码和描述该程序段实现了服务器端界面显示的功能,主要程序段如下:public Server(SocketMIDlet m, int p) parent = m;port = p;display = Display.getDisplay(parent);f = new Form(Socket Server);si = new StringItem(Status:, );tf = new TextField(Send:, , 30, TextField.ANY);f.append(si);f.append(tf);f.addCommand(exitCommand);f.setCommandListener(this);display.setCurrent(f); 3.3服务器端接收信息程序设计3.3.1功能简介打开两个模拟器,另外一个当作Server端,一个当作Client 端,Client端向Server端发送“1231AD”。3.3.2界面截图3.3.3界面关键代码和描述 该程序段实现了服务器端发送/接收信息的功能,主要程序段如下:public void start() Thread t = new Thread(this); t.start(); /启动线程,防止网络堵塞 public void run() String portString = String.valueOf(port);try si.setText(Waiting for connection on port +portString); scn = (ServerSocketConnection) Connector.open(socket:/: + portString); / 等待连接 sc = (SocketConnection) scn.acceptAndOpen(); si.setText(Connection accepted); is = sc.openInputStream();/打开输入流 os = sc.openOutputStream();/打开输出流 sender = new Sender(os); / Allow sending of messages only after Sender is created f.addCommand(sendCommand); while (true) StringBuffer sb = new StringBuffer();int c = 0;while (c = is.read() != n) & (c != -1) /判断读取的字符 sb.append(char) c);if (c = -1) break;si.setText(Message received - + sb.toString(); stop(); si.setText(Connection is closed); f.removeCommand(sendCommand); catch (IOException ioe) if (ioe.getMessage().equals(ServerSocket Open) Alert a = new Alert(Server, Port + portString+ is already taken., null, AlertType.ERROR);a.setTimeout(Alert.FOREVER);a.setCommandListener(this);display.setCurrent(a); else if (!stop) ioe.printStackTrace(); catch (Exception e) e.printStackTrace(); public void commandAction(Command c, Displayable s) if (c = sendCommand) & !parent.isPaused() sender.send(tf.getString();/发送数据if (c = Alert.DISMISS_COMMAND) | (c = exitCommand) parent.notifyDestroyed(); parent.destroyApp(true);/退出 3.4客户端主界面设计3.4.1功能简介客户端界面程序设计、客户端发送/接收信息线程设计3.4.2界面截图3.4.3界面关键代码和描述该程序段实现了客户端面界面显示的功能,主要程序段如下:public Client(SocketMIDlet m, int p) parent = m;port = p;display = Display.getDisplay(parent);f = new Form(Socket Client);si = new StringItem(Status:, );tf = new TextField(Send:, , 30, TextField.ANY);f.append(si);f.append(tf);f.addCommand(exitCommand);f.addCommand(sendCommand);f.setCommandListener(this);display.setCurrent(f);3.5客户端发送/接收信息设计3.5.1功能简介 服务器端接受到Client 端发送的消息:“1231AD”,并发送一个消息给Client端:“GJ1JMAMJM”3.5.2界面截图3.5.3界面关键代码和描述该程序段实现了客户端发送/接收信息的功能,主要程序段如下:public void start() Thread t = new Thread(this);t.start(); public void run() String portString = String.valueOf(port);try sc = (SocketConnection) Connector.open(socket:/localhost: + portString); si.setText(Connected to server on port +portString); is = sc.openInputStream(); os = sc.openOutputStream(); / Start the thread for sending messages - see Senders main / comment for explanation sender = new Sender(os); / Loop forever, receiving data while (true) StringBuffer sb = new StringBuffer();int c = 0;while (c = is.read() != n) & (c != -1) sb.append(char) c);if (c = -1) break;/ 显示发过来的信息si.setText(Message received - + sb.toString(); stop(); si.setText(Connection closed); f.removeCommand(sendCommand); catch (ConnectionNotFoundException cnfe) Alert a = new Alert(Client, Please run Server MIDlet first on port + portString, null, AlertType.ERROR); a.setTimeout(Alert.FOREVER); a.setCommandListener(this); display.setCurrent(a); catch (IOException ioe) if (!stop) ioe.printStackTrace(); catch (Exception e) e.printStackTrace();四、课程设计总结这次课程设计做的是客户端与服务器信息收发系统,系统分为练习和测试两部分,系统中采用的技术主要利用MyEclipse集成开发环境与WTK文档。socket通信,基本原理就是创建socket,然后连接、监听、接受、发送、关闭等等。在本次实践中,给我印象最为深刻的是通过运行继承于MIDlet的主程序看结果,再来分析程序的界面设计和其中的通信过程,遇到不懂的知识查阅WTK文档,或者从网上查查了很多资料,如ChoiceGroup 类的getSelectedIndex()方法Choice.EXCLUSIVE的意义等。对低级界面的移动应用软件的设计使我更好的掌握Java编程和J2ME程序调试在基本技能。更让我掌握设计移动应用软件的基本思路和方法,提高运用J2ME解决实际问题的能力。通过这次课程设计,我收获的不仅仅是课程上的知识得到实际应用,还有编程的基本习惯和开发系统时应注意的流程。体现出自己单独设计的能力以及综合运用知识的能力,体会了学以致用、突出自己劳动成果的喜悦心情,从中发现自己平时学习的不足和薄弱环节,从而加以弥补。在此感谢我们的贾老师,老师严谨细致、一丝不苟的作风一直是我工作、学习中的榜样;老师循循善诱的教导和不拘一格的思路给予我无尽的启迪;这次课程设计的每个细节和每个数据,都离不开老师您的细心指导。而您开朗的个性和宽容的态度,帮助我能够很顺利的完成了这次课程设计。五、参考文献1 张光兰.基于J2ME的手机日志工作系统的设计和实现.成都信息工程学院,2007.2 高明娟. 基于J2ME手机考试系统的设计与实现.成都信息工程学院,2007.附录(源程序代码)SocketMIDletpackage socket;import javax.microedition.lcdui.Choice;import javax.microedition.lcdui.ChoiceGroup;import javax.microedition.lcdui.Command;import javax.microedition.lcdui.CommandListener;import javax.microedition.lcdui.Display;import javax.microedition.lcdui.Displayable;import javax.microedition.lcdui.Form;import javax.microedition.lcdui.TextField;import javax.microedition.midlet.MIDlet;public class SocketMIDlet extends MIDlet implements CommandListener private static final int DEFAULT_PORT = 5000; private static final String SERVER = Server; private static final String CLIENT = Client; private static final String names = SERVER, CLIENT ; private static Display display; private Form f; private ChoiceGroup cg; private boolean isPaused; private TextField portField; private Server server; private Client client; private Command exitCommand = new Command(Exit, Command.EXIT, 1); private Command startCommand = new Command(Start, Command.ITEM, 1); public SocketMIDlet() display = Display.getDisplay(this);f = new Form(Socket Demo);cg = new ChoiceGroup(Please select peer, Choice.EXCLUSIVE, names,null);f.append(cg);portField = new TextField(Port number:, String.valueOf(DEFAULT_PORT),6, TextField.NUMERIC);f.append(portField);f.addCommand(exitCommand);f.addCommand(startCommand);f.setCommandListener(this);display.setCurrent(f); public boolean isPaused() return isPaused; public void startApp() isPaused = false; public void pauseApp() isPaused = true; public void destroyApp(boolean unconditional) if (server != null) server.stop();if (client != null) client.stop(); public void commandAction(Command c, Displayable s) if (c = exitCommand) destroyApp(true); notifyDestroyed(); else if (c = startCommand) String name = cg.getString(cg.getSelectedIndex(); int port = Integer.parseInt(portField.getString(); if (name.equals(SERVER) server = new Server(this, port);server.start(); else client = new Client(this, port);client.start(); Clientpackage socket;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import javax.microedition.io.ConnectionNotFoundException;import javax.microedition.io.Connector;import javax.microedition.io.SocketConnection;import javax.microedition.lcdui.Alert;import javax.microedition.lcdui.AlertType;import javax.microedition.lcdui.Command;import javax.microedition.lcdui.CommandListener;import javax.microedition.lcdui.Display;import javax.microedition.lcdui.Displayable;import javax.microedition.lcdui.Form;import javax.microedition.lcdui.StringItem;import javax.microedition.lcdui.TextField;public class Client implements Runnable, CommandListener private SocketMIDlet parent; private Display display; private Form f; private StringItem si; private TextField tf; private boolean stop; private Command sendCommand = new Command(Send, Command.ITEM, 1); private Command exitCommand = new Command(Exit, Command.EXIT, 1); InputStream is; OutputStream os; SocketConnection sc; Sender sender; private int port; public Client(SocketMIDlet m, int p) parent = m;port = p;display = Display.getDisplay(parent);f = new Form(Socket Client);si = new StringItem(Status:, );tf = new TextField(Send:, , 30, TextField.ANY);f.append(si);f.append(tf);f.addCommand(exitCommand);f.addCommand(sendCommand);f.setCommandListener(this);display.setCurrent(f); /* * Start the client thread */ public void start() Thread t = new Thread(this);t.start(); public void run() String portString = String.valueOf(port);try sc = (SocketConnection) Connector.open(socket:/localhost: + portString); si.setText(Connected to server on port +portString); is = sc.openInputStream(); os = sc.openOutputStream(); / Start the thread for sending messages - see Senders main / comment for explanation sender = new Sender(os); / Loop forever, receiving data while (true) StringBuffer sb = new StringBuffer();int c = 0;while (c = is.read() != n) & (c != -1) sb.append(char) c);if (c = -1) break;/ Display message to usersi.setText(Message received - + sb.toString(); stop(); si.setText(Connection closed); f.removeCommand(sendCommand); catch (ConnectionNotFoundException cnfe) Alert a = new Alert(Client, Please run Server MIDlet first on port + portString, null, AlertType.ERROR); a.setTimeout(Alert.FOREVER); a.setCommandListener(this); display.setCurrent(a); catch (IOException ioe) if (!stop) ioe.printStackTrace(); catch (Exception e) e.printStackTrace(); public void commandAction(Command c, Displayable s) if (c = sendCommand) & !parent.isPaused() sender.send(tf.getString();if (c = Alert.DISMISS_COMMAND) | (c = exitCommand) parent.notifyDestroyed(); parent.destroyApp(true); /* * Close all open streams */ public void stop() try stop = true; if (sender != null) sender.stop(); if (is != null) is.close(); if (os != null) os.close(); if (sc != null) sc.close(); catch (IOException ioe) Senderpackage socket;import java.io.IOException;import java.io.OutputStream;public class Sender extends Thread private OutputStream os; private String message; public Sender(OutputStream os) this.os = os;start(); public synchronized void send(String msg) message = msg;notify(); public synchronized void run() while (true) / If no client to deal, wait until one connects if (message = null) try wait(); catch (InterruptedException e) if (message = null) break; try os.write(message.getBytes();os.write(rn.getBytes(); catch (IOException ioe) ioe.printStackTrace(); / Completed client handling, return handler to pool and / mark for wait message = null; public synchronized void stop() message = null;notify(); Serverpackage socket;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import javax.microedition.io.Connector;import javax.microedition.io.ServerSocketConnection;import javax.microedition.io.SocketConnection;import javax.microedition.lcdui.Alert;import javax.microedition.lcdui.AlertType;import javax.microedition.lcdui.Command;import javax.microedition.lcdui.CommandListener;import javax.microedition.lcdui.Display;import javax.microedition.lcdui.Displayable;import javax.microedition.lcdui.Form;import javax.microedition.lcdui.StringItem;import javax.microedition.lcdui.TextField;public class Server implements Runnable, CommandListener private SocketMIDlet parent; private Display display; private Form f; private StringItem si; private TextField tf; private boolean stop; private Command sendCommand = new Command(Send, Command.ITEM, 1); private Command exitCommand = new Command(Exit, Command.EXIT, 1); InputStream is; OutputStream os; SocketConnection sc; ServerSocketConnection scn; Sender sender; private int port; public Server(SocketMIDlet m, int p) parent = m;port = p;display = Display.getDisplay(parent);f = new Form(Socket Server);si = new StringItem(Status:, );tf = new TextField(Send:, , 30, TextField.ANY);f.append(si);f.append(tf);f.addCommand(exitCommand);f.setCommandListener(this);display.setCurrent(f); public void start() Thread t = new Thread(this);t.start(); public void run() String portString = String.valueOf(port);try si.setText(Waiting for connection on port +portString); scn = (ServerSocketConnection) Connector.open(socket:/: + portString); / Wait for a connection. sc = (SocketConnection) scn.acceptAndOpen(); si.setText(Connection accepted); is = sc.openInputStream(); os = sc.openOutputStream(); sender = new Sender(os); / Allow sending of messages only after Sender is created f.addCommand(sendCommand); while (true) StringBuffer sb = new StringBuffer();int c = 0;while (c = is.read() != n) & (c != -1) sb.append(char) c);if (c = -1) break;si.setText(Message received - + sb.toString(); stop(); si.setText(Connection is closed); f.removeCommand(sendCommand); catch (IOException ioe) if (ioe.getMessage().equals(ServerSocket Open) Alert a = new Alert(Server, Port + portString+ is already taken., null, AlertType.ERROR);a.setTimeout(Alert.FOREVER);a.setCommandListener(this);display.setCurrent(a); else if (!stop) ioe.printStackTrace(); catch (Exception e) e.printStackTrace(); public void commandAction(Command c, Displayable s) if (c = sendCommand) & !parent.isPaused() sender.send(tf.getString();if (c = Alert.DISMISS_COMMAND) | (c = exitCommand) parent.notifyDestroyed(); parent.destroyApp(true); /* * Close all open streams */ public void stop() try stop = true; if (i
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2030动力总成电控系统集成化趋势与供应商格局分析报告
- 2025-2030功能性肽类饲料添加剂作用机制与临床效果验证报告
- 2025年新能源光伏电站智能化技术创新与运维报告
- 2025年偏远牧区光储一体化电力系统运行效果评估报告
- 2025年新能源产业人才培养与职业发展报告
- 2025年心脏病学冠心病患者抢救技能应急测试题答案及解析
- 2025年安全生产知识竞赛题库含答案
- 2025年低血糖处理试题及答案
- 2025年山东省安全员C证考试题库含答案
- 2025年新能源汽车充电设施与电动汽车产业协同发展报告
- 2023年广东生物高考第18题光合作用说题课件
- 除锈剂MSDS参考资料
- 6社会体育导论
- 部编版七年级历史与社会下册《第三课中华文明探源》评课稿
- 中考英语作文预测(范文20篇)
- 选煤厂原煤分级筛技术操作规程
- 方物电子教室q2用户手册
- 消防管道支架工程量计算表
- GB/T 700-2006碳素结构钢
- 腹腔镜下肾癌根治术
- 如何学好初中数学-课件
评论
0/150
提交评论