⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socketclient.java

📁 Java手机程序开发-SocketClient1
💻 JAVA
字号:
import javax.microedition.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;

public class SocketClient extends MIDlet implements CommandListener{

	Display display;
	Form result;
	String serverURL = "socket://127.0.0.1:5678";
	String message;
	Command exitCmd;
	Command connectCmd;

	public SocketClient(){
		display = Display.getDisplay(this);
		result = new Form("取的信息");
		exitCmd = new Command("退出", Command.EXIT, 1);
		connectCmd = new Command("联机", Command.SCREEN, 1);
		result.addCommand(exitCmd);
		result.addCommand(connectCmd);
		result.setCommandListener(this);
	}

	public void startApp(){
		display.setCurrent(result);
	}

	public void pauseApp(){
	}

	public void destroyApp(boolean conditional){
	}

	public void commandAction(Command c, Displayable d){
		if( c == exitCmd){
			destroyApp(true);
			notifyDestroyed();
		}
		else if (c == connectCmd){
			try{
				connect(serverURL);
			}
			catch(Exception ex){}
		}
	}

	void connect(String url) throws IOException{
		StreamConnection con = (StreamConnection)Connector.open(url);
		InputStream ins = con.openInputStream();
		int data;
		StringBuffer sb = new StringBuffer();
		try{
			while((data = ins.read()) != -1){
				sb.append((char)data);
			}
			message = sb.toString();
			System.out.println(message);
			result.append(message);
			display.setCurrent(result);
		}
		finally{
			if(ins != null)
				ins.close();
			if(con != null)
				con.close();
		}
	}
}

⌨️ 快捷键说明

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