public static void handleSelectionKey(SelectionKey selectionKey)
throws Exception {
if (selectionKey.isAcceptable()) { // 有客户端进来
clientCount++; ServerSocketChannel serverSocketChannel = (ServerSocketChannel) selectionKey
.channel();
SocketChannel socketChannel = serverSocketChannel.accept();
socketChannel.configureBlocking(false);
Socket socket = socketChannel.socket(); // 立即注册一个 OP_READ 的SelectionKey, 接收客户端的消息
SelectionKey key = socketChannel.register(selector,
SelectionKey.OP_READ);
key.attach("第 " + clientCount + " 个客户端 ["
+ socket.getRemoteSocketAddress() + "]: "); p(key.attachment()
+ "\t[接入] ========================================="); } else if (selectionKey.isReadable()) { ByteBuffer byteBuffer=ByteBuffer.allocate(100);
SocketChannel socketChannel=(SocketChannel)selectionKey.channel();
try
{
int len=socketChannel.read(byteBuffer);
String newMsg="";
// 如果len>0,表示有输入。如果len==0, 表示输入结束。需要关闭
// socketChannel
if (len>0)
{
byteBuffer.flip();
String msg=charsetDecoder.decode(byteBuffer).toString();
if (msg.substring(0, 3).equals(":5E")) {
WriteIpPort.saveip(socketChannel.socket().getInetAddress()
.toString(), socketChannel.socket().getPort(), msg
.substring(3, msg.length()), 1);
newMsg = "Start data transfer.";
if(!checksocketbyselectkey(selectionKey,msg.substring(3, msg.length()),socketChannel.socket().getInetAddress()
.toString(),socketChannel.socket().getPort())){
userlist alist = new userlist();
alist.setSelectionKey(selectionKey);
alist.setTno(msg.substring(3, msg.length()));
alist.setSocketChannel(socketChannel);
alist.setMyip(socketChannel.socket().getInetAddress()
.toString());
alist.setMyport(socketChannel.socket().getPort());
list.add(alist);
//whilelist();
}
else
{
if(checksocketlist(msg.substring(3, msg.length()))){
removelist(Getsocket(msg.substring(3, msg.length())));
userlist alist = new userlist();
alist.setSelectionKey(selectionKey);
alist.setTno(msg.substring(3, msg.length()));
alist.setSocketChannel(socketChannel);
alist.setMyip(socketChannel.socket().getInetAddress()
.toString());
alist.setMyport(socketChannel.socket().getPort());
list.add(alist);
//whilelist();
}
else
{
userlist alist = new userlist();
alist.setSelectionKey(selectionKey);
alist.setTno(msg.substring(3, msg.length()));
alist.setSocketChannel(socketChannel);
alist.setMyip(socketChannel.socket().getInetAddress()
.toString());
alist.setMyport(socketChannel.socket().getPort());
list.add(alist);
//whilelist();
}
}
}
if (msg.substring(0, 3).equals(":50")) {
Getdata.getdata1(msg);
newMsg = "Stop data transfer.";
if(!checksocketbyselectkey(selectionKey,msg.split(",")[8],socketChannel.socket().getInetAddress()
.toString(),socketChannel.socket().getPort())){
userlist alist = new userlist();
alist.setSelectionKey(selectionKey);
alist.setTno(msg.split(",")[8]);
alist.setSocketChannel(socketChannel);
alist.setMyip(socketChannel.socket().getInetAddress()
.toString());
alist.setMyport(socketChannel.socket().getPort());
list.add(alist);
//whilelist();
}
else
{
if(checksocketlist(msg.split(",")[8])){
removelist(Getsocket(msg.split(",")[8]));
userlist alist = new userlist();
alist.setSelectionKey(selectionKey);
alist.setTno(msg.split(",")[8]);
alist.setSocketChannel(socketChannel);
alist.setMyip(socketChannel.socket().getInetAddress()
.toString());
alist.setMyport(socketChannel.socket().getPort());
list.add(alist);
//whilelist();
}
else
{
userlist alist = new userlist();
alist.setSelectionKey(selectionKey);
alist.setTno(msg.split(",")[8]);
alist.setSocketChannel(socketChannel);
alist.setMyip(socketChannel.socket().getInetAddress()
.toString());
alist.setMyport(socketChannel.socket().getPort());
list.add(alist);
//whilelist();
}
}
}
if (msg.substring(0, 3).equals(":5F")) {
Getdata.getdata1(msg);
newMsg = "Stop data transfer.";
if(!checksocketbyselectkey(selectionKey,msg.split(",")[8],socketChannel.socket().getInetAddress()
.toString(),socketChannel.socket().getPort())){
userlist alist = new userlist();
alist.setSelectionKey(selectionKey);
alist.setTno(msg.split(",")[8]);
alist.setSocketChannel(socketChannel);
alist.setMyip(socketChannel.socket().getInetAddress()
.toString());
alist.setMyport(socketChannel.socket().getPort());
list.add(alist);
//whilelist();
}
else
{
if(checksocketlist(msg.split(",")[8])){
removelist(Getsocket(msg.split(",")[8]));
userlist alist = new userlist();
alist.setSelectionKey(selectionKey);
alist.setTno(msg.split(",")[8]);
alist.setSocketChannel(socketChannel);
alist.setMyip(socketChannel.socket().getInetAddress()
.toString());
alist.setMyport(socketChannel.socket().getPort());
list.add(alist);
//whilelist();
}
else
{
userlist alist = new userlist();
alist.setSelectionKey(selectionKey);
alist.setTno(msg.split(",")[8]);
alist.setSocketChannel(socketChannel);
alist.setMyip(socketChannel.socket().getInetAddress()
.toString());
alist.setMyport(socketChannel.socket().getPort());
list.add(alist);
//whilelist();
}
}
} if (newMsg==null)
newMsg="Sorry? I don't understand your message. ";
// UTF-8 格式输出到客户端,并输出一个'n'
socketChannel.write(charsetEncoder.encode(CharBuffer.wrap(newMsg
+"\n")));
p(selectionKey.attachment()
+"\t[recieved]: "
+msg
+" ----->\t[send]: "
+newMsg);
} else
{
// 输入结束,关闭 socketChannel
p(selectionKey.attachment()
+"read finished. close socketChannel. ");
removelist(selectionKey);
socketChannel.close();
clientCount--;
}
} catch (Exception e)
{
// 如果read抛出异常,表示连接异常中断,需要关闭
// socketChannel
// e.printStackTrace();
removelist(selectionKey);
p(selectionKey.attachment()
+"第一个客户端关闭了 ");
socketChannel.close();
clientCount--;
}
} else
if (selectionKey.isWritable())
{
p(selectionKey.attachment()
+"TODO: isWritable() ???????????????????????????? ");
} else
if (selectionKey.isConnectable())
{
p(selectionKey.attachment()
+"TODO: isConnectable() ????????????????????????? ");
} else
{
p(selectionKey.attachment()
+"TODO: else. ");
}
}是这样的,客户端能够长连接,当客户端直接断电时,服务器不能检测到抛出的异常,就是服务器认为客户端还是连上的,并且要在4、5分钟后才能检测都客户单断开了,如果客户端是正常的断开,服务器马上就能检测到。这个问题怎么解决啊
throws Exception {
if (selectionKey.isAcceptable()) { // 有客户端进来
clientCount++; ServerSocketChannel serverSocketChannel = (ServerSocketChannel) selectionKey
.channel();
SocketChannel socketChannel = serverSocketChannel.accept();
socketChannel.configureBlocking(false);
Socket socket = socketChannel.socket(); // 立即注册一个 OP_READ 的SelectionKey, 接收客户端的消息
SelectionKey key = socketChannel.register(selector,
SelectionKey.OP_READ);
key.attach("第 " + clientCount + " 个客户端 ["
+ socket.getRemoteSocketAddress() + "]: "); p(key.attachment()
+ "\t[接入] ========================================="); } else if (selectionKey.isReadable()) { ByteBuffer byteBuffer=ByteBuffer.allocate(100);
SocketChannel socketChannel=(SocketChannel)selectionKey.channel();
try
{
int len=socketChannel.read(byteBuffer);
String newMsg="";
// 如果len>0,表示有输入。如果len==0, 表示输入结束。需要关闭
// socketChannel
if (len>0)
{
byteBuffer.flip();
String msg=charsetDecoder.decode(byteBuffer).toString();
if (msg.substring(0, 3).equals(":5E")) {
WriteIpPort.saveip(socketChannel.socket().getInetAddress()
.toString(), socketChannel.socket().getPort(), msg
.substring(3, msg.length()), 1);
newMsg = "Start data transfer.";
if(!checksocketbyselectkey(selectionKey,msg.substring(3, msg.length()),socketChannel.socket().getInetAddress()
.toString(),socketChannel.socket().getPort())){
userlist alist = new userlist();
alist.setSelectionKey(selectionKey);
alist.setTno(msg.substring(3, msg.length()));
alist.setSocketChannel(socketChannel);
alist.setMyip(socketChannel.socket().getInetAddress()
.toString());
alist.setMyport(socketChannel.socket().getPort());
list.add(alist);
//whilelist();
}
else
{
if(checksocketlist(msg.substring(3, msg.length()))){
removelist(Getsocket(msg.substring(3, msg.length())));
userlist alist = new userlist();
alist.setSelectionKey(selectionKey);
alist.setTno(msg.substring(3, msg.length()));
alist.setSocketChannel(socketChannel);
alist.setMyip(socketChannel.socket().getInetAddress()
.toString());
alist.setMyport(socketChannel.socket().getPort());
list.add(alist);
//whilelist();
}
else
{
userlist alist = new userlist();
alist.setSelectionKey(selectionKey);
alist.setTno(msg.substring(3, msg.length()));
alist.setSocketChannel(socketChannel);
alist.setMyip(socketChannel.socket().getInetAddress()
.toString());
alist.setMyport(socketChannel.socket().getPort());
list.add(alist);
//whilelist();
}
}
}
if (msg.substring(0, 3).equals(":50")) {
Getdata.getdata1(msg);
newMsg = "Stop data transfer.";
if(!checksocketbyselectkey(selectionKey,msg.split(",")[8],socketChannel.socket().getInetAddress()
.toString(),socketChannel.socket().getPort())){
userlist alist = new userlist();
alist.setSelectionKey(selectionKey);
alist.setTno(msg.split(",")[8]);
alist.setSocketChannel(socketChannel);
alist.setMyip(socketChannel.socket().getInetAddress()
.toString());
alist.setMyport(socketChannel.socket().getPort());
list.add(alist);
//whilelist();
}
else
{
if(checksocketlist(msg.split(",")[8])){
removelist(Getsocket(msg.split(",")[8]));
userlist alist = new userlist();
alist.setSelectionKey(selectionKey);
alist.setTno(msg.split(",")[8]);
alist.setSocketChannel(socketChannel);
alist.setMyip(socketChannel.socket().getInetAddress()
.toString());
alist.setMyport(socketChannel.socket().getPort());
list.add(alist);
//whilelist();
}
else
{
userlist alist = new userlist();
alist.setSelectionKey(selectionKey);
alist.setTno(msg.split(",")[8]);
alist.setSocketChannel(socketChannel);
alist.setMyip(socketChannel.socket().getInetAddress()
.toString());
alist.setMyport(socketChannel.socket().getPort());
list.add(alist);
//whilelist();
}
}
}
if (msg.substring(0, 3).equals(":5F")) {
Getdata.getdata1(msg);
newMsg = "Stop data transfer.";
if(!checksocketbyselectkey(selectionKey,msg.split(",")[8],socketChannel.socket().getInetAddress()
.toString(),socketChannel.socket().getPort())){
userlist alist = new userlist();
alist.setSelectionKey(selectionKey);
alist.setTno(msg.split(",")[8]);
alist.setSocketChannel(socketChannel);
alist.setMyip(socketChannel.socket().getInetAddress()
.toString());
alist.setMyport(socketChannel.socket().getPort());
list.add(alist);
//whilelist();
}
else
{
if(checksocketlist(msg.split(",")[8])){
removelist(Getsocket(msg.split(",")[8]));
userlist alist = new userlist();
alist.setSelectionKey(selectionKey);
alist.setTno(msg.split(",")[8]);
alist.setSocketChannel(socketChannel);
alist.setMyip(socketChannel.socket().getInetAddress()
.toString());
alist.setMyport(socketChannel.socket().getPort());
list.add(alist);
//whilelist();
}
else
{
userlist alist = new userlist();
alist.setSelectionKey(selectionKey);
alist.setTno(msg.split(",")[8]);
alist.setSocketChannel(socketChannel);
alist.setMyip(socketChannel.socket().getInetAddress()
.toString());
alist.setMyport(socketChannel.socket().getPort());
list.add(alist);
//whilelist();
}
}
} if (newMsg==null)
newMsg="Sorry? I don't understand your message. ";
// UTF-8 格式输出到客户端,并输出一个'n'
socketChannel.write(charsetEncoder.encode(CharBuffer.wrap(newMsg
+"\n")));
p(selectionKey.attachment()
+"\t[recieved]: "
+msg
+" ----->\t[send]: "
+newMsg);
} else
{
// 输入结束,关闭 socketChannel
p(selectionKey.attachment()
+"read finished. close socketChannel. ");
removelist(selectionKey);
socketChannel.close();
clientCount--;
}
} catch (Exception e)
{
// 如果read抛出异常,表示连接异常中断,需要关闭
// socketChannel
// e.printStackTrace();
removelist(selectionKey);
p(selectionKey.attachment()
+"第一个客户端关闭了 ");
socketChannel.close();
clientCount--;
}
} else
if (selectionKey.isWritable())
{
p(selectionKey.attachment()
+"TODO: isWritable() ???????????????????????????? ");
} else
if (selectionKey.isConnectable())
{
p(selectionKey.attachment()
+"TODO: isConnectable() ????????????????????????? ");
} else
{
p(selectionKey.attachment()
+"TODO: else. ");
}
}是这样的,客户端能够长连接,当客户端直接断电时,服务器不能检测到抛出的异常,就是服务器认为客户端还是连上的,并且要在4、5分钟后才能检测都客户单断开了,如果客户端是正常的断开,服务器马上就能检测到。这个问题怎么解决啊
解决方案 »
- 请教使用aptana的高手帮下忙~~~~~
- JSP图片路径问题
- 我怎样才能获得刚刚插入语句的ID值?
- Hibernate+Sturts+Spring 问题 急!!!!!!请各位帮帮忙 小弟感激不尽
- 怎样在Servlet中根据某一session id得到该session对象???
- 我写了个FILTER,但总是重复加载页面!!
- 怎样判断一个游客已经离开(就是已经关掉我的网页)
- 概念不明,高手赐教。ant,Catalina,struts,JSF
- jsp文件运行后出现HTTP Status 500 - 是为什么?
- 关于导航栏下拉列表在IE中不显示的问题
- 我的jdk以前安装的是1.6。现在我又装了一个1.5.我把环境变量都改成1.5的路径了。为什么java -version还是显示1.6呀。
- 为什么Web客户端浏览器无法申请到一个JSON文件的URI?
len > 0 网络正常,且收到数据;
len = 0 网络正常,没有收到数据;
len < 0 网络已中断