sc.setSocketOption(KEEPALIVE, 0);
sc.setSocketOption(RCVBUF, 128);
sc.setSocketOption(SNDBUF, 128);
// Get the input stream of the connection.
DataInputStream is = sc.openDataInputStream();
// Get the output stream of the connection.
DataOutputStream os = sc.openDataOutputStream();
// Read the input data.
String result = is.readUTF();
// Echo the data back to the sender.
os.writeUTF(result);
// Close everything.
is.close();
os.close();
sc.close();
scn.close();
7.4.2 Socket示例
其实前面已经说过了,在J2ME中使用Socket和J2SE中差不多,无非就是由服务端打开一个监听端口等待客户端请求该端口,下面我们还来看个具体的示例(该示例代码来自J2ME开发网 mingjava)
package socket;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class SocketMIDlet extends MIDlet implements CommandListener {
private static final String SERVER = "Server";
private static final String CLIENT = "Client";