Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.34 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.net.InetSocketAddress;
  3. import java.net.ServerSocket;
  4. import java.nio.ByteBuffer;
  5. import java.nio.channels.CancelledKeyException;
  6. import java.nio.channels.FileChannel;
  7. import java.nio.channels.SelectionKey;
  8. import java.nio.channels.Selector;
  9. import java.nio.channels.ServerSocketChannel;
  10. import java.nio.channels.SocketChannel;
  11. import java.nio.charset.Charset;
  12. import java.nio.charset.StandardCharsets;
  13. import java.nio.file.Paths;
  14. import java.nio.file.StandardOpenOption;
  15. import java.util.Set;
  16. import java.util.Iterator;
  17.  
  18.  
  19. public class Main {
  20.     public static final int DEFAULT_PORT = 1919;
  21.     public static void main(String[] args) throws InterruptedException {
  22.         int port;
  23.         try{
  24.             port = Integer.parseInt(args[0]);
  25.         }catch(RuntimeException ex) {
  26.             port = DEFAULT_PORT;
  27.         }
  28.         System.out.println("Listening for connections on port "+port);
  29.         ServerSocketChannel serverChannel;
  30.         Selector selector;
  31.         try {
  32.             serverChannel = ServerSocketChannel.open();
  33.             ServerSocket ss = serverChannel.socket();
  34.             InetSocketAddress address = new InetSocketAddress(port);
  35.             ss.bind(address);
  36.             serverChannel.configureBlocking(false);
  37.             selector = Selector.open();
  38.             serverChannel.register(selector, SelectionKey.OP_ACCEPT);
  39.         }catch(IOException ex) {
  40.             ex.printStackTrace();
  41.             return;
  42.         }
  43.         int n = 0;
  44.         while(true) {
  45.             try{
  46.                 selector.selectedKeys().clear();
  47.                 n = selector.select();
  48.             }catch(IOException ex) {
  49.                 ex.printStackTrace();
  50.                 break;
  51.             }
  52.             if(n == 0) continue;
  53.             Set<SelectionKey> readyKeys = selector.selectedKeys();
  54.             Iterator<SelectionKey> iterator = readyKeys.iterator();
  55.             while(iterator.hasNext()) {
  56.                 SelectionKey key = iterator.next();
  57.                 iterator.remove();
  58.                 try{
  59.                     if(key.isAcceptable()) {
  60.                         ServerSocketChannel server = (ServerSocketChannel) key.channel();
  61.                         SocketChannel client = server.accept();
  62.                         System.out.println("Accepted connection from " + client);
  63.                         client.configureBlocking(false);
  64.                         SelectionKey key2 = client.register(selector, SelectionKey.OP_READ);
  65.                         ByteBuffer output = ByteBuffer.allocate(128);
  66.                         key2.attach(output);
  67.                     } else if (key.isReadable()) {
  68.                         SocketChannel client = (SocketChannel) key.channel();
  69.                         ByteBuffer input = (ByteBuffer) key.attachment();
  70.                         String file = "";
  71.                         try {
  72.                             int n_read = client.read(input);
  73.                             byte[] bytes = input.array();
  74.                             String actual = new String(bytes,Charset.forName("UTF-8")).trim();
  75.                             char[] actToArray = actual.toCharArray();
  76.                             for(int h = 0;h<n_read;h++) file = file + actToArray[h];
  77.                             System.out.println("Got : "+file);
  78.                         } catch (IOException e) {
  79.                             key.cancel();
  80.                             return;
  81.                         } catch (CancelledKeyException e){
  82.                             e.printStackTrace();
  83.                             System.exit(-1);
  84.                         }
  85.                        
  86.                         // Lettura dal file -> contenuto , da fare
  87.                         FileChannel fc = FileChannel.open(Paths.get(file), StandardOpenOption.READ);
  88.                         ByteBuffer bf = ByteBuffer.allocate(128);
  89.                         String contenuto = "";
  90.                         while (fc.read(bf) > 0) {
  91.                             bf.flip();
  92.                             byte[] bytes = bf.array();
  93.                             contenuto = contenuto + new String(bytes,Charset.forName("UTF-8")).trim();
  94.                             bf.clear();
  95.                         }
  96.                         System.out.println(contenuto);
  97.                         //String contenuto = "ciao";
  98.                         char[] contToArray = contenuto.toCharArray();
  99.                         int i = 0;
  100.                         int j = 0;
  101.                         try {
  102.                             while(i<contToArray.length/128){
  103.                                 contenuto = "";
  104.                                 for(j = 0;j<128;j++){
  105.                                     contenuto = contenuto + contToArray[i*j];
  106.                                 }
  107.                                 i++;
  108.                                 input.clear();
  109.                                 byte[] bytes = contenuto.getBytes(Charset.forName("UTF-8"));
  110.                                 input.put(bytes);
  111.                                 input.flip();
  112.                                 client.write(input);
  113.                             }
  114.                             contenuto = "";
  115.                             for(int k = i*j;k<contToArray.length;k++){
  116.                                 contenuto = contenuto + contToArray[k];
  117.                             }
  118.                             input.clear();
  119.                             byte[] bytes = contenuto.getBytes(Charset.forName("UTF-8"));
  120.                             input.put(bytes);
  121.                             input.flip();
  122.                             client.write(input);
  123.                             input.clear();
  124.                         } catch (IOException e) {
  125.                             e.printStackTrace();
  126.                             System.exit(-1);
  127.                         }
  128.                     }
  129.                 }catch(IOException ex) {
  130.                     try{
  131.                         key.cancel();
  132.                         key.channel().close();
  133.                     } catch(IOException cex) {
  134.                        
  135.                     }
  136.                 }
  137.             }
  138.         }
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement