私はしばらくの間NIOSocketChannelsを操作しようとしていましたが、SocketChannelへの書き込みに困惑しています。次のコードは私のクライアントからのものです:
public class nbClient {
/**
* @param args
*/
static int id;
static int delay = 1000;
static int port;
public static void main(String[] args) throws Exception{
if (args.length > 0){
id = Integer.parseInt(args[0]);
port = Integer.parseInt(args[1]);
}
else{
id = 99;
port = 4444;
}
// Create client SocketChannel
SocketChannel client = SocketChannel.open();
// nonblocking I/O
client.configureBlocking(false);
// Connection to host port 8000
client.connect(new java.net.InetSocketAddress("localhost",port));
// Create selector
Selector selector = Selector.open();
//SelectionKey clientKey = client.register(selector, SelectionKey.OP_CONNECT);
SelectionKey clientKey = client.register(selector, client.validOps());
// Waiting for the connection
while (selector.select(1000) > 0) {
// Get keys
Set keys = selector.selectedKeys();
Iterator i = keys.iterator();
// For each key...
while (i.hasNext()) {
SelectionKey key = (SelectionKey)i.next();
// Remove the current key
i.remove();
// Get the socket channel held by the key
SocketChannel channel = (SocketChannel)key.channel();
// Attempt a connection
if (key.isConnectable()) {
// Connection OK
System.out.println("Server Found");
// Close pendency connections
if (channel.isConnectionPending())
channel.finishConnect();
//channel.close();
channel.register(selector, SelectionKey.OP_WRITE | SelectionKey.OP_READ);
}
if (key.isWritable()){
System.out.println("Ready for writing");
// Write on the buffer
ByteBuffer buffer = null;
int counter = 0;
buffer =
ByteBuffer.wrap(
new String(" This is a very long message from Client " + id + " that should exceed the bufer by a bit").getBytes());
int outBytes = channel.write(buffer);
System.out.println(channel.isConnectionPending());
System.out.println(outBytes);
buffer.clear();
counter++;
}
if (key.isReadable()){
System.out.println("Ready for reading");
}
}
}
}
}
私の問題は、私がチャンネルに書き出そうとするときに関係しています。コードのこの部分が実行されるたびに、サーバーがデータを処理するのを待たずに、各反復中にデータを書き出すことで、何度もループします。コードを使用してデバッガーを実行すると、サーバーは転送に追いついて処理できるようです(クライアントは要求を再送信し続けますが、少なくともサーバーは転送されたバイトを表示します)。ただし、強制的な遅延なしにコードをそのまま実行すると、クライアントコードは数十回実行され、サーバーは転送を無視しているように見えますが、接続は切断されます。これが私のサーバーコードセクションです-Runnableクラスから実行されることに注意してください:
try {
readwriteSelector.select();
// Once the event occurs, get keys
Set<SelectionKey> keys = readwriteSelector.selectedKeys();
Iterator<SelectionKey> i = keys.iterator();
// For each keys...
while(i.hasNext()) {
// Get this most recent key
SelectionKey key = i.next();
if (key.isReadable()){
System.out.println("Is Readable");
}
if (key.isWritable()){
System.out.println("Is Writable");
SocketChannel client = (SocketChannel) key.channel();
buf.clear();
int numBytesRead = client.read(buf);
if (numBytesRead == -1){
client.close();
}
else {
buf.flip();
byte[] tempb = new byte[buf.remaining()];
buf.get(tempb);
String s = new String(tempb);
System.out.println(s);
}
}
// Remove the current key
i.remove();
//readwriteSelector.selectedKeys().clear();
}
} catch (IOException e) {
e.printStackTrace();
}
これはかなりのコードであることは知っていますが、現時点では問題がどこにあるのか特定できません。遅延を強制すると転送が正常に行われるにもかかわらず、クライアントとサーバーが通信できないように見える理由を誰かが推測できますか?
ありがとう。