?? datagramclient.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 + -