This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes nio and http request Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "nio and http request" Watch "nio and http request" New topic
Author

nio and http request

Nicolas Viollin
Greenhorn

Joined: Apr 13, 2001
Posts: 26
Hi,

I must write an app that controls http requests. I would like to use java.nio.* API, but I don't know how to keep a trace of which connection is asking for what and forward the data back to it:

Let's say I open ask for the page www.google.fr, my proxy will get the incoming connection, will read the request, the will have to send it to the web site i want to access. The only thing is how fo I know who's asking for which website ???

Here is the code I use to read incoming connections:

public void run(){
while (getRun()){
try{
int num = _selector.select();

if (num == 0)
continue;

Set keys = _selector.selectedKeys();
Iterator it = keys.iterator();
while (it.hasNext()){
SelectionKey key = (SelectionKey)it.next();

if ((key.readyOps() & SelectionKey.OP_ACCEPT) ==
SelectionKey.OP_ACCEPT)
incomingConnection();
else if ((key.readyOps() & SelectionKey.OP_READ) ==
SelectionKey.OP_READ)
incomingData(key);
}

keys.clear();

}catch(Exception e){
e.printStackTrace();
setRun(false);
}
}
}

private void incomingConnection() throws Exception{
Socket s = _ss.accept();
System.out.println( "Got connection from "+s );

// Make sure to make it non-blocking, so we can
// use a selector on it.
SocketChannel sc = s.getChannel();
sc.configureBlocking( false );

// Register it with the selector, for reading
sc.register( _selector, SelectionKey.OP_READ );
}

private void incomingData(SelectionKey key){
SocketChannel sc = null;

try {

// It's incoming data on a connection, so
// process it
sc = (SocketChannel)key.channel();
boolean ok = readData(sc);

// If the connection is dead, then remove it
// from the selector and close it
if (!ok) {
key.cancel();

Socket s = null;
try {
s = sc.socket();
s.close();
} catch( IOException ie ) {
System.err.println( "Error closing socket "+s+": "+ie );
}
}

} catch( Exception ie ) {

// On exception, remove this channel from the selector
key.cancel();

try {
sc.close();
} catch( IOException ie2 ) { System.out.println( ie2 ); }

System.out.println( "Closed "+sc );
}
}

private boolean readData(SocketChannel sc) throws Exception{
buffer.clear();
sc.read( buffer );
buffer.flip();

if (buffer.limit()==0) {
return false;
}

System.out.print(new String(buffer.array()));

return true;
}

any ideas ??
thnx
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
I haven't used NIO enough yet to remember off the top of my head, but I believe you can associate a single object instance with each key. Write a class that will hold any state needed (like the HTTP request itself) for each key.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: nio and http request