⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.java

📁 一个midlet应用模型 非常经典的例子 大家可以看看 非常经典
💻 JAVA
字号:
/*
 * @(#)Server.java	1.7 03/03/02
 *
 * Copyright (c) 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms
 */
package example.datagram;

import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import java.io.IOException;

public class Server implements Runnable, CommandListener {

    private DatagramMIDlet parent;
    private Display display;
    private Form f;
    private StringItem si;
    private TextField tf;

    private Command sendCommand = new Command("Send", Command.ITEM, 1);

    Sender sender;

    private String address;

    public Server(DatagramMIDlet m) {
        parent = m;
        display = Display.getDisplay(parent);
        f = new Form("Datagram Server");
        si = new StringItem("Status:" , " ");
        tf = new TextField("Send:", "", 30, TextField.ANY);
        f.append(si);
        f.append(tf);
        f.addCommand(sendCommand);
        f.setCommandListener(this);
        display.setCurrent(f);
    }

    public void start() {

        Thread t = new Thread(this);
        t.start();
    }


    public void run() {
        try {

            si.setText("Waiting for connection");
            DatagramConnection dc = (DatagramConnection)
                  Connector.open("datagram://:5555");

            sender = new Sender(dc);

            while (true) {
                Datagram dg = dc.newDatagram(100);
                dc.receive(dg);
                address = dg.getAddress();

                si.setText("Message received - " +
                                new String(dg.getData(), 0, dg.getLength()));

            }

        } catch (IOException ioe) {
            Alert a = new Alert("Server", "Port 5000 is already taken.",
                                null, AlertType.ERROR);
            a.setTimeout(Alert.FOREVER);
            a.setCommandListener(this);
            display.setCurrent(a);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void commandAction(Command c, Displayable s) {
        if (c == sendCommand && !parent.isPaused()) {
            if (address == null) {
                si.setText("No destination address");
            } else {
                sender.send(address, tf.getString());
            }
        }
        if (c == Alert.DISMISS_COMMAND) {
            parent.destroyApp(true);
            parent.notifyDestroyed();
        }
    }

    public void stop() {
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -