欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

J2ME中多线程网络连接编程的分析,与设计。

程序员文章站 2022-05-29 08:27:48
...
注意。本文引用http://www.j2medev.com网上一些内容,只是出于文章的连贯性,请见谅
引言
 
  J2ME(Java 2 Micro Edition)是Java 2的一个组成部分,它与J2SE、J2EE并称。J2ME是一种高度优化的Java运行环境,主要针对消费类电子设备的,例如蜂窝电话、可视电话、数字机顶盒和汽车导航系统等等。即J2ME是为消费电子产品和手持设备量身定制的Java专用版本。
J2ME的出现使开发跨平台的消费类电子产品的应用软件成为可能。Java语言的与平台无关的特性移植到小型电子设备上,允许移动无线设备之间共享应用程序。它提供了基于HTTP的高级Internet协议,使移动电话能以Client/Server方式直接访问Internet的全部信息,从而使得不同的Client访问不同的资源。

  在将来的无线通信时代中,大量的网络应用程序将被开发出来去满足无线移动通讯的要求,而要充分的发挥无线移动通讯设备的通信能力,J2ME网络编程就变得尤为重要。那么为了高效地进行网络编程,就要利用Java语言的多线程编程机制。

下面我将给出代码来怎么才可以设计一个好的多线程连网。多线程在设计中的地位是很高的,也是比较难设计好的,没经验的人很容易就使程序产生死锁,崩溃等(在下现在还经常这样^_^)

我的例子是基于socket。
1。数据发送线程SenderTask,

package org.wuhua.net;

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

/**
* <b>类名:SenderTask.java</b> </br> 编写日期: 2006-7-16 <br/> 程序功能描述�
* 发��命令线程��如果OputStream 出现异常,则断开�有连�<br/> 断开 Demo: <br/> Bug: <br/>
*
* 程序变更日期 �<br/> 变更作� �<br/> 变更说明 �<br/>
*
* @author wuhua </br> <a href="mailto:rrq12345@163.com">rrq12345@163.com</a>
*/
public class SenderTask implements Runnable {

public OutputStream os;

private String message;

private boolean stop = false;

SenderTask(OutputStream os) {
this.os = os;

}

/**
* 发送指令,具体的指令格式,可以按照自己定义的规则。
*
* @param _msg
*/
public synchronized void send(String _msg) {
message = _msg;
notify(); // 执行运行
}

/**
* 执行,监听客户发送指令,如果指令不为null则工作�, 否则暂停工作直到有客户发送指令为�止才工作,
*/
public synchronized void run() {
try {
runImpl();
} catch (Throwable tw) {
tw.printStackTrace();

}
}

private void runImpl() throws IOException {
while (true) {
if (stop)
break;

// If no client to deal, wait until one connects
if (message == null) {
try {
wait();
} catch (Exception e) {

}
}
wirte();

}
}

/**
* 具体发�送指令的实现
*
* @throws IOException
*
*/
private void wirte() throws IOException {

os.write(message.getBytes());
os.flush();

message = null; // 指令为空,等待下一个指�,
}

public synchronized void stop() {
stop = true;
if (os != null) {
try {
os.close();
os = null;
} catch (IOException e) {

e.printStackTrace();
}
}

notify();

}

}


2。IncepterTask。负责接收从数据传送过来的数据,并且根据服务器要求,做自己的工作,代码:


package org.wuhua.net;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream; 
/**
 * <b>类名:CheckForm.java</b> </br> 编写日期: 2006-7-18 <br/>
 * 程序功能描述:检测从服务器上传过来的数据,并判断是否需要弹出窗口显示给用户,并更新相关的信息 <br/>
 * 此程序的操作网络的核心类,负责网络,跟一些消息��辑处理<br/> Demo: <br/>
 * 
 * Bug: <br/>
 * 
 * 程序变更日期 �<br/> 变更作� �<br/> 变更说明 �<br/>
 * 
 * @author wuhua </br> <a href="mailto:rrq12345@163.com">rrq12345@163.com</a>
 */
public class IncepterTask implements Runnable  {
	private DataInputStream in;
 	private boolean stop;
	private byte[] content = null;
	IncepterTask(InputStream _in) {
		in = new DataInputStream(_in);	
	}
	/**
	 * 程序启动后不停的获取数据,并根据数据的类型做相应的工作
	 */
	public synchronized void run() {
		try{	 
			runImpl();
		}catch(Throwable tw){
			tw.printStackTrace();
			 
		}
		
	}

	private void runImpl() throws IOException {
		while (true) {
			if (stop)
				break;
			
			while (true) {
				readMessage();

				if (content == null)
					continue;			
				
				executeIncepter( );
 
			}
		}
	}
	
	//获取数据后的具体工作
	private void executeIncepter() {
		// TODO Auto-generated method stub
		
	}
	/**
	 * 读取数据
	 * 
	 * @throws IOException
	 */
	private void readMessage() throws IOException {

		content = null;
		int length = 0;
		int offer = 0;

		// 如果没数据则阻塞在这�,等啊等啊等啊.
		length = in.readShort();

		if (length < 0)
			length = length + 65536;
		content = new byte[length];
		while (true) {
			int i = in.read(content, offer, length - offer);
			offer += i;
			if (offer == length)
				break;
		}
	}
 
	public synchronized void stop() {
		stop = true;
		if(in != null){
			try {
				in.close();
				in = null;
			} catch (IOException e) {
				 
				e.printStackTrace();
			}
		}
			
		notify();
		 
	}
}


1。Client类,负责打开连接以及管理,接收数据,与发送数据线程。
package org.wuhua.net;
import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.SocketConnection;
public class Client {
	static SocketConnection sc; 
	static IncepterTask it;
	static SenderTask st;
	
	static int restart;
	public static boolean IS_START = false;
 
	private Client() {
		try {
			sc = (SocketConnection) Connector.open(
					"socket://127。0。0。1:1232", 3, false); 
			
			
			it = new IncepterTask(sc.openInputStream());
			new Thread(it).start(); //启动拦截数据线程
			
			st = new SenderTask(sc.openOutputStream());
			new Thread(st).start();//启动数据发送线程
			IS_START = true;
			
		} catch (Exception e) {
			 
			IS_START = false;
			 
		}
	}
	
	public  static void start(){
		new Client();		
	}
	
	public static void stop() {
		try {
			if (sc != null)
				sc.close();
			if(it != null)
				it.stop();
			if(st != null)
				st.stop();
		} catch (IOException e) {
			 
		}
		sc = null;	
		st = null;
		it = null;
	}

	/**
	 * 发送指令
	 */

	public static void sendDictate(String type) {

		try {
			st.send(type);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}


以上是连接网络的基本框架,如果需要加些功能可以很好的利用这些代码