NIO は、単純化されたケースを除いて、せいぜい不十分に文書化されていることがわかりました。それでも、私はチュートリアルといくつかのリファクタリングを経て、最終的に最も単純なケースに戻りました.0バイトのSocketChannel読み取りでisReadableが起動することがあります. すべての実行で発生しているわけではありません。
以前は別のスレッドでアタッチされたオブジェクトから読み取りを呼び出していましたが、競合状態である可能性があると考えていましたが、セレクターのスレッドで読み取りを行うようになりましたが、それでも問題は解決しません。私はそれが私のテストクライアントである可能性があると思いますが、クライアントソケットはサーバーからの応答を受信するまで閉じてはならないため、何が一貫してトリガーされるのかわかりません。
含まれているコードでは、このスニペットによって送信された「こんにちは」メッセージは、私が期待するように毎回うまくいきます
out.write("hello".getBytes()); out.write(EOT); out.flush();
この後、長さ 0 のソケット チャネルを取得することがあります。そして時々、このスニペットから適切な応答が得られます:
out.write(dataServerCredentials.getBytes()); out.write(EOT); out.flush();
これについての洞察をいただければ幸いです。それは私をゆっくりと殺しています。私はすでにここで答えを見つけようとしましたが、関連していると思われる1つの質問は、私の問題にあまり光を当てませんでした.
前もって感謝します!
以下のコード スニペット:
選択方法:
public void execute()
{
initializeServerSocket();
for (;;)
{
try
{
System.out.println("Waiting for socket activity");
selector.select();
Iterator<SelectionKey> selectedKeys =
this.selector.selectedKeys().iterator();
while(selectedKeys.hasNext())
{
SelectionKey key = selectedKeys.next();
selectedKeys.remove();
if (!key.isValid())
{
continue;
}
if (key.isAcceptable())
{ // New connection
// TODO: Create helper method for this that configures user info?
System.out.println("Accepting connection");
ServerSocketChannel serverSocketChannel =
(ServerSocketChannel)key.channel();
SocketChannel socketChannel =
serverSocketChannel.accept();
socketChannel.socket().setSoTimeout(0);
socketChannel.configureBlocking(false);
SelectionKey newKey =
socketChannel.register(selector, SelectionKey.OP_READ);
// Create and attach an AuthProcessor to drive the states of this
// new Authentication request
newKey.attach(new AuthenticationRequestProcessor(newKey));
}
else if (key.isReadable())
{ // Socket has incoming communication
AuthenticationRequestProcessor authProcessor =
(AuthenticationRequestProcessor)key.attachment();
if (authProcessor == null)
{ // Cancel Key
key.channel().close();
key.cancel();
System.err.print("Cancelling Key - No Attachment");
}
else
{
if (authProcessor.getState() ==
AuthenticationRequestProcessor.TERMINATE_STATE)
{ // Cancel Key
key.channel().close();
key.cancel();
}
else
{ // Process new socket data
authProcessor.process(readStringFromKey(key));
}
}
}
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
読み取り方法 (ここでの愚かな部分は無視してください。これは別のスレッドからヤンクされたものです)
protected String readStringFromKey(SelectionKey key)
{
SocketChannel socketChannel = (SocketChannel)key.channel();
readBuffer.clear();
String message = null;
try
{
final int bytesRead = socketChannel.read(readBuffer);
if (-1 == bytesRead)
{ // Empty/Closed Channel
System.err.println("Error - No bytes to read on selected channel");
}
else
{ // Convert ByteBuffer into a String
System.out.println("Bytes Read: " + bytesRead);
readBuffer.flip();
message = byteBufferToString(readBuffer, bytesRead);
readBuffer.clear();
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
// Trim EOT off the end of the message
return message.trim();
}
クライアント スニペット:
public void connect()
{
boolean connectionStatus = false;
String connectionHost = null;
int connectionPort = 0;
String connectionAuthKey = null;
try
{ // Login
authenticationSocket = new Socket(AUTH_HOST, AUTH_PORT);
out = authenticationSocket.getOutputStream();
in = new BufferedInputStream(authenticationSocket.getInputStream());
out.write("hello".getBytes());
out.write(EOT);
out.flush();
StringBuilder helloResponse = new StringBuilder();
// Read response off socket
int currentByte = in.read();
while (currentByte > -1 && currentByte != EOT)
{
helloResponse.append((char)currentByte);
currentByte = in.read();
}
outgoingResponses.offer(Plist.fromXml(helloResponse.toString()));
System.out.println("\n" + helloResponse.toString());
out.write(credentials.getBytes());
out.write(EOT);
out.flush();
// Read status
int byteRead;
StringBuilder command = new StringBuilder();
do
{
byteRead = in.read();
if (0 < byteRead)
{
if (EOT == byteRead)
{
Logger.logData(command.toString());
Map<String, Object> plist = Plist.fromXml(command.toString());
outgoingResponses.offer(plist);
// Connection info for Data Port
connectionStatus = (Boolean)plist.get(STATUS_KEY);
connectionHost = (String)plist.get(SERVER_KEY);
connectionPort = (Integer)plist.get(PORT_KEY);
connectionAuthKey = (String)plist.get(AUTH_KEY);
Logger.logData("Server =>" + plist.get("server"));
command = new StringBuilder();
}
else
{
command.append((char)byteRead);
}
}
}
while (EOT != byteRead);
}
catch (UnknownHostException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (XmlParseException e)
{
Logger.logData("Invalid Plist format");
e.printStackTrace();
}
finally
{ // Clean up handles
try
{
authenticationSocket.close();
out.close();
in.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("Connection status =>" + connectionStatus);
System.out.println("Connection host =>" + connectionHost);
System.out.println("Connection port =>" + connectionPort);
if (connectionStatus)
{
dataServerHost = connectionHost;
dataServerPort = connectionPort;
dataServerAuthKey = connectionAuthKey;
System.out.println("Connecting to data server @: " + dataServerHost + ":" + dataServerPort);
connectToDataServer();
}
}