我最近做个采集服务端。要用到长连接。这里我用到这样的长连接。不过,如果是多条连接,系统会崩溃了。
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.LinkedList;
import java.nio.channels.SocketChannel;
import java.nio.channels.SelectionKey;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.util.Date;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;崩溃
import java.io.IOException;//import lon.ChannelState;/**
* <p>
* Title: ���߳�
* </p>
* <p>
* Description: ���߳����ڶ�ȡ�ͻ������
* </p>
*
* @author starboy
* @version 1.0
*/public class Reader extends Thread { private static int timeOut = 10000;
CharsetEncoder encoder = Charset.forName("GB2312").newEncoder();
private static List pool = new LinkedList();
private static Notifier notifier = Notifier.getNotifier();
private ByteBuffer m_buffer = ByteBuffer.allocateDirect(1024);
HashMap map = new HashMap();
SelectionKey key = null;
static int b=1;
private static int BUFFER_SIZE = 1024;
static int i; public Reader(SelectionKey key) {
this.key = key;
}
public void run() {
//CountAccuMess map = new Countmap();
String tempStr=null;
Request request =(Request)key.attachment();
StringBuffer sBuffer = null;
out: for (;;) {
m_buffer.clear();
int count = 0;
SocketChannel channel = (SocketChannel) key.channel();
long start=System.currentTimeMillis();
if(channel.socket().isClosed()==false){
try{
while((count = channel.read(m_buffer))==0){
if((System.currentTimeMillis()-start)==timeOut){
map.remove(key.channel().hashCode());
System.out.println("ends");
channel.socket().close();
break out;
}
}
}catch(Exception e){
map.remove(key.channel().hashCode());
try {
channel.socket().close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("end");
break out;
}
}
else{
System.out.println("outing!!!!!1");
map.remove(key.channel().hashCode());
channel.socket();
break out;
}
while (count > 0) {
try {m_buffer.clear();
channel.read(m_buffer);
tempStr = Charset.forName("UTF-8").newDecoder()
.decode(m_buffer).toString();
Portocol por=new Portocol();
String endData=por.portocol(tempStr, channel, key, map);
if(endData==null){
map.remove(key.channel().hashCode());
channel.socket();
break out;
} else if(endData!=null){
map.put(key.channel().hashCode(),key.channel().hashCode());
channel.write(encoder.encode(CharBuffer.wrap(endData)));
}
count=0;
} catch (Exception e1) {
map.remove(key.channel().hashCode());
channel.socket();
break out;
}
}
if (count < 0) {
try {
map.remove(key.channel().hashCode());
channel.socket().close();
channel.close();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
b++;
System.out.println(map);
System.out.println(b);
}}
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.LinkedList;
import java.nio.channels.SocketChannel;
import java.nio.channels.SelectionKey;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.util.Date;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;崩溃
import java.io.IOException;//import lon.ChannelState;/**
* <p>
* Title: ���߳�
* </p>
* <p>
* Description: ���߳����ڶ�ȡ�ͻ������
* </p>
*
* @author starboy
* @version 1.0
*/public class Reader extends Thread { private static int timeOut = 10000;
CharsetEncoder encoder = Charset.forName("GB2312").newEncoder();
private static List pool = new LinkedList();
private static Notifier notifier = Notifier.getNotifier();
private ByteBuffer m_buffer = ByteBuffer.allocateDirect(1024);
HashMap map = new HashMap();
SelectionKey key = null;
static int b=1;
private static int BUFFER_SIZE = 1024;
static int i; public Reader(SelectionKey key) {
this.key = key;
}
public void run() {
//CountAccuMess map = new Countmap();
String tempStr=null;
Request request =(Request)key.attachment();
StringBuffer sBuffer = null;
out: for (;;) {
m_buffer.clear();
int count = 0;
SocketChannel channel = (SocketChannel) key.channel();
long start=System.currentTimeMillis();
if(channel.socket().isClosed()==false){
try{
while((count = channel.read(m_buffer))==0){
if((System.currentTimeMillis()-start)==timeOut){
map.remove(key.channel().hashCode());
System.out.println("ends");
channel.socket().close();
break out;
}
}
}catch(Exception e){
map.remove(key.channel().hashCode());
try {
channel.socket().close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("end");
break out;
}
}
else{
System.out.println("outing!!!!!1");
map.remove(key.channel().hashCode());
channel.socket();
break out;
}
while (count > 0) {
try {m_buffer.clear();
channel.read(m_buffer);
tempStr = Charset.forName("UTF-8").newDecoder()
.decode(m_buffer).toString();
Portocol por=new Portocol();
String endData=por.portocol(tempStr, channel, key, map);
if(endData==null){
map.remove(key.channel().hashCode());
channel.socket();
break out;
} else if(endData!=null){
map.put(key.channel().hashCode(),key.channel().hashCode());
channel.write(encoder.encode(CharBuffer.wrap(endData)));
}
count=0;
} catch (Exception e1) {
map.remove(key.channel().hashCode());
channel.socket();
break out;
}
}
if (count < 0) {
try {
map.remove(key.channel().hashCode());
channel.socket().close();
channel.close();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
b++;
System.out.println(map);
System.out.println(b);
}}
或者说,类和对象的用法不地道。
没有看到明显的Selector类。
使用NIOSocket,是必须用到Selector的,
而且,要显示设置非阻塞形式,才可以实现异步机制,
事件通知,以及,函数回调。