急救!!!j2me 服务器
这段代码怎么改就又可以群聊又可以私聊
这个是实现私聊的
public void sendMessage()
{
String name=null;
if(vectorOn.size()==0)
return;
for(int i=0;i<vectorOn.size();i++){
try {
OnlineClient oc=(OnlineClient)vectorOn.elementAt(i);
if(oc.isQuit==true||oc.dis.available()==0)
continue;
style=(byte)oc.dis.read();
if(style==SYTLE_MESSAGE)
{
name=oc.dis.readUTF();
String message=null;
try {
message = oc.dis.readUTF();
} catch (IOException e1) {
System.out.println("chatthread erroer 5");
e1.printStackTrace();
}
for(int j=0;j<vectorOn.size();j++){
OnlineClient oct=(OnlineClient)vectorOn.elementAt(j);
if(oct.neckName.equals(name))
{
if(oct.isQuit==false)
{
try {
oct.dos.writeByte(SYTLE_MESSAGE);
oct.dos.writeUTF(oc.neckName);
System.out.println(oc.neckName);
oct.dos.writeUTF(message);
} catch (IOException e1) {
e1.printStackTrace();
System.out.println("chatthread errror 6");
}
}
}
}
}
else if(style==SYTLE_QUIT)
{
if(oc.Quit()==false)
{
oc.isQuit=true;
oc.dos.writeByte(QUIT_OK);
}
}
style=-1;
} catch (IOException e1) {
System.out.println("接收消息失败");
}
}
}
[解决办法]
可不可以只加这一句
oct.dos.writeUTF(oc.neckName);
System.out.println(oc.neckName);
oct.dos.writeUTF(message);
} catch (IOException e1) {
e1.printStackTrace();
System.out.println("chatthread errror 6");
}
}
}
else {
oct.dos.writeUTF(message);
}
}
}
表示不匹配好友直接将信息发送给所有好友
[解决办法]
改成多线程处理。具体可以看郭克华的视频,里面有聊天室的开发示例。