设为首页】 【加入收藏】 【网站地图】 【商品折扣
娱乐一生 娱乐明星
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
首页  |  java  |  .NET  |  C/C++  |  网页技术  |  php  |  asp  |  delphi  |  VC  |  VB开发  |  游戏开发  |  软件工程  |  Power Builder  |  Linux开发  |  Windows开发技巧
当前位置:首页 >> 联网开发 >> 教程:在J2ME中基于UDP协议编程

教程:在J2ME中基于UDP协议编程 -


  在GCF中提供了DatagramCo ection和Datagram两个接口,借助他们我们可以在J2ME中基于UDP协议开发联网应用程序,在MIDP2.0中,添加了UDPDatagramCo ection这个接口。他扩展了DatagramCo ection并添加了两个方法getLocalAddre ()和getLocalPort()。我们知道UDP服务是不可靠的,如果你希望开发更可靠的联网应用的话可以采用SocketCo ection,因为TCP服务是面向连接且可靠的。我们还必须清楚地一点是以上所说的各种连接方式都不是MIDP规范中规定必须实现的。因此在使用之前请参考特定设备的开发文档。MIDP中只有Http连接是必须支持的。
  
  同样,我们要获得DatagramCo ection的话,必须通过Co ector的open方法,其中的URL应该满足如下的形式。
  
  datagram://localhost:5555 这样的话表示建立了一个客户端模式的连接。在指定ip:localhost和指定端口:5555
  datagram://:5555 这样建立的是一个服务器端模式的连接,在本地的5555端口。
  建立连接后,我们可以通过DatagramCo ection的newDatagram()方法构造一个Datagram,然后调用DatagramCo ection的send()方法。这样数据报将会发送到指定的接受方。例如你可以构建这个一个负责发送数据的Sender类。
  


  package com.sieme .datagramtest;
  
  import javax.microedition.io.Datagram;
  import javax.microedition.io.DatagramCo ectio 
  
  public cla  Sender extends Thread
  {
  
    private DatagramCo ection dc;
  
    private String addre 
  
    private String me age;
  
    public Sender(DatagramCo ection dc)
    {
      this.dc = dc;
      start();
    }
  
    public synchronized void send(String addr, String msg)
    {
      addre  = addr;
      me age = msg;
      notify();
    }
  
    public synchronized void run()
    {
  
      while (true)
      {
  
        // If no client to deal, wait until one co ects
        if (me age == null)
        {
          try
          {
            wait();
          } catch (InterruptedException e)
          {
          }
        }
  
        try
        {
          byte[] bytes = me age.getBytes();
          Datagram dg = null;
          // Are we a sender thread for the client ? If so then there"s
          // no addre  parameter
          if (addre  == null)
          {
            dg = dc.newDatagram(bytes, bytes.length);
          } else
          {
            dg = dc.newDatagram(bytes, bytes.length, addre );
            System.out.println(addre );
          }
          dc.send(dg);
        } catch (Exception ioe)
        {
          ioe.printStackTrace();
        }
  
        // Completed client handling, return handler to pool and
        // mark for wait
        me age = null;
      }
    }
  
  }
  注意联网的时候我们应该在另外一个线程中而不是在主线程中。
  
  服务器端的目的就是启动后监听指定的端口,当客户端连接过来后接受数据并记录下客户端的地址,以便服务器端向客户端发送数据。
  package com.sieme .datagramtest;
  
  import java.io.IOExceptio 
  
  import javax.microedition.io.Co ector;
  import javax.microedition.io.Datagram;
  import javax.microedition.io.DatagramCo ectio 
  import javax.microedition.io.UDPDatagramCo ectio 
  import javax.microedition.lcdui.Alert;
  import javax.microedition.lcdui.AlertType;
  import javax.microedition.lcdui.Command;
  import javax.microedition.lcdui.CommandListener;
  import javax.microedition.lcdui.Di lay;
  import javax.microedition.lcdui.Di layable;
  import javax.microedition.lcdui.Form;
  import javax.microedition.lcdui.StringItem;
  import javax.microedition.lcdui.TextField;
  
  public cla  Server implements Ru able, CommandListener
  {
  
    private DatagramMIDlet parent;
  
    private Di lay di lay;
  
    private Form f;
  
    private StringItem si;
  
    private TextField tf;
  
    private Command sendCommand = new Command("Send", Command.ITEM, 1);
  
    Sender sender;
  
    private String addre 
  
    public Server(DatagramMIDlet m)
    {
      parent = m;
      di lay = Di lay.getDi lay(parent);
      f = new Form("Datagram Server");
      si = new StringItem("Status:", " ");
      tf = new TextField("Send:", "", 30, TextField.ANY);
      f.a end(si);
      f.a end(tf);
      f.addCommand(sendCommand);
      f.setCommandListener(this);
      di lay.setCurrent(f);
    }
  
    public void start()
    {
  
      Thread t = new Thread(this);
      t.start();
    }

JAVA天堂


  
    public void run()
    {
      try
      {
  
        si.setText("Waiting for co ection");
        DatagramCo ection dc =(DatagramCo ection)Co ector.open("datagram://:5555");
  
  
        sender = new Sender(dc);
  
        while (true)
        {
          Datagram dg = dc.newDatagram(100);
          dc.receive(dg);
          addre  = dg.getAddre ();
          si.setText("Me age 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);
        di lay.setCurrent(a);
      } catch (Exception e)
      {
        e.printStackTrace();
      }
    }
  
    public void commandAction(Command c, Di layable s)
    {
      if (c == sendCommand   !parent.i aused())
      {
        if (addre  == null)
        {
          si.setText("No destination addre ");
        } else
        {
          sender.send(addre , tf.getString());
        }
      }
      if (c == Alert.DISMI _COMMAND)
      {
        parent.destroyA (true);
        parent.notifyDestroyed();
      }
    }
  
    public void stop()
    {
    }
  
  }
  
  客户端代码则是建立连接后向服务器端发送数据,并等待接受服务器返回的数据。
  package com.sieme .datagramtest;
  
  import java.io.IOExceptio 
  
  import javax.microedition.io.Co ectio otFoundExceptio 
  import javax.microedition.io.Co ector;


  import javax.microedition.io.Datagram;
  import javax.microedition.io.DatagramCo ectio 
  import javax.microedition.lcdui.Alert;
  import javax.microedition.lcdui.AlertType;
  import javax.microedition.lcdui.Command;
  import javax.microedition.lcdui.CommandListener;
  import javax.microedition.lcdui.Di lay;
  import javax.microedition.lcdui.Di layable;
  import javax.microedition.lcdui.Form;
  import javax.microedition.lcdui.StringItem;
  import javax.microedition.lcdui.TextField;
  
  public cla  Client implements Ru able, CommandListener
  {
  
    private Da



 

娱乐图摘

更多 >>

靓丽清纯美女meimei

美女私房全裸照
导演劝女演员脱衣服(视频)

大胆火辣人体艺术写真(图)

黑丝妹妹热辣诱惑-丝袜美女妹妹

PLMM 漂亮妹妹图集-妹妹图库

全球美女图库-美女集中营

52MM 我爱漂亮妹妹-制服妹妹诱惑

图王图库-世界美女明星图片资料库
美女写真集锦

激情两性-解密性生活
浴室MM湿身内衣诱惑
邻家小妹洗澡被偷拍(视频)

热点文章

更多

· 如何使用“月蚀”进行J2ME开发
· Java学习:解决J2ME中的内存泄漏
· J2ME编程实例---之数字键的测试
· 手机铃声格式规范,midi格式规范 -IT资源在线
· 介绍J2ME平台的几个重要概念
· 移动业务代码规范方案 -IT资源在线
· 如何搭建J2ME的开发环境之一二
· 请关注J2ME WTK2.2 的新特性
· 对于开发SMS初学者的,经常用到的几个函数。 -IT资源在线
· J2ME与Web Service-KSOAP快速上手

热点文章

更多