科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件在J2ME中实现基于UDP协议通讯程序

在J2ME中实现基于UDP协议通讯程序

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

在GCF中提供了DatagramConnection和Datagram两个接口,借助他们我们可以在J2ME中基于UDP协议开发联网应用程序.

作者:mingjava 来源:J2ME开发网 2007年12月28日

关键字:

  • 评论
  • 分享微博
  • 分享邮件
客户端代码则是建立连接后向服务器端发送数据,并等待接受服务器返回的数据。

package com.siemens.datagramtest;

import java.io.IOException;

import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;
import javax.microedition.io.DatagramConnection;
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 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;

  public Client(DatagramMIDlet m)
  {
    parent = m;
    display = Display.getDisplay(parent);
    f = new Form("Datagram Client");
    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
    {

      DatagramConnection dc = (DatagramConnection) Connector
          .open("datagram://localhost:5555");
     

      si.setText("Connected to server");

      sender = new Sender(dc);

      while (true)
      {
        Datagram dg = dc.newDatagram(100);
        dc.receive(dg);
        // Have we actually received something or is this just a timeout
        // ?
        if (dg.getLength() > 0)
        {
          si.setText("Message received - "
              + new String(dg.getData(), 0, dg.getLength()));
        }
      }

    } catch (ConnectionNotFoundException cnfe)
    {
      Alert a = new Alert("Client", "Please run Server MIDlet first",
          null, AlertType.ERROR);
      a.setTimeout(Alert.FOREVER);
      display.setCurrent(a);
    } catch (IOException ioe)
    {
      ioe.printStackTrace();
    }
  }

  public void commandAction(Command c, Displayable s)
  {
    if (c == sendCommand && !parent.isPaused())
    {
      sender.send(null, tf.getString());
    }
  }

  public void stop()
  {
  }

}

  本文的代码取自WTK demo中的例子,您可以参考demo中的源代码!下面给出MIDlet的代码

package com.siemens.datagramtest;

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.midlet.MIDlet;

public class DatagramMIDlet 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;

  ChoiceGroup cg;

  private boolean isPaused;

  private Command exitCommand = new Command("Exit", Command.EXIT, 1);

  private Command startCommand = new Command("Start", Command.ITEM, 1);

  public DatagramMIDlet()
  {
    display = Display.getDisplay(this);
    f = new Form("Datagram Demo");
    cg = new ChoiceGroup("Please select peer", Choice.EXCLUSIVE, names,
        null);
    f.append(cg);

    f.addCommand(exitCommand);
    f.addCommand(startCommand);
    f.setCommandListener(this);

    display.setCurrent(f);
  }

  public static Display getDisplay()
  {
    return display;
  }

  public boolean isPaused()
  {
    return isPaused;
  }

  public void startApp()
  {
    isPaused = false;
  }

  public void pauseApp()
  {
    isPaused = true;
  }

  public void destroyApp(boolean unconditional)
  {
  }

  public void commandAction(Command c, Displayable s)
  {
    if (c == exitCommand)
    {
      destroyApp(true);
      notifyDestroyed();
    } else if (c == startCommand)
    {
      String name = cg.getString(cg.getSelectedIndex());
      if (name.equals(SERVER))
      {
        Server server = new Server(this);
        server.start();
      } else
      {
        Client client = new Client(this);
        client.start();
      }
    }
  }

}

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章