Java ME 打开Socket连接示范

/*
* 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();
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值