Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.net.InetSocketAddress;
- import java.net.ServerSocket;
- import java.nio.ByteBuffer;
- import java.nio.channels.CancelledKeyException;
- import java.nio.channels.FileChannel;
- import java.nio.channels.SelectionKey;
- import java.nio.channels.Selector;
- import java.nio.channels.ServerSocketChannel;
- import java.nio.channels.SocketChannel;
- import java.nio.charset.Charset;
- import java.nio.charset.StandardCharsets;
- import java.nio.file.Paths;
- import java.nio.file.StandardOpenOption;
- import java.util.Set;
- import java.util.Iterator;
- public class Main {
- public static final int DEFAULT_PORT = 1919;
- public static void main(String[] args) throws InterruptedException {
- int port;
- try{
- port = Integer.parseInt(args[0]);
- }catch(RuntimeException ex) {
- port = DEFAULT_PORT;
- }
- System.out.println("Listening for connections on port "+port);
- ServerSocketChannel serverChannel;
- Selector selector;
- try {
- serverChannel = ServerSocketChannel.open();
- ServerSocket ss = serverChannel.socket();
- InetSocketAddress address = new InetSocketAddress(port);
- ss.bind(address);
- serverChannel.configureBlocking(false);
- selector = Selector.open();
- serverChannel.register(selector, SelectionKey.OP_ACCEPT);
- }catch(IOException ex) {
- ex.printStackTrace();
- return;
- }
- int n = 0;
- while(true) {
- try{
- selector.selectedKeys().clear();
- n = selector.select();
- }catch(IOException ex) {
- ex.printStackTrace();
- break;
- }
- if(n == 0) continue;
- Set<SelectionKey> readyKeys = selector.selectedKeys();
- Iterator<SelectionKey> iterator = readyKeys.iterator();
- while(iterator.hasNext()) {
- SelectionKey key = iterator.next();
- iterator.remove();
- try{
- if(key.isAcceptable()) {
- ServerSocketChannel server = (ServerSocketChannel) key.channel();
- SocketChannel client = server.accept();
- System.out.println("Accepted connection from " + client);
- client.configureBlocking(false);
- SelectionKey key2 = client.register(selector, SelectionKey.OP_READ);
- ByteBuffer output = ByteBuffer.allocate(128);
- key2.attach(output);
- } else if (key.isReadable()) {
- SocketChannel client = (SocketChannel) key.channel();
- ByteBuffer input = (ByteBuffer) key.attachment();
- String file = "";
- try {
- int n_read = client.read(input);
- byte[] bytes = input.array();
- String actual = new String(bytes,Charset.forName("UTF-8")).trim();
- char[] actToArray = actual.toCharArray();
- for(int h = 0;h<n_read;h++) file = file + actToArray[h];
- System.out.println("Got : "+file);
- } catch (IOException e) {
- key.cancel();
- return;
- } catch (CancelledKeyException e){
- e.printStackTrace();
- System.exit(-1);
- }
- // Lettura dal file -> contenuto , da fare
- FileChannel fc = FileChannel.open(Paths.get(file), StandardOpenOption.READ);
- ByteBuffer bf = ByteBuffer.allocate(128);
- String contenuto = "";
- while (fc.read(bf) > 0) {
- bf.flip();
- byte[] bytes = bf.array();
- contenuto = contenuto + new String(bytes,Charset.forName("UTF-8")).trim();
- bf.clear();
- }
- System.out.println(contenuto);
- //String contenuto = "ciao";
- char[] contToArray = contenuto.toCharArray();
- int i = 0;
- int j = 0;
- try {
- while(i<contToArray.length/128){
- contenuto = "";
- for(j = 0;j<128;j++){
- contenuto = contenuto + contToArray[i*j];
- }
- i++;
- input.clear();
- byte[] bytes = contenuto.getBytes(Charset.forName("UTF-8"));
- input.put(bytes);
- input.flip();
- client.write(input);
- }
- contenuto = "";
- for(int k = i*j;k<contToArray.length;k++){
- contenuto = contenuto + contToArray[k];
- }
- input.clear();
- byte[] bytes = contenuto.getBytes(Charset.forName("UTF-8"));
- input.put(bytes);
- input.flip();
- client.write(input);
- input.clear();
- } catch (IOException e) {
- e.printStackTrace();
- System.exit(-1);
- }
- }
- }catch(IOException ex) {
- try{
- key.cancel();
- key.channel().close();
- } catch(IOException cex) {
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement