六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網(wǎng)中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

在J2ME中完成基于UDP協(xié)議通訊程序

[摘要]在GCF中提供了DatagramConnection和Datagram兩個接口,借助他們我們可以在J2ME中基于UDP協(xié)議開發(fā)聯(lián)網(wǎng)應用程序。在MIDP2.0中,添加了UDPDatagramConnection這個接口,他擴展了DatagramConnection并添加了兩個方法getLocalAd...
在GCF中提供了DatagramConnection和Datagram兩個接口,借助他們我們可以在J2ME中基于UDP協(xié)議開發(fā)聯(lián)網(wǎng)應用程序。在MIDP2.0中,添加了UDPDatagramConnection這個接口,他擴展了DatagramConnection并添加了兩個方法getLocalAddress()和getLocalPort()。我們知道UDP服務是不可靠的,如果你希望開發(fā)更可靠的聯(lián)網(wǎng)應用的話可以采用SocketConnection,因為TCP服務是面向連接且可靠的。我們還必須清楚地一點是以上所說的各種連接方式都不是MIDP規(guī)范中規(guī)定必須實現(xiàn)的。因此在使用之前請參考特定設備的開發(fā)文檔。MIDP中只有Http連接是必須支持的。

  同樣,我們要獲得DatagramConnection的話,必須通過Connector的open方法,其中的URL應該滿足如下的形式。
datagram://localhost:5555 這樣的話表示建立了一個客戶端模式的連接。在指定ip:localhost和指定端口:5555
datagram://:5555 這樣建立的是一個服務器端模式的連接,在本地的5555端口。
  建立連接后,我們可以通過DatagramConnection的newDatagram()方法構造一個Datagram,然后調用DatagramConnection的send()方法。這樣數(shù)據(jù)報將會發(fā)送到指定的接受方。例如你可以構建這個一個負責發(fā)送數(shù)據(jù)的Sender類。

package com.siemens.datagramtest;

import javax.microedition.io.Datagram;
import javax.microedition.io.DatagramConnection;

public class Sender extends Thread
{

  private DatagramConnection dc;

  private String address;

  private String message;

  public Sender(DatagramConnection dc)
  {
    this.dc = dc;
    start();
  }

  public synchronized void send(String addr, String msg)
  {
    address = addr;
    message = msg;
    notify();
  }

  public synchronized void run()
  {

    while (true)
    {

      // If no client to deal, wait until one connects
      if (message == null)
      {
        try
        {
          wait();
        } catch (InterruptedException e)
        {
        }
      }

      try
      {
        byte[] bytes = message.getBytes();
        Datagram dg = null;
        // Are we a sender thread for the client ? If so then there's
        // no address parameter
        if (address == null)
        {
          dg = dc.newDatagram(bytes, bytes.length);
        } else
        {
          dg = dc.newDatagram(bytes, bytes.length, address);
          System.out.println(address);
        }
        dc.send(dg);
      } catch (Exception ioe)
      {
        ioe.printStackTrace();
      }

      // Completed client handling, return handler to pool and
      // mark for wait
      message = null;
    }
  }

}


  注意聯(lián)網(wǎng)的時候我們應該在另外一個線程中而不是在主線程中。

服務器端的目的就是啟動后監(jiān)聽指定的端口,當客戶端連接過來后接受數(shù)據(jù)并記錄下客戶端的地址,以便服務器端向客戶端發(fā)送數(shù)據(jù)。

package com.siemens.datagramtest;

import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;
import javax.microedition.io.DatagramConnection;
import javax.microedition.io.UDPDatagramConnection;
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 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()
  {
  }

}


客戶端代碼則是建立連接后向服務器端發(fā)送數(shù)據(jù),并等待接受服務器返回的數(shù)據(jù)。

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();
      }
    }
  }

}




主站蜘蛛池模板: 日本一区二区视频在线 | 亚洲成a人片在线v观看 | 日韩中文一区宇都宫紫苑 | 天天色播 | 婷婷开心六月久久综合丁香 | 青青青国产成人久久111网站 | 依人在线视频 | 天天干妹子 | 青青青青爽极品在线视频 | 色噜噜狠狠一区二区三区 | 日日夜夜摸| 日本道久久 | 一级片aaaa | 天天干妹子 | 五月婷婷深爱五月 | 伊人中文字幕在线观看 | 日韩国产在线观看 | 色先锋资源 | 色站综合 | 最好韩国日本高清免费 | 青春草在线观看 | 日韩在线网址 | 一级做a爰片久久毛片免费看 | 色呦呦视频在线观看 | 欧美一级视频免费看 | 最近在线观看免费完整视频 | 亚洲男人天堂视频 | 天美传媒在线观看 | 日本五级床片全部免费午夜 | 日本一区二区三区精品国产 | 中文字幕在线看视频一区二区三区 | 亚洲精品中文字幕午夜 | 日本高清视频一区 | 日韩免费专区 | 亚洲六月丁香六月婷婷花 | 日韩色影院 | 日韩一区国产二区欧美三区 | 四虎在线精品 | 青娱乐国产精品 | 日韩毛片大全免费高清 | 亚洲综合色丁香婷婷六月图片 |