sockettest.java

来自「J2ME开发实例,内含有可运行的实例代码!」· Java 代码 · 共 51 行

JAVA
51
字号
//SocketTest.java
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.* ;
import java.io.*;
public class SocketTest extends MIDlet
{
	private Display display;
	public SocketTest()
	{
		display = Display.getDisplay(this);
	}
	public void startApp()
	{
		try
		{
			String url = "socket://127.0.0.1:80" ;
			String command = "GET /test.html HTTP/1.0 \n\n" ;
			//打开连接
			StreamConnection sc = (StreamConnection)
				Connector.open(url) ;
			//发送请求
			DataOutputStream dos =
				new DataOutputStream(sc.openOutputStream()) ;
			byte bytes[] = command.getBytes() ;
			dos.write(bytes,0,bytes.length) ;
			dos.flush() ;
			//取得回应
			DataInputStream dis =
				new DataInputStream(sc.openInputStream()) ;
			String content = "" ;
			int ic ;
			while( (ic = dis.read()) != -1 )
			{
				content = content + (char)ic ;
			}
			Form f = new Form("Socket Test");
			f.append(content) ;
			display.setCurrent(f) ;
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage()) ;
			notifyDestroyed() ;
		}
	}
	public void pauseApp()
	{ }
	public void destroyApp(boolean unconditional)
	{}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?