java.nio.- Kanäle.IllegalBlockingModeException beim Lesen der Inhalte vom client über einen Kanal
Bin ganz neu NIO am Gebäude eine chat-Anwendung, die ich bekam connection-in allen client, sondern beim Lesen der Inhalte vom client bekam ich java.nio.channels.IllegalBlockingModeException
. bitte helft mir hier ist der code, wo die exception tritt. beim erreichen while (rbc.read(b) != -1)
von PrintRequest class
Ausnahme tritt
public class PrintRequest extends Thread
{
public PrintRequest(SocketChannel sc,int i)throws Exception
{
System.out.println("going to enter the try block of PrintRequest");
try
{
System.out.println("Am in the try block of PrintRequest");
ReadableByteChannel rbc = Channels.newChannel(sc.socket().getInputStream());
System.out.println("checking in PrintRequest 0001");
WritableByteChannel wbc = Channels.newChannel(System.out);
System.out.println("checking in PrintRequest 0010");
ByteBuffer b = ByteBuffer.allocateDirect(1024); //read 1024 bytes
//int numBytesRead = sc.read(b);
System.out.println("checking in PrintRequest 0011");
while (rbc.read(b) != -1)
{
System.out.println("Am in while loop of PrintRequest ");
b.flip();
while (b.hasRemaining())
{
wbc.write(b);
System.out.println();
}
b.clear();
}
}
catch(Exception E)
{
System.out.println("Exception in printlnRequest "+E);
}
} }
mein server code:
public class Server
{
private Selector selector;
private ServerSocketChannel channel;
public void listner()
{
try
{
this.selector = Selector.open();
this.channel = ServerSocketChannel.open();
this.channel.configureBlocking(false);
this.channel.socket().bind(new InetSocketAddress(8888));
this.channel.register(this.selector, 16);
}
catch (IOException e)
{
throw new RuntimeException("Could not register listener", e);
}}
public void non_Socket()throws Exception
{
try
{
int i=0;
this.channel = ServerSocketChannel.open();
this.channel.configureBlocking(false);
this.channel.socket().bind(new InetSocketAddress(80));
while(true)
{
this.selector = Selector.open();
this.channel.register(this.selector, 16);
Set<SelectionKey> keys = this.selector.selectedKeys();
Iterator<SelectionKey> iterator = keys.iterator();
int readyChannels = selector.select();
if(readyChannels == 0) continue;
Set<SelectionKey> selectedKeys = selector.selectedKeys();
Iterator<SelectionKey> keyIterator = selectedKeys.iterator();
while(keyIterator.hasNext())
{
SelectionKey key = keyIterator.next();
if(key.isAcceptable())
{
//a connection was accepted by a ServerSocketChannel.
System.out.println("a connection was accepted by a ServerSocketChannel");
SocketChannel sc = this.channel.accept();
sc.configureBlocking(false);
System.out.println("Received an incoming connection from " + sc.socket().getRemoteSocketAddress());
System.out.println("checking 0101");
//new PrintRequest(sc,i).start();
System.out.println("checking 0110");
if(sc == null )
{
System.out.println("Please login");
Thread.sleep(6000);
}
else
{
System.out.println("Last Login was successful");
//new PrintRequest(sc,i).start();
PrintRequest pr=new PrintRequest(sc,i);
new Thread(pr).start();
}
}
else if (key.isConnectable())
{
//a connection was established with a remote server.
System.out.println("a connection was established with a remote server");
}
else if (key.isWritable())
{
//a channel is ready for writing
System.out.println(" a channel is ready for writing");
}
else if (key.isReadable())
{
//a channel is ready for reading
System.out.println(" a channel is ready for Reading");
}
System.out.println(" a channel is prepare for Reading");
keyIterator.remove();
Thread.sleep(5000);
}
}
}
catch(Exception E)
{
System.out.println(" Here : "+E);
}
finally
{
if (channel != null)
{
try
{
channel.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
public static void main(String [] abc) throws Exception
{
new Server().non_Socket();
}}
Client-Seite:
public class Client
{
public void non_Client_Socket()
{
SocketChannel sChannel=null;
try
{
sChannel = SocketChannel.open();
sChannel.connect(new InetSocketAddress("localhost", 80));
while (!sChannel.finishConnect())
{
System.out.println("Channel is not connected yet");
Thread.sleep(5000);
}
System.out.println("Channel is ready to use");
/* ---------- going to send data to server ------------*/
System.out.println("please enter the text");
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
while(true)
{
System.out.println("Enter the text");
String HELLO_REQUEST =stdin.readLine().toString();
if(HELLO_REQUEST.equalsIgnoreCase("end"))
{
break;
}
System.out.println("Sending a request to HelloServer");
ByteBuffer buffer = ByteBuffer.wrap(HELLO_REQUEST.getBytes());
sChannel.write(buffer);
}
}
catch(Exception E)
{
}
finally
{
if (sChannel != null)
{
try
{
sChannel.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
} }
/* ---------- the data is written in sChannel server will read from this channel ------------ */
public static void main(String [] args)throws Exception
{
new Client().non_Client_Socket();
}}
- Fortsetzung von server nicht annehmen mit mehr als einem client in nio
Du musst angemeldet sein, um einen Kommentar abzugeben.
Dein problem hier ist. Sie können nicht verwenden Sie einen stream aus einem Kanal im nicht-blockierenden Modus. Der code ist zwecklos, da
sc
ist schon eine ReadableByteChannel. Löschen Sie einfach diese Zeile und führen Sie die folgenden I/O mitsc
stattrbc
. Aber siehe unten.Haben Sie immer noch nicht behoben, die Probleme, die ich erwähnt in Ihrem vorherigen post. Ihre connect-Technik ist immer noch falsch, und Sie rufen PrintRequest() die Schleifen Lesung im non-blocking Modus anstelle der Registrierung der akzeptierten Kanal für OP_READ. Der code wird weiterhin keinen Sinn machen.
you are calling PrintRequest() which loops reading in non-blocking mode instead of registering the accepted channel for OP_READ
können Sie bitte erklären dies im detail. . . .OP_READ
Feuer, D. H. geben Sie IhreisReadable()
block, und tun, das Lesen Sie in da. Sie können Schleife, bis Sie einenread()
Rückgabewert0
, wenn Sie sollten gehen Sie zurück zu der select-Schleife, oder-1
, wenn Sie sollte in der Nähe des Kanals.OP_READ
. Haben Sie untersucht die NIO-Beispiele? Nicht viel Zeichen es hier.