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

📄 client.java

📁 3G体验手机展示程序 3G体验手机展示程序 3G体验手机展示程序
💻 JAVA
字号:
package com.hongguan.lifeshowclient2;

import javax.microedition.midlet.*; 
import javax.microedition.io.*; 
import javax.microedition.lcdui.*; 
import java.io.*; 
import java.util.regex.*;

public class Client implements Runnable, CommandListener {

	private socketMIDlet parent; 
	private Display display; 
	private Form f; 
	private StringItem si; 
	private TextField tf;
	private boolean stop;
	private Command sendCommand = new Command("Send", Command.ITEM, 1);
	private Command exitCommand = new Command("Exit", Command.EXIT, 1);
	InputStream is;
	OutputStream os;
	SocketConnection sc;
	Sender sender;

	public Client(socketMIDlet m) { 
		parent = m; 
		display = Display.getDisplay(parent); 
		f = new Form("Socket Client"); 
		si = new StringItem("Status:", " "); 
		tf = new TextField("Send:", "", 30, TextField.ANY); 
		f.append(si); f.append(tf); 
		f.addCommand(exitCommand); 
		f.addCommand(sendCommand); 
		f.setCommandListener(this); 
		display.setCurrent(f); 
	} 
	public void start() { 
		Thread t = new Thread(this);
		t.start(); 
	} 

	public void run() { 
		try { 
			sc = (SocketConnection)Connector.open("socket://10.6.0.231:4321"); 
			si.setText("Connected to server"); 
			is = sc.openInputStream(); 
			os = sc.openOutputStream(); // Start the thread for sending messages - see Sender's main 
			sender = new Sender(os); // Loop forever, receiving data 
			while (true) {
				System.out.println("In run");
				StringBuffer sb = new StringBuffer(); 
				int c = 0; 
				while (((c = is.read()) != '\n') && (c!='\r') && (c != -1)) {
					sb.append((char) c); 
				}
				if (c == -1) { 
					break; 
				} 
				if(sb.toString()!=null&&!sb.toString().equalsIgnoreCase("")){
					String receive_message = sb.toString();
					System.out.println("Message received show first:"+receive_message);

					if(receive_message.equalsIgnoreCase("1")){
						System.out.println("00000000000000:"+receive_message);
						Welcome welcome = new Welcome("欢迎进入3G生活体验馆!");
						display.setCurrent(welcome);
					}else if(receive_message.equalsIgnoreCase("2")){
						MainMenu mainmenu = new MainMenu("欢迎进入3G生活体验馆主菜单!");
						display.setCurrent(mainmenu);
					}else {
						display.setCurrent(f);
						si.setText("Message received - " + receive_message+ " It's not a valid command!");
					}
				}

				/*Form form = new Form("欢迎图片");
				Image img = null;
				try {
					img = Image.createImage("/res/welcome.png");//这里的leaf不能太大.大了

					////就不能显示了.并且放在source folder下 一般名字为"res"
				} catch (IOException ex) {
					ex.printStackTrace();
				}
				form.append(img);
				display.setCurrent(form);*/
			}
			stop();
			//si.setText("Connection closed"); 
			f.removeCommand(sendCommand); 
		} catch (ConnectionNotFoundException cnfe) { 
			Alert a = new Alert("Client", "Please run Server MIDlet first", null, AlertType.ERROR); 
			a.setTimeout(Alert.FOREVER); 
			a.setCommandListener(this); 
			display.setCurrent(a); 
		} catch (IOException ioe) {
			System.out.println("The stop value is:"+stop);
			if (!stop) { 
				System.out.println("aa");
				ioe.printStackTrace(); 
			} 
		} catch (Exception e) {
			System.out.println("bb");
			e.printStackTrace(); 
		} 
	} 

	/*	public static void replaceBlank(String str){
	   Pattern p = Pattern.compile("\\s*|\t|\r|\n");
	   System.out.println("before:"+str);
	   Matcher m = p.matcher(str);
	   String after = m.replaceAll("");
	   System.out.println("after:"+after);
	}*/

	public void commandAction(Command c, Displayable s) { 
		if (c == sendCommand && !parent.isPaused()) { 
			sender.send(tf.getString()); 
		} if ((c == Alert.DISMISS_COMMAND) || (c == exitCommand)) { 
			parent.notifyDestroyed(); 
			parent.destroyApp(true); 
		} 
	} 

	/** * Close all open streams */ 
	public void stop() { 
		try { 
			stop = true; 
			if (sender != null) { 
//				sender.stop();
				sender.interrupt();
			} if (is != null) { 
				is.close(); 
			} if (os != null) { 
				os.close(); 
			} if (sc != null) { 
				sc.close(); 
			} 
		} catch (IOException ioe) {
			System.out.println("cc");
		} 
	} 
}

⌨️ 快捷键说明

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