?? serverprocessor.java
字號(hào):
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 自動(dòng)生成方法存根
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[4];
//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 自動(dòng)生成方法存根
while(!isReady){
synchronized(sendMessages){
try{
byte[] temp=((String)sendMessages.elementAt(0)).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);
//println(sendMessages);
sendMessages.notify();
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -