serversocket.java
来自「OReilly出版的《J2ME in a Nutshell》可谓经典之作」· Java 代码 · 共 50 行
JAVA
50 行
package ora.ch6;
import java.io.IOException;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
public class ServerSocket {
public static void main(String[] args) {
try {
StreamConnectionNotifier serverSocket =
(StreamConnectionNotifier)Connector.open("socket://:8000");
for (;;) {
// Get the next connection
final StreamConnection socket =
(StreamConnection)serverSocket.acceptAndOpen();
// Handle the connection in a new thread
Thread t = new Thread() {
public void run() {
OutputStream os = null;
try {
os = socket.openOutputStream();
// Communicate with client here....
} catch (IOException ex) {
System.out.println(ex);
} finally {
if (os != null) {
try {
os.close();
os = null;
} catch (IOException ex) {
}
}
try {
socket.close();
} catch (IOException ex) {
}
}
}
};
t.start();
}
} catch (IOException e) {
System.out.println(e);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?