?? socketclient.java
字號:
/*
* @(#)SocketClient.java
*
* 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 SocketClient implements Runnable, CommandListener {
private InstantChat parent;
private Display display;
private Form f;
private StringItem si;
private TextField tf;
private boolean stop;
private boolean connected = false;
private static int KNOWN_PORT = 40000;
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:40000", 30,
TextField.ANY);
InputStream is;
OutputStream os;
SocketConnection sc;
SocketSender sender;
public SocketClient(InstantChat m) {
parent = m;
display = Display.getDisplay(parent);
f = new Form("Chat Session - Socket");
si = new StringItem("Status:" , "Not connected");
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);
}
/**
* Start the client thread
*/
public void start() {
Thread t = new Thread(this);
t.start();
}
synchronized private void waitForConnect()
{
try {
wait();
}
catch (InterruptedException e) {
throw new RuntimeException("Wait failure");
}
}
synchronized private void signalConnectDone()
{
notify();
}
private void connect()
{
try
{
String host = tfAddr.getString(); //+ ":" + KNOWN_PORT;
sc = (SocketConnection) Connector.open("socket://" + host);
si.setText("Connected to " + host);
is = sc.openInputStream();
os = sc.openOutputStream();
sender = new SocketSender(os);
signalConnectDone();
connected = true;
}
catch (ConnectionNotFoundException cnfe) {
cnfe.printStackTrace();
}
catch (IOException ioe) {
if (!stop) {
ioe.printStackTrace();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
try {
waitForConnect();
// Start the thread for sending messages - see Sender's main
// comment for explanation
// Loop forever, receiving data
while (true) {
StringBuffer sb = new StringBuffer();
int c = 0;
while (((c = is.read()) != '\n') && (c != -1)) {
sb.append((char) c);
}
// Display message to user
si.setText("Message received - " + sb.toString());
}
} catch (ConnectionNotFoundException cnfe) {
Alert a = new Alert("Client", "Please run Server MIDlet first",
null, AlertType.ERROR);
a.setTimeout(Alert.FOREVER);
a.setCommandListener(this);
display.setCurrent(a);
} catch (IOException ioe) {
if (!stop) {
ioe.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void commandAction(Command c, Displayable s) {
if (c == sendCommand && !parent.isPaused()) {
if (!connected)
connect();
sender.send(tf.getString());
}
else if (c == exitCommand) {
parent.notifyDestroyed();
parent.destroyApp(true);
}
}
/**
* Close all open streams
*/
public void stop() {
try {
stop = true;
sender.stop();
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
if (sc != null) {
sc.close();
}
}
catch (IOException ioe) {
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -