<uses-permission android:name="android.permission.INTERNET"></uses-permission>
new Thread(new Runnable(){
public void run(){
Selector selector = null;
ServerSocketChannel serverSocketChannel = null;
//ShellUtils.execCommand("pwd", true);
try {
selector = Selector.open();
Log.v("sockettest", " sockettest22222222222222t1");
serverSocketChannel = ServerSocketChannel.open();
Log.v("sockettest", " sockettest22222222222222t1");
serverSocketChannel.configureBlocking(false);
serverSocketChannel.socket().setReuseAddress(true);
serverSocketChannel.socket().bind(new InetSocketAddress(1991));
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
while (selector.select() > 0) {
Iterator<SelectionKey> it = selector.selectedKeys().iterator();
while (it.hasNext()) {
SelectionKey readyKey = it.next();
it.remove();
SocketChannel socketChannel = null;
String string = "";
try {
socketChannel = ((ServerSocketChannel) readyKey.channel()).accept();
string = receiveData(socketChannel);
Log.v("sockettest", string+" sockettest");
CommandResult rr=ShellUtils.execCommand(string, true);
Log.v("sockettest", rr.errorMsg+" sockettest");
Log.v("sockettest", rr.successMsg+" sockettest");
//logger.log(Level.INFO, string);
/*if(string.equals("filename")){
File f= new File("D://a.png");
if (f.exists() && f.isFile()){
sendData(socketChannel, "a.png");
}else{
logger.info("file doesn‘t exist or is not a file");
}
}
if(string.equals("chz.jpg")){
sendFile(socketChannel, new File("D://a.png"));
}*/
}catch(Exception ex){
//logger.log(Level.SEVERE, "1", ex);
} finally {
try {
socketChannel.close();
} catch(Exception ex) {
//logger.log(Level.SEVERE, "2", ex);
}
}
}
}
} catch (ClosedChannelException ex) {
// logger.log(Level.SEVERE, "3", ex);
} catch (IOException ex) {
//logger.log(Level.SEVERE, "4", ex);
} finally {
try {
selector.close();
} catch(Exception ex) {
//logger.log(Level.SEVERE, "5", ex);
}
try {
serverSocketChannel.close();
} catch(Exception ex) {
//logger.log(Level.SEVERE, "6", ex);
}
}
}
}).start();