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

📄 serverprocessor.java

📁 基于J2ME的一个可用的手机蓝牙程序
💻 JAVA
字号:

import java.io.*;
import java.util.Vector;

import javax.microedition.io.*;

public class ServerProcessor implements Runnable{

	private volatile boolean isReady;
	
	private StreamConnection conn;
	private Server server;
	private InputStream in;
	private OutputStream out;

	private Vector sendMessages=new Vector();
	
	
	public ServerProcessor(StreamConnection conn,Server server){
		this.conn=conn;
		this.server=server;
		
	}
	
	public synchronized void start(){
		new Thread(this).start();
	}
	
	public void run() {
		// TODO 自动生成方法存根
		try{
			in=conn.openInputStream();
			out=conn.openOutputStream();
			
			Write write=new Write();
			new Thread(write).start();
			server.open();
		}catch(Exception e){}
		
		while(!isReady){
			try{
				byte[] temp=new byte[20];
				System.out.println("=======================sever1");
				in.read(temp);
				server.read(temp);
			}catch(Exception e){
				close();
			}
			
		}
		
	}
	
	
    public void close(){
        if (!isReady){
            synchronized(this){
            	isReady = true;
            }

            synchronized(sendMessages){
                sendMessages.notify();
            }

            if (out != null){
                try{
                    out.close();
                    synchronized (this){
                        out = null;
                    }
                }
                catch(IOException e){}
            }

            if (in != null){
                try {
                    in.close();
                    synchronized (this){
                        in = null;
                    }
                }
                catch(IOException e){}
            }

            if (conn != null){
                try {
                	conn.close();
                    synchronized (this) {
                    	conn = null;
                    }
                }
                catch (IOException e){}
            }
        }
    }
    
    
	
	private class Write implements Runnable{
		public Write() {
		}

		public void run() {
			// TODO 自动生成方法存根
			while(!isReady){
				synchronized(sendMessages){
					try{
						byte[] temp=sendMessages.elementAt(0).toString().getBytes();
						out.write(temp);
						out.flush();
						sendMessages.removeElementAt(0);
					}catch(Exception e){
//						close();
					}
					if(sendMessages.size()==0){
						try{
							sendMessages.wait();
						}catch (InterruptedException ex){}
						
					}
					
				}
			}
			
		}
		
		
	}
	
	public void send(String data){
		synchronized(sendMessages){
			sendMessages.addElement(data);
			sendMessages.notify();
		}
	}

}

⌨️ 快捷键说明

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