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

📄 datagramclient.java

📁 这是我在测试手机java程序时候做的关于手机java及时聊天的工具
💻 JAVA
字号:
/*
 * @(#)DatagramClient.java	1.6 03/03/02
 *
 * Copyright (c) 2003 Qualcomm, Inc. All rights reserved.
 * PROPRIETARY/CONFIDENTIAL
 * Use is subject to license terms
 */
package com.qualcomm.demo.instantchat;

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

public class DatagramClient implements Runnable, CommandListener {
    private InstantChat parent;
    private Display display;
    private Form f;
    private StringItem si;
    private TextField tf;
    private boolean connected = false;
    private static int KNOWN_PORT = 50000;

    private Command sendCommand = new Command("Send", Command.ITEM, 1);
    private Command exitCommand = new Command("Exit", Command.EXIT, 1);
    private TextField tfAddr = new TextField("Remote host:",
                                             "129.46.223.132:50000", 30,
                                             TextField.ANY);
    DatagramSender sender;
    private DatagramConnection dc;

    public DatagramClient(InstantChat m) {
        parent = m;
        display = Display.getDisplay(parent);
        f = new Form("Chat Session - Datagram");
        si = new StringItem("Status:" , " ");
        tf = new TextField("Send:", "", 30, TextField.ANY);

        f.append(si);
        f.append(tfAddr);
        f.append(tf);
        f.addCommand(sendCommand);
        f.addCommand(exitCommand);
        f.setCommandListener(this);
        display.setCurrent(f);
    }

    public void start() {
        Thread t = new Thread(this);
        t.start();
    }

    private void connect()
    {
      try
      {
        String host = tfAddr.getString();// + ":" + KNOWN_PORT;
        DatagramConnection dc = (DatagramConnection)
                                Connector.open("datagram://" + host);
        si.setText("Connected to " + host);
        sender = new DatagramSender(dc);
        signalConnectDone();
        connected = true;
      }
      catch (ConnectionNotFoundException cnfe) {
            cnfe.printStackTrace();
      }
      catch (IOException ioe) {
            ioe.printStackTrace();
      }
      catch (Exception e) {
        e.printStackTrace();
      }
    }

    public void run() {
        try {
            //String host = tfAddr.getString() + ":" + KNOWN_PORT;
            //DatagramConnection dc = (DatagramConnection) Connector.open("datagram://" + host);
            //si.setText("Connected to " + host);
            //sender = new DatagramSender(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()) {
          if (!connected)
          {
            connect();
          }
          sender.send(null, tf.getString());
        }
        else if (c == exitCommand) {
            parent.notifyDestroyed();
            parent.destroyApp(true);
        }
    }

    public void stop() {
    }

    synchronized private void waitForConnect()
    {
      try {
        wait();
      }
      catch (InterruptedException e) {
        throw new RuntimeException("Wait failure");
      }
    }

    synchronized private void signalConnectDone()
    {
       notify();
    }
}

⌨️ 快捷键说明

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