j2me中socket的开发
来源:互联网 发布:js中换行符是哪个 编辑:程序博客网 时间:2024/05/01 15:24
在J2ME中基于UDP协议编程一文中介绍了如何使用Datagram和DatagramConnection来开发应用程序,本文将主要讲述在MIDP2.0中使用ServerSocketConnection和SocketConnection来开发联网应用程序。TCP协议是可以保证传输的质量的,这也是他和UDP的一大区别。由于以上谈到的四个类都属于GCF,他们在程序编写方面也非常相似,通常我们在Server端的某个端口监听,当客户端连接服务器的时候,则可以得到一个SocketConnection的实例。通过两端的SocketConnection则可以实现C/S结构的通信了。 首先看一下,ServerSocketConnection类,它的一个非常重要的方法就是acceptAndOpen()方法,这个方法返回一个SocketConnection实例,从而使得客户端和服务器端可以通过socket通信了。典型的代码如下: // Wait for a connection. // Set application specific hints on the socket. // Get the input stream of the connection. // Get the output stream of the connection. // Read the input data. // Echo the data back to the sender. // Close everything. SocketConnection的使用也是非常简单,通过Connector的open方法我们可以得到一个SocketConnection的实例。 InputStream is = sc.openInputStream(); os.write("/r/n".getBytes()); is.close(); 下图是我在运行WTK中NetworkDemo的时候的截图!代码也一并贴出来。 package socket; import javax.microedition.midlet.*; public class SocketMIDlet extends MIDlet implements CommandListener { 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 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.addCommand(exitCommand); display.setCurrent(f); public boolean isPaused() { public void startApp() { public void pauseApp() { public void destroyApp(boolean unconditional) { public void commandAction(Command c, Displayable s) { } package socket; import javax.microedition.midlet.*; public class Server implements Runnable, CommandListener { 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; public Server(SocketMIDlet m) { public void start() { public void run() { // Wait for a connection. // Allow sending of messages only after Sender is created while (true) { while (((c = is.read()) != '/n') && (c != -1)) { if (c == -1) { public void commandAction(Command c, Displayable s) { if ((c == Alert.DISMISS_COMMAND) || (c == exitCommand)) { /** if (is != null) { if (os != null) { if (sc != null) { if (scn != null) { package socket; import javax.microedition.midlet.*; public class Client implements Runnable, CommandListener { 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; public Client(SocketMIDlet m) { /** public void run() { // Start the thread for sending messages - see Sender's main // Loop forever, receiving data while (((c = is.read()) != '/n') && (c != -1)) { if (c == -1) { // Display message to user public void commandAction(Command c, Displayable s) { if ((c == Alert.DISMISS_COMMAND) || (c == exitCommand)) { /** if (sender != null) { if (is != null) { if (os != null) { if (sc != null) { package socket; import javax.microedition.midlet.*; public class Sender extends Thread { private OutputStream os; private String message; public Sender(OutputStream os) { public synchronized void send(String msg) { public synchronized void run() { while (true) { // If no client to deal, wait until one connects if (message == null) { try { // Completed client handling, return handler to pool and public synchronized void stop() { |
- j2me中socket的开发
- J2ME中使用socket开发联网程序
- J2ME中使用Socket开发联网程序
- J2ME中使用Socket开发联网程序
- J2ME:MIDP开发Socket程序
- j2me 程序开发中内存大小的思考
- j2me的socket通讯问题
- J2ME SOCKET
- J2me socket
- J2ME游戏开发中使用层的概念
- J2ME游戏开发中时钟的简单实现
- J2ME游戏开发中使用层的概念
- J2ME游戏开发中时钟的简单实现
- J2ME开发中彩色转灰度算法的应用
- J2me开发中采用CMWAP接入点的问题
- J2ME游戏开发中时钟的简单实现
- j2me开发中遇见的一点错误记录
- J2ME游戏开发中时钟的简单实现
- Project Note4
- 一个封装比较完整的FTP类——clsFTP
- 关于SSH整合的一点心得
- oracle锁机制
- 理解java.sql和java.text包---日期处理相关
- j2me中socket的开发
- 管理软件到底有没有前途(三)-从国家十一五信息化战略规划来看
- php调用web service
- Tomcat+Axis实现webservice
- 数组指针和指针数组
- POJ 2996 Help Me with the Game && POJ 2993 Emag eht htiw Em Pleh
- 补水养生的14个惊人真相
- 修改时间
- Firefox在含有flash的网页上提示:不建议使用 getBoxObjectFor() 。 请使用 element.getBoundingClientRect()。