Java ME 開啟Socket串連示範

來源:互聯網
上載者: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();
}

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.