Example of opening a socket connection in Java me

Source: Internet
Author: User
/*
* 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();
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.