?? server.java
字號(hào):
package bluetoochgame;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.*;
import java.io.IOException;
import java.util.Vector;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
public class Server implements Runnable{
Connection connection;
StreamConnection conn1 = null;
LocalDevice local = null;
ClientProcessor processor;
DataOutputStream dos;
boolean isClosed = false;
boolean isOut = false;
StreamConnectionNotifier notifier;
String messagex = null,messagey=null;
String connectionURL =
"btspp://localhost:F0E0D0C0B0A000908070605040302010;"
+ "authenticate=false;encrypt=false;name=RFCOMM Server"; //UUID is 393a84ee7cd111d89527000bdb544cb1
Vector queue = new Vector();
public Server(Connection connection) {
this.connection = connection;
}
public void start()
{
Thread t = new Thread(this);
t.start() ;
}
public void run()
{
try {
local = LocalDevice.getLocalDevice();
local.setDiscoverable(DiscoveryAgent.GIAC);
} catch (Exception e) {}
try {
notifier = (StreamConnectionNotifier)Connector.open(connectionURL);
System.out.println("server is opening") ;
} catch (IOException e1) {}
processor = new ClientProcessor();
// ok, start accepting connections then
while (!isClosed) {
StreamConnection conn = null;
try {
conn = notifier.acceptAndOpen();
} catch (IOException e) {
// wrong client or interrupted - continue anyway
continue;
}
processor.addConnection(conn);
}
}//end run
public void sendMessage(String message1,String message2)
{
messagex = message1;
messagey = message2;
try {
// DataOutputStream dos = conn.openDataOutputStream();
dos.writeUTF(messagex);
System.out.println("afsfas");
dos.writeUTF(messagey) ;
dos.flush();
dos.close();
} catch (IOException e) {
}
}
private class ClientProcessor implements Runnable {
private Thread processorThread;
private Vector queue = new Vector();
private boolean isOk = true;
ClientProcessor() {
processorThread = new Thread(this);
processorThread.start();
}
public void run() {
while (!isClosed) {
synchronized (this) {
if (queue.size() == 0) {
try {
//阻塞,直到有新客戶連接
wait();
} catch (InterruptedException e) {
}
}
}
//處理連接隊(duì)列
StreamConnection conn;
synchronized (this) {
if (isClosed) {
return;
}
conn = (StreamConnection) queue.firstElement();
queue.removeElementAt(0);
processConnection(conn);
}
}
}
/**
* 往連接隊(duì)列添加新連接,同時(shí)喚醒處理線程
*/
void addConnection(StreamConnection conn) {
synchronized (this) {
queue.addElement(conn);
notify();
}
}
} //end processor
private void readInputString(StreamConnection conn) {
String message1,message2 = null;
try {
DataInputStream dis = conn.openDataInputStream();
message1 = dis.readUTF();
message2 = dis.readUTF() ;
dis.close();
connection.receiveMessage(message1,message2) ;
} catch (Exception e) {
e.printStackTrace();
System.out.println("yi chang") ;
}
}
private void processConnection(StreamConnection conn) {
try{
dos = conn.openDataOutputStream();
}
catch(Exception e){}// 讀取輸入
readInputString(conn);
//輸出響應(yīng)
// sendOutputData(messagex,messagey, conn);
}
/*
private void sendOutputData(String messagex,String messagey, StreamConnection conn) {
try {
DataOutputStream dos = conn.openDataOutputStream();
dos.writeUTF(messagex);
dos.writeUTF(messagey) ;
dos.flush();
dos.close();
} catch (IOException e) {
}
}*/
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -