您的位置:首页 > 其它

蓝牙休闲娱乐平台(毕业设计)第五部份

2008-03-08 22:19 381 查看



/** *//**


*


* @author 梧州学院 04计本2班 罗功武


* date: 08/02/27


*/




package chat;




import java.io.DataInputStream;


import java.io.DataOutputStream;


import java.io.IOException;


import javax.bluetooth.BluetoothStateException;


import javax.bluetooth.DataElement;


import javax.bluetooth.DiscoveryAgent;


import javax.bluetooth.LocalDevice;


import javax.bluetooth.ServiceRecord;


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.Form;


import javax.microedition.lcdui.StringItem;


import javax.microedition.lcdui.TextField;


import javax.microedition.lcdui.Ticker;


import flat.FlatMainMIDlet;




/** *//**


* PairChatChatRoomServer 类


* 双人聊天室的服务器类


* 新健一个双人聊天室,并等待用户的链接.


* 采用多线程的技术,服务端主线程,发送数据线程,


* 两种线程维护聊天室的运行


*/




public class PairChatRoomServer extends Form implements Runnable, CommandListener ...{




/**//*用户召娱乐妮称*/


private String userName;




/**//*客服端用户娱乐妮称*/


private String friendName;




/**//*娱乐平台主类*/


private FlatMainMIDlet flatMainMIDlet;




/**//*窗口滚动条*/


private static final Ticker ticker = new Ticker("我的聊天室,我做主^_^");


//用于输入要发送的消息


private TextField tfData = new TextField("请输入发送的消息",


"",255,TextField.ANY);




private static final Command cmdExit = new Command("退出", Command.EXIT, 0);


private static final Command cmdSend = new Command("发送", Command.SCREEN, 1);






/**//*线程运行标志*/


private boolean isRunning = true;




/**//*客户端是否退出*/


private boolean isNoExit = true;


//是否是第一次接收数据


private boolean isFirstAccept = true;


//服务链接器


StreamConnectionNotifier server = null;


//服务器服务记录


ServiceRecord record=null;


//数据输入流


private DataInputStream dis = null;


//数据输出流


private DataOutputStream dos = null;




/** *//**


* 构造方法


* 完成双人聊天室窗口的初使化


* @param midlet


* @param mainMenu


* @param name


*/




public PairChatRoomServer(FlatMainMIDlet midlet, String name) ...{




super("双人聊天室");


this.flatMainMIDlet = midlet;


this.userName = name;


setTicker(ticker);


append(tfData);


addCommand(cmdExit);


addCommand(cmdSend);


setCommandListener(this);


//开始服务器线程


new Thread(this).start();


}






/** *//**


* 处理命令按钮事件


*/




public void commandAction(Command cmd, Displayable d) ...{




if (cmd == cmdExit) ...{




/**//*发送退出信息*/


String str = "exit";


new sendThread(str).start();


isRunning = false;




try ...{


Thread.sleep(1000);




} catch (InterruptedException e) ...{




}


close();


flatMainMIDlet.showFlatMenu();




} else if(cmd == cmdSend) ...{


String str = tfData.getString();


//显示发送的消息


insert(size()-1,new StringItem("","我说: " + str + " "));




/**//*发送数据,新建发送数据线程*/


new sendThread(str).start();


tfData.setString("");


}


}






/** *//**


* 服务器线程


*/




public void run() ...{




isRunning = true;


LocalDevice local = null;




try ...{




/**//*设置设备的发现模式,使设备处可发现*/


local = LocalDevice.getLocalDevice();


local.setDiscoverable(DiscoveryAgent.GIAC);




} catch (BluetoothStateException e) ...{


return;


}




try ...{




/**//*开启服务*/


server = (StreamConnectionNotifier)Connector.open(


"btspp://localhost:11111111111111111111111111111111");




/**//*服务器服务记录*/


ServiceRecord record = local.getRecord(server);




/**//*自定义服务记录属性*/


DataElement elm = null;


elm = new DataElement(DataElement.STRING, userName);


record.setAttributeValue(0x1234, elm);


elm = null;


elm = new DataElement(DataElement.STRING, "SCHOLAR_II");


record.setAttributeValue(0x1235, elm);




/**//*更新服务记录属性*/


local.updateRecord(record);




} catch (IOException e) ...{


return;


}


String message = "";




/**//*循环的为客户端服务*/




while(isRunning) ...{


isNoExit = true;


isFirstAccept = true;




try ...{


local.setDiscoverable(DiscoveryAgent.GIAC);




} catch (BluetoothStateException e1) ...{


// TODO Auto-generated catch block


return;


}


StreamConnection conn = null;




try ...{




try ...{




/**//*等待客户端的链接*/


conn = server.acceptAndOpen();




/**//*客户端链接后,关闭发现模式*/


local.setDiscoverable(DiscoveryAgent.NOT_DISCOVERABLE);




} catch (IOException e) ...{


return;


}




/**//*获得输入输出流*/


dis = conn.openDataInputStream();


dos = conn.openDataOutputStream();




/**//*接收数据*/




while(isRunning&&isNoExit) ...{




/**//*等待用户发送数据*/


message = dis.readUTF();


if(isFirstAccept)




...{




/**//*第一接收客户端的数据,客户端的用户名*/


insert(size()-1,new StringItem("","^_^欢迎"


+ message + "进入我的聊天室" + " "));


friendName = message;


isFirstAccept = false;


}


else




...{ /**//*用户退出*/


if(message.equals("exit"))




...{


insert(size()-1,new StringItem("",


friendName + "退出我的聊天室" + " "));


isNoExit = false;


dos=null;


}


else




...{


insert(size()-1,new StringItem("",friendName + "说: "


+ message + " "));


}




}




/**//*线程暂停*/


Thread.sleep(1000);


}






} catch (Exception e) ...{




} finally ...{




if (conn!=null) ...{




try ...{


conn.close();


conn = null;




}catch (Exception e) ...{


}


}


}


}


close();




/**//*结束聊天*/


}






/** *//**


* 关闭连接


*/




public void close() ...{




try ...{




/**//*关闭输入流*/


if (dis != null)




...{


dis.close();


dis = null;


}




/**//*关闭输出流*/


if (dos != null)




...{


dos.close();


dos = null;


}




/**//*关闭服务器*/


if (server != null)




...{


server.close();


server = null;


}




} catch (Exception e) ...{


}


}




/**//*内部类-发送数据的线程*/




class sendThread extends Thread...{


private String data = null;




public sendThread(String data)...{


this.data = data;


}




public void run()...{




if (dos == null) ...{


return;


}




try ...{


dos.writeUTF(data);


dos.flush();




} catch (IOException e) ...{


return;


}


}


}


}//PairChatRoomServer.java文件结束






/** *//**


* Copyright@scholar_ii


* @author 梧州学院 04计本2班 罗功武


* date 08/02/27


*/


package chat;




import java.io.DataInputStream;


import java.io.DataOutputStream;


import java.io.IOException;


import javax.microedition.io.Connector;


import javax.microedition.io.StreamConnection;


import javax.microedition.lcdui.Command;


import javax.microedition.lcdui.CommandListener;


import javax.microedition.lcdui.Displayable;


import javax.microedition.lcdui.Form;


import javax.microedition.lcdui.StringItem;


import javax.microedition.lcdui.TextField;


import javax.microedition.lcdui.Ticker;


import flat.FlatMainMIDlet;




/** *//**


* PairChatChatRoomClient 类


* 双人聊天室的客户端类


* 新健一个双人聊天室,并链双人聊天室接服务器


* 采用多线程的技术,客户端主线程,发送数据线程,


* 两种线程维护聊天室的运行


*/


public class PairChatRoomClient extends Form implements




Runnable, CommandListener ...{




/**//*用户娱乐妮称*/


private String userName = null;




/**//*服务器用户的娱妮称*/


private String friendName = null;




/**//*娱乐平台的主类*/


private FlatMainMIDlet flatMainMIDlet = null;




/**//*滚动条*/


static final Ticker ticker = new Ticker("我的聊天室,我做主^_^");




/**//*用于输入要发送的消息*/


private TextField tfData = new TextField("请输入发送的消息",


"",255,TextField.ANY);




private static final Command cmdExit = new Command("退出", Command.EXIT, 0);


private static final Command cmdSend = new Command("发送", Command.SCREEN, 1);






/**//*线程运行标志*/


private boolean isRunning = true;




/**//*链接字符串*/


private String connectURL = null;




/**//*链接流*/


private StreamConnection client = null;




/**//*数据输入流*/


private DataInputStream dis = null;




/**//*数据输出流*/


private DataOutputStream dos = null;




/** *//**


* 构造方法


* 完成客户端聊天室窗口的初使化


* @param connectURL


* @param flatMainMidlet


* @param chatRoomList


* @param userName


* @param friendName


*/


public PairChatRoomClient(String connectURL, FlatMainMIDlet flatMainMidlet,




String userName, String friendName)...{




super("双人聊天室");


this.connectURL = connectURL;


this.flatMainMIDlet = flatMainMidlet;


this.userName = userName;


this.friendName = friendName;


setTicker(ticker);


append(tfData);


addCommand(cmdExit);


addCommand(cmdSend);


setCommandListener(this);


//开始服务器线程


new Thread(this).start();


}




public void commandAction(Command command, Displayable displayable) ...{




if (command == cmdExit) ...{




/**//*发送退出信息*/




new Thread() ...{




public void run() ...{




if (dos == null) ...{


return;


}


String str = "exit";




try ...{


dos.writeUTF(str);


dos.flush();




} catch (IOException e) ...{


return;


}


}


}.start();


isRunning = false;




/**//*线程暂停*/




try ...{


Thread.sleep(1000);




} catch (InterruptedException e) ...{


return;


}


close();


flatMainMIDlet.showFlatMenu();




} else if(command == cmdSend) ...{


String str = tfData.getString();




/**//*显示发送的消息*/


insert(size()-1, new StringItem("","我说: " + str + " "));




/**//*发送数据的线程*/


new sendThread(str).start();


tfData.setString("");




}




}




/** *//**


* 客户端聊天线程(接收数据线程)


*/




public void run() ...{




isRunning = true;




try ...{




/**//*链接服务器*/


client = (StreamConnection)Connector.open(connectURL);




/**//*获得输入输出流*/


dis = client.openDataInputStream();


dos = client.openDataOutputStream();






} catch (Exception e) ...{


return;


}


insert(size()-1, new StringItem("","^_^进入" +


friendName + "的聊天室" + " "));




/**//*自动发客服端的用户名给服务器*/




/**//*把自己的用户名发送到服务器*/


String str = userName;


new sendThread(str).start();




/**//*接收数据*/




while(isRunning) ...{




try ...{


String message = dis.readUTF();


if(message.equals("exit"))




...{


insert(size()-1, new StringItem("",friendName +


"离开了聊天室"+ " "));


close();


}


else




...{


insert(size()-1, new StringItem("",friendName +


"说: " + message + " "));


}




Thread.sleep(1000);




} catch (Exception e) ...{


e.printStackTrace();


break;


}




/**//*线程暂停*/




try ...{


Thread.sleep(1000);




} catch (InterruptedException e) ...{


}


}


close();




}






/** *//**


* 关闭连接


*/




public void close() ...{




try ...{




/**//*关闭输入流*/


if (dis != null)




...{


dis.close();


dis = null;


}




/**//*关闭输出流*/


if (dos != null)




...{


dos.close();


dos = null;


}




/**//*关闭客户端链接流*/


if (client != null)




...{


client.close();


client = null;


}




} catch (Exception e) ...{


}


}






/**//*内部类-发送数据的线程*/




class sendThread extends Thread...{


private String data = null;




public sendThread(String data)...{


this.data = data;


}




public void run()...{




if (dos == null) ...{


return;


}




try ...{


dos.writeUTF(data);


dos.flush();




} catch (IOException e) ...{


return;


}


}


}


}//PairChatRoomClient.java文件结束

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息