关于非阻塞Socket的问题,高手请进,解决马上给分!
服务器
package simulation;
import java.io.*;
import java.util.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.channels.spi.*;
public class ServerThread extends Thread
{
FrmMain frm;
Selector selector;
ServerSocketChannel channel;
SocketChannel socket;
int port = 8000;
long dispTime = 0;
public ServerThread(FrmMain frm)
{
this.frm = frm;
try
{
selector = Selector.open();
channel = ServerSocketChannel.open();
channel.configureBlocking(false);
InetAddress localhost = InetAddress.getLocalHost();
InetSocketAddress isa = new InetSocketAddress(localhost, port);
channel.socket().bind(isa);
channel.register(selector, SelectionKey.OP_ACCEPT);
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void run()
{
while (true)
{
try
{
while (selector.select() > 0)
{
Set readyKeys = selector.keys();
Iterator it = readyKeys.iterator();
while (it.hasNext())
{
SelectionKey key = (SelectionKey)it.next();
it.remove();
if (key.isAcceptable())
{
ServerSocketChannel ssc = (ServerSocketChannel)key.channel();
socket = ssc.accept();
socket.configureBlocking(false);
socket.register(selector, SelectionKey.OP_READ);
}
if (key.isReadable())
{
readPkg(key);
}
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
this.yield();
}
}
public void readPkg(SelectionKey key)
{
ByteBuffer buf;
SocketChannel sc = (SocketChannel)key.channel();
try
{
ByteBuffer buf = ByteBuffer.allocate(16);
SocketChannel sc = (SocketChannel)key.channel();
int k = sc.read(buf);
System.out.println(buf.toString());
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
客户端
package client;
import java.net.*;
import java.nio.channels.*;
import java.io.*;
import java.nio.ByteBuffer;
public class Client
{
public SocketChannel client = null;
public InetSocketAddress isa = null;
Head head = new Head();
static Client ct;
FrmClient frm;
public Client(FrmClient frm)
{
frm = frm;
int result = 0;
try
{
client = SocketChannel.open();
isa = new InetSocketAddress("192.168.0.16",8000);
client.connect(isa);
client.configureBlocking(false);
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public int sendHead(byte[] pkghead)
{
int nBytes = -1;
try
{
ByteBuffer buffer = ByteBuffer.wrap(pkghead);
nBytes = client.write(buffer);
}
catch (IOException e)
{
e.printStackTrace();
}
return nBytes;
}
public String readHead(String str)
{
byte temp[] = new byte[4];
int nBytes ;
try
{
ByteBuffer buffer = ByteBuffer.wrap(temp);
nBytes = client.read(buffer);
}
catch (IOException e)
{
e.printStackTrace();
}
return str = str +":"+sys_conver.get_int(temp) + "\n";
}
public void readMsg()
{
String str = null;
str = str + readHead("pkgLen");
str = str + readHead("pkgCmd");
str = str + readHead("pkgStatus");
str = str + readHead("pkgSeq");
frm.jTextArea1.append(str);
}
public void sendMsg()
{
try
{
ByteBuffer buf = ByteBuffer.allocate(16);
buf.putInt(10);
buf.putInt(20);
buf.putInt(30);
buf.putInt(40);
buf.flip();
int nbytes = client.write(buf);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
1、客户端程序可以连上来,但发送一次之后,发送第2次,服务器就出问题了,主要是在run()方法中的while (selector.select() > 0)这句上,本来selector.select()应该是阻塞的,但第2次发送后,就不阻塞了,立刻返回,而且为false。请问这是为什么?
2、连第二个客户端的时候,程序也没有检测到,key.isAcceptable()始终为false,是怎么回事?