Computers & Internet Logo

Related Topics:

Anonymous Posted on Nov 26, 2008

Please explain the working of this code

Import java.io.*;
import java.net.*;
import java.util.*;
import java.nio.*;
import java.nio.charset.*;
import java.nio.channels.*;
class NewIOHTTPServerTest{
private static String[] symbols = {"DELL", "INTC", "MSFT", "ORCL", "SUNW"};
private static Random rnd = new Random();
private static Charset cs = Charset.forName("UTF-8");
public static void main(String[] args) throws Exception{
Selector selector = Selector.open();

ServerSocketChannel server = ServerSocketChannel.open();
server.configureBlocking(
false);
server.socket().bind(
new InetSocketAddress(8055));
server.register(selector, SelectionKey.OP_ACCEPT);

//register other server channels

for(;;){
if(selector.select(1000) > 0){
Iterator i = selector.selectedKeys().iterator();
while(i.hasNext()){
SelectionKey key = (SelectionKey) i.next();
if(key.isAcceptable()){
ServerSocketChannel channel = (ServerSocketChannel) key.channel();
SocketChannel client = channel.accept();
client.configureBlocking(
false);
client.register(selector, SelectionKey.OP_READ);
}
else if(key.isReadable()){
SocketChannel channel = (SocketChannel) key.channel();
if(channel.socket().getLocalPort() == 8055)
handleHTTPRequest(channel);
}
i.remove();
}
}
else{
System.out.print(
new Date() + "\r");
}
}
}
private static void handleHTTPRequest(SocketChannel channel) throws IOException{
ByteBuffer buffer = ByteBuffer.allocate(1024);
int n = channel.read(buffer);
buffer.flip();
String request = cs.decode(buffer).toString();
String[] headers = request.split("\r\n");
String symbol = headers[0].substring(5, headers[0].length() - 9);
String response;
if(Arrays.binarySearch(symbols, symbol) >= 0)
response = "Price is " + (1000 + rnd.nextInt(9000)) / 100.0;
else
response = "Price not available";
channel.write(cs.encode("HTTP/1.0 200 OK\r\n"));
channel.write(cs.encode("Content-type: text/plain\r\n\r\n"));
channel.write(cs.encode(response));
channel.close();
}
}




















1 Answer

Anonymous

Level 1:

An expert who has achieved level 1.

  • Contributor 2 Answers
  • Posted on Nov 26, 2008
Anonymous
Contributor
Level 1:

An expert who has achieved level 1.

Joined: Nov 26, 2008
Answers
2
Questions
1
Helped
235
Points
4

It's a Java http server (webserver) that serves random "prices" if they match a search string.

Well here is an explaination:

private static String[] symbols = {"DELL", "INTC", "MSFT", "ORCL", "SUNW"};
These are search strings

private static Random rnd = new Random();
This generates a random number

private static Charset cs = Charset.forName("UTF-8");
This is the charset that the http server uses

ServerSocketChannel server = ServerSocketChannel.open();
Create a new server

server.socket().bind(new InetSocketAddress(8055));
Bind the http server to port 8055

The rest is handling the request and processing it

But here:
if(Arrays.binarySearch(symbols, symbol) >= 0)
response = "Price is " + (1000 + rnd.nextInt(9000)) / 100.0;
else
response = "Price not available";
channel.write(cs.encode("HTTP/1.0 200 OK "));
channel.write(cs.encode("Content-type: text/plain "));
channel.write(cs.encode(response));
channel.close();
}
It sees if the string input matchs: DELL INTC MSFT ORCL SUNW.

(private static String[] symbols = {"DELL", "INTC", "MSFT", "ORCL", "SUNW"};)
If so, it gives a price based on a random number.
(
response = "Price is " + (1000 + rnd.nextInt(9000)) / 100.0;)
Or else
It says "price not available"
then it gives a html header to end the error message.

Hope I helped you :)
Gook luck

Add Your Answer

×

Uploading: 0%

my-video-file.mp4

Complete. Click "Add" to insert your video. Add

×

Loading...
Loading...

206 views

Ask a Question

Usually answered in minutes!

Top Computers & Internet Experts

ADMIN Andrew
ADMIN Andrew

Level 3 Expert

73783 Answers

ADMIN Eric
ADMIN Eric

Level 3 Expert

42222 Answers

Brad Brown

Level 3 Expert

19190 Answers

Are you a Computer and Internet Expert? Answer questions, earn points and help others

Answer questions

Manuals & User Guides

Loading...