/*
* ScoketClient.java
*/
package mypackage;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import mypackage.SocketBot;
/**
* socket commucation midlet
*
* @author
*
*/
public class SocketClient extends MIDlet implements CommandListener {
private static final String TITLE = "Socket Test";
private static final String SOCKET_URL = "socket_url";
private TextBox textBox;
private Command exitCommand;
public SocketClient() {
initUI();
}
protected void destroyApp(boolean unconditional) {
// empty
}
protected void pauseApp() {
// empty
}
protected void startApp() throws MIDletStateChangeException {
Display.getDisplay(this).setCurrent(textBox);
}
private void initUI() {
String url = this.getAppProperty(SOCKET_URL);
SocketBot soc = new SocketBot(url);
soc.send();
String content = soc.retrieveLog();
textBox = new TextBox(TITLE, content, 1024, TextField.ANY);
exitCommand = new Command("Exit", Command.EXIT, 0);
textBox.addCommand(exitCommand);
textBox.setCommandListener(this);
}
public void commandAction(Command arg0, Displayable arg1) {
this.destroyApp(false);
super.notifyDestroyed();
}
}
//////////////////////////////////////// //////////////////////////////////////// ////////////////////////////////////////
/*
* SocketBot.java Sep 21, 2006
*/
package mypackage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.SocketConnection;
/**
* do socket connection things
*
* @author
*
*/
public class SocketBot {
private static final String HELLO = "hello";
private static final String CRLF = "/r/n";
private static final String SEND = ">>", RECV = "<<";
private String url;
private StringBuffer sb;
public SocketBot(String newUrl) {
this.url = newUrl;
sb = new StringBuffer();
}
/**
* send and recieve some data
*/
public void send() {
SocketConnection conn;
// open connection goes here
try {
conn = (SocketConnection) Connector.open(url);
} catch (IOException e) {
sb.append(e.toString());
return;
}
try {
conn.setSocketOption(SocketConnection.DELAY, 0);
InputStream recvStream = conn.openInputStream();
OutputStream sendStream = conn.openOutputStream();
// write something
sendStream.write(HELLO.getBytes());
sendStream.write(CRLF.getBytes());
// append send data to log
sb.append(SEND).append(HELLO).append(CRLF);
// begin to read
StringBuffer sbuf = new StringBuffer();
int c = 0;
while ((c = recvStream.read()) != -1) {
sbuf.append((char) c);
}
// put to log
sb.append(RECV).append(sbuf.toString()).append(CRLF);
// close input stream and output stream when done
recvStream.close();
sendStream.close();
// append exception to log if any
} catch (IllegalArgumentException e) {
sb.append(e.toString());
} catch (IOException e) {
sb.append(e.toString());
} finally {
// do not forget to close the connection for re-use
try {
conn.close();
} catch (IOException ignored) {
// should be ignored
}
}
}
/**
* retreive the result log
*
* @return
*/
public String retrieveLog() {
return sb.toString();
}
}