服务端程序崩溃
我最近做个采集服务端。要用到长连接。这里我用到这样的长连接。不过,如果是多条连接,系统会崩溃了。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);
}
}
--------------------编程问答-------------------- 你通过nio中的管道来实现的话 应该是不会崩溃的。 因为他可以无阻塞的运行。 你最好还是理清思路才写。 --------------------编程问答-------------------- 补充下 udp中是不需要维持连接的哟。。。。 是数据报方式的 --------------------编程问答-------------------- selector这个东西好像是有限的。等你连接到一定的数量时他会阻塞了。我觉得是这样。老听网上的人说能可以实现上千上万个连接。我就不能实现。想来想去不知道问题出于哪里。
--------------------编程问答-------------------- 程序理念不正确。
或者说,类和对象的用法不地道。
没有看到明显的Selector类。
使用NIOSocket,是必须用到Selector的,
而且,要显示设置非阻塞形式,才可以实现异步机制,
事件通知,以及,函数回调。
补充:Java , Java SE