📄 socketconnection.java
字号:
// SocketConnection.java
// Connect socket to remote server at port 80
// and request HTTP service for viewing index.html
// initiate MIDlet Class
import javax.microedition.midlet.*;
// initiate GUI Class
import javax.microedition.lcdui.*;
// initiate Network Class
import javax.microedition.io.*;
// initiate IO Class
import java.io.*;
// create SocketConnection by inheritting MIDlet
public class SocketConnection extends MIDlet
{
// declare object variable sc for i/p stream
// and o/p for send/receive socket connection
private StreamConnection sc = null;
// declare object variable os for o/p stream
// use for service requesting
private OutputStream os = null;
// declare object variable dos for o/p stream data
// use for service requesting
private DataOutputStream dos = null;
// declare object is for i/p stream
// use for web server responding
private InputStream is = null;
// declare object dis for i/p stream data
// use for web server responding
private DataInputStream dis = null;
// create connection string to host 216.21.156.11
// at port 80 by socket connection
private String url = "socket://216.21.156.11:80";
// declare object variable buffer
// for containing web pag content
private StringBuffer buffer;
// declare variables for GUI components
private Display display = null;
private Form screen;
private StringItem getString;
// initiate SocketConnection() Constructor
// for the MIDlet
public SocketConnection()
{
// create object buffer for carrying string
// for service requsting
buffer = new StringBuffer();
// get object display
// for mobile device display
display = Display.getDisplay(this);
// create object screen
// for forming Page Content on the screen
screen = new Form("Page Content");
}
// start MIDlet
public void startApp()
{
try {
// create object sc
// for socket connection
sc = (StreamConnection) Connector.open(url);
// create object os
// for o/p stream
os = sc.openOutputStream();
// create object dos
// for o/p stream data
dos = new DataOutputStream(os);
// request HTTP service
dos.writeChars("GET /toc.htm \n");
// use method flush()
// to make sure that the request has // been sent to server
dos.flush();
// create object is
// for i/p stream
is = sc.openInputStream();
// create object dis
// for o/p stream data
dis = new DataInputStream(is);
int inputChar;
// read the web page content
// until it ends
while ( (inputChar = dis.read()) != -1)
{
// use buffer
buffer.append((char) inputChar);
}
// convert web page content
// to string by using getString
getString = new StringItem(null, buffer.toString());
// form string on mobile device
screen.append(getString);
// display string on screen
display.setCurrent(screen);
}
catch (IOException ioe) {
// report error
System.out.println(ioe.getMessage());
}
finally {
try {
// close I/O stream
if (dis != null)
dis.close();
}
catch (Exception ex) {}
try {
// close I/O stream
if (dos != null)
dos.close();
}
catch (Exception ex) {}
try {
// close o/p stream
if (os != null)
os.close();
}
catch (Exception ex) {}
try {
// close i/p stream
if (is != null)
is.close();
}
catch (Exception ex) {}
try {
// close stream connection
if (sc != null)
sc.close();
}
catch (Exception ex) {}
}
}
// pause MIDlet
public void pauseApp()
{ }
// release all MIDlet resources
public void destroyApp(boolean unconditional)
{
// clear objects value
buffer = null;
display = null;
screen = null;
getString = null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -