1、苏 州 市 职 业 大 学课 程 设 计 说 明 书名称_手持移动设备应用开发课程设计_2011 年 12 月 27 日 至 2011 年 12 月 29 日 共 1 周院 系_计算机工程系_班 级_09 计算机应用(对口)_姓 名_季丽_系主任_李金祥_教研室主任_刘文芝_指 导 教 师 _ 贾震斌/徐涛 _1目录一、课程设计目的与要求 11.1 设计目的 11.2 设计要求 11.3 设计内容简介 1二、开发环境与系统流程 22.1开发平台构建 .22.2系统流程图 .2三、详细设计与分析 23.1 服务器端主程序设计 23.2 服务器端读取客户信息线程设计 33.3 服务器端收发线程设计
2、 33.4 客户端主界面设计 33.5 客户端发送/接收信息设计 .3四、系统调试和运行 3六、课程设计总结 3七、参考文献 4附录(源程序代码) 41一、课程设计目的与要求1.1 设计目的本课程设计课题是为配合手持设备移动应用开发课程教学所开设的实践性环节课程,旨在要求学生综合掌握 Eclipse集成开发环境中基于 J2ME技术的手持移动设备应用开发的完整过程,特别是掌握用户界面设计、基于 Socket网络连接的程序设计方法,进一步熟悉 WTK文档的使用,强化学生面向职业应用的综合程序设计能力。1.2 设计要求(一)掌握 Eclipse集成开发环境中基于 J2ME技术的手持移动设备应用开发的
3、完整开发流程;(二)掌握课程设计课题的工作原理,完成系统分析及相应的程序设计工作,包括:(1)指定服务器端、客户端及通信端口号功能的主界面程序设计;(2)服务器端界面程序设计、服务器端读取客户发送信息程线程设计;(3)客户端界面程序设计、客户端发送/接收信息线程设计。(三)完成课程设计说明书编写。1.3 设计内容简介本课程设计参照 WTK文档实现一个基于 Socket网络编程协议的智能手机 J2ME网络通信应用程序,该系统同时包含服务器和客户端功能,可以根据通信需要启用服务器端功能或客户端功能,可以指定通信端口号,客户端与服务器之间的通信由独立的线程完成。2二、开发环境与系统流程2.1开发平台
4、构建JDK由一个标准类库和一组建立,测试及建立文档的 Java实用程序组成。其核心 Java WTK是 Sun公司提供的无线开发工具包, 分四种模拟器Defaultcolorphone 用于具有彩色显示屏幕的手机模拟器Defaultgrayphone 用于具有单色灰度屏幕的手机模拟器Mediacontrolskin 用于具有音频和视频控制功能的手机模拟器Qwertydevice 用于具有标准打字键盘的手机模拟器Eclipse 是一个开放源代码的软件开发项目,专注于为高度集成的工具开发提供一个全功能的、具有商业品质的工业平台。它主要由 Eclipse项目、Eclipse 工具项目和 Eclips
5、e技术项目三个项目组成,具体包括四个部分组成Eclipse Platform、JDT、CDT 和PDE。EclipseME是一个用来开发 J2ME应用程序的 Eclipse插件。EclipseME 集成了 Eclipse现有的 java editor功能,提高代码的编写效率1、新建一个 Socket项目。图 132、导入文件图 23、编写程序图 344、运行与调试,运行结果如下图。图 42.2系统流程图图 55三、详细设计与分析3.1 主界面程序设计public SocketMIDlet() display = Display.getDisplay(this);f = new Form(“So
6、cket 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.s
7、etCurrent(f);图 63.2 服务器端界面程序设计public Server(SocketMIDlet m, int p) parent = m;port = p;display = Display.getDisplay(parent);f = new Form(“Socket Server“);si = new StringItem(“Status:“, “ “);6tf = new TextField(“Send:“, “, 30, TextField.ANY);f.append(si);f.append(tf);f.addCommand(exitCommand);f.setCo
8、mmandListener(this);display.setCurrent(f);图 73.3 服务器端接收信息程序设计 while (c = is.read() != n) 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“)
9、 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();7图 83.4 客户端主界面设计public Client(SocketMIDlet m,
10、 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.setCur
11、rent(f);图 983.5 客户端发送 /接收信息设计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.se
12、tTimeout(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) if (c = Alert.DISMISS_COMMAND) | (c = exitCommand) pare
13、nt.notifyDestroyed();parent.destroyApp(true);图 109四、系统调试和运行这部分内容主要介绍设计、调试及运行过程中遇到困难及解决方法。1 要切换空间,重新设置 Eclipse。2当从其它盘导入程序时,会出现如图 12所示错误,只要按照提示选择项即可。五、课程设计总结通过本次实验,我能够在 MIDlet中使用 Socket网络连接以及进行简单编程,实现了模拟发短信的功能,顺利完成了本次实验的任务。通过对整个应用程序的设计,建模,编码和测试,从中学到了不少的知识。由于对开发平台不是很熟悉,之前也没做过类似的项目,所以在完成项目的过程中也遇到不少的困难和疑
14、问,通过查阅资料、向他人请教以及自己的实践,在大家的共同努力下最终解决了这些问题,使得程序得以实现。程序中还有一些不足和需要完善的地方,待今后的深入实践中再继续完善。六、参考文献1 陆东林,宾晟,国刚 编著.J2ME 开发技术原理与实践教程.北京:电子工业出版社,2008.2 刘斌 编著. Java ME 实用详解用 Eclipse进行移动开发.北京:电子工业出版社,2007.10附录(源程序代码)Client 类:import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import j
15、avax.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.CommandListene
16、r;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
17、 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;priva
18、te 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);
19、f.setCommandListener(this);display.setCurrent(f);/* Start the client thread*/public void start() 11Thread 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 s
20、erver on port “+portString);is = sc.openInputStream();os = sc.openOutputStream();/ Start the thread for sending messages - see Senders main/ comment for explanationsender = new Sender(os);/ Loop forever, receiving datawhile (true) StringBuffer sb = new StringBuffer();int c = 0;while (c = is.read() !
21、= n) 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, Ale
22、rtType.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) if (c = Alert.DISMISS_COMMAND) | (c =
23、 exitCommand) parent.notifyDestroyed();parent.destroyApp(true);/* Close all open streams*/12public 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) Sender 类:import java.io.IOExcepti
24、on;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, wa
25、it until one connectsif (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 waitmessage = null;1
26、3public synchronized void stop() message = null;notify();Server 类: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.mi
27、croedition.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.
28、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“, Com
29、mand.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(“So
30、cket 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();14public void run() String portString = S
31、tring.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.openOutputSt
32、ream();sender = new Sender(os);/ Allow sending of messages only after Sender is createdf.addCommand(sendCommand);while (true) StringBuffer sb = new StringBuffer();int c = 0;while (c = is.read() != n) if (c = -1) break;si.setText(“Message received - “ + sb.toString();stop();si.setText(“Connection is
33、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 (!sto
34、p) ioe.printStackTrace(); catch (Exception e) e.printStackTrace();public void commandAction(Command c, Displayable s) if (c = sendCommand) if (c = Alert.DISMISS_COMMAND) | (c = exitCommand) parent.notifyDestroyed();parent.destroyApp(true);/* Close all open streams*/public void stop() 15try stop = tr
35、ue;if (is != null) is.close();if (os != null) os.close();if (sc != null) sc.close();if (scn != null) scn.close(); catch (IOException ioe) SocketMIDlet类:import javax.microedition.lcdui.Choice;import javax.microedition.lcdui.ChoiceGroup;import javax.microedition.lcdui.Command;import javax.microedition
36、.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 privat
37、e 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 portF
38、ield;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 s
39、elect 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);16f.addCommand(startCommand);f.setCommandListener(this);display.setCurrent(f);public boolean isPaused() r
40、eturn 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();