Created
February 7, 2010 10:10
-
-
Save drewlesueur/297350 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//non blocking socket server | |
package server3; | |
/** | |
* | |
* | |
*/ | |
import java.io.*; | |
import java.nio.*; | |
import java.nio.channels.*; | |
import java.nio.channels.spi.*; | |
import java.nio.charset.*; | |
import java.net.*; | |
import java.util.*; | |
//import javax.script.*; | |
public class Nbs { | |
static void handle_message(SocketChannel client, String message) throws Exception { | |
if (message.startsWith("GET / HTTP")) { | |
write(client, "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\nThis is the resῼponse yall"); | |
client.close(); | |
/* ScriptEngineManager mgr = new ScriptEngineManager(); | |
ScriptEngine jsEngine = mgr.getEngineByName("JavaScript"); | |
try { | |
jsEngine.eval("print('Hello, world!')"); | |
} catch (ScriptException ex) { | |
ex.printStackTrace(); | |
}*/ | |
} | |
} | |
static void write(SocketChannel client, String message) throws Exception{ | |
byte[] message_bytes = message.getBytes("UTF8"); | |
ByteBuffer b = ByteBuffer.allocate(message_bytes.length); | |
byte[] arr = b.array(); | |
for (int i = 0; i < arr.length; i++) { | |
arr[i] = message_bytes[i]; | |
} | |
System.out.println(message + "--"); | |
client.write(b); | |
} | |
static String read(SocketChannel client) throws Exception { | |
ByteBuffer b = ByteBuffer.allocate(1024); | |
try { | |
client.read(b); | |
} catch (Exception e) { | |
//System.out.println("oops"); | |
client.close(); | |
return "yo"; | |
//return ""; | |
} | |
b.flip(); | |
String ret = new String(b.array(), "UTF8"); | |
System.out.println(":)"); | |
return ret; | |
} | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) throws IOException, Exception { | |
// TODO code application logic here | |
Selector acceptSelector = Selector.open(); | |
//Selector acceptSelector = SelectorProvider.provider().openSelector(); | |
ServerSocketChannel ssc = ServerSocketChannel.open(); | |
ssc.configureBlocking(false); | |
//InetAddress lh = InetAddress.getLocalHost(); | |
String lh = "localhost"; | |
int port = 80; | |
InetSocketAddress isa = new InetSocketAddress(lh, port); | |
ssc.socket().bind(isa); | |
SelectionKey acceptKey = ssc.register(acceptSelector, SelectionKey.OP_ACCEPT); | |
int keysAdded = 0; | |
while ((keysAdded = acceptSelector.select()) > 0) { | |
Set readyKeys = acceptSelector.selectedKeys(); | |
Iterator i = readyKeys.iterator(); | |
// Walk through the ready keys collection and process date requests. | |
while (i.hasNext()) { | |
SelectionKey sk = (SelectionKey)i.next(); | |
i.remove(); | |
if (sk.isAcceptable()) { | |
ServerSocketChannel nextReady = (ServerSocketChannel)sk.channel(); | |
// Accept the date request and send back the date string | |
SocketChannel client = nextReady.accept(); | |
//Socket s = channel.socket(); | |
client.configureBlocking( false ); | |
client.register( acceptSelector, SelectionKey.OP_READ|SelectionKey.OP_WRITE ); | |
} | |
else if (sk.isReadable()) { | |
SocketChannel client = (SocketChannel) sk.channel(); | |
//Socket s = client.socket(); | |
//System.out.println(read(s.getInputStream())); //no can do here | |
String message = read(client); | |
handle_message(client, message); | |
} | |
if (sk.isValid() && sk.isWritable()) { | |
//SocketChannel client = (SocketChannel) sk.channel(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment