Posted on 2005-11-08 14:08
讓變化成為計劃的一部分 閱讀(642)
評論(1) 編輯 收藏 所屬分類:
J2ME
第一,同一個端口是不允許兩個程序監(jiān)聽的。
參見注釋:
Trying to bind to an already reserved local address causes an IOException to be thrown!
也就是說。調(diào)用(MessageConnection)Connector.open("sms://:5000"); 時就會拋出異常。
第二,(MessageConnection)Connector.open監(jiān)聽不同端口是沒有問題的。比如http://www.cnblogs.com/Files/zhengyun_ustc/SimplePushRegistry.rar的例子,你就可以看出這一點。在midlet的startApp中,
protected void startApp() throws MIDletStateChangeException {
if (!init) {
init = true;
String[] conns =
PushRegistry.listConnections(false);
System.out.println("Found " + conns.length +
" connections.");
for(int ccnt=0; ccnt < conns.length; ccnt++){
DatagramHandler handler =
new DatagramHandler(conns [ccnt], this);
connectionHandlers.addElement(handler);
handler.start();
}
}
}
從而得到所有注冊的連接,并轉(zhuǎn)發(fā)給線程處理。線程就可以根據(jù)所傳入的連接名稱,自行打開:
public DatagramHandler(String c, PushSMSListner midlet){
try {
dgc = (DatagramConnection)Connector.open(c);
} catch (IOException x){
x.printStackTrace();
}
m_midlet = midlet;
}