sender.java

来自「j2me学习 简单例子」· Java 代码 · 共 59 行

JAVA
59
字号
package example.demowireless.socket;

import java.io.IOException;
import java.io.OutputStream;

/**
 * <p>
 * J2ME Socket Demo -- Client
 * </p>
 * 
 * @author 刘光辉
 * @version 2009.02.04
 */
public class Sender extends Thread {

	private OutputStream os;
	private String message;

	public Sender(OutputStream os) {
		this.os = os;
		start();
	}

	public synchronized void send(String msg) {
		message = msg;
		notify();
	}

	public synchronized void run() {
		while (true) {
			// If no client to deal, wait until one connects
			if (message == null) {
				try {
					wait();
				} catch (InterruptedException e) {
				}
			}
			if (message == null) {
				break;
			}
			try {
				os.write(message.getBytes());
				os.write("\r\n".getBytes());
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}
			// Completed client handling, return handler to pool and
			// mark for wait
			message = null;
		}
	}

	public synchronized void stop() {
		message = null;
		notify();
	}

}

⌨️ 快捷键说明

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