Guest User

Untitled

a guest
Jan 21st, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.98 KB | None | 0 0
  1. package fr.upem.net.udp;
  2.  
  3. import java.io.IOException;
  4. import java.io.UnsupportedEncodingException;
  5. import java.net.InetSocketAddress;
  6. import java.net.SocketAddress;
  7. import java.nio.ByteBuffer;
  8. import java.nio.CharBuffer;
  9. import java.nio.channels.DatagramChannel;
  10. import java.nio.charset.Charset;
  11. import java.util.Optional;
  12. import java.util.Scanner;
  13.  
  14. public class ClientBetterUpperCaseUDP {
  15.  
  16.     public static Charset ASCII_CHARSET = Charset.forName("US-ASCII");
  17.     public static int BUFFER_SIZE = 1024;
  18.  
  19.     /**
  20.      * Create and return the String message represented by the ByteBuffer buffer in
  21.      * the following representation: - the size (as an Big Indian int) of the
  22.      * charsetName encoding in ASCII<br/>
  23.      * - the bytes encoding this charsetName in ASCII<br/>
  24.      * - the bytes encoding the message in this charsetName.<br/>
  25.      * The accepted ByteBuffer buffer must be in <strong>write mode</strong> (need
  26.      * to be flipped before to be used).
  27.      *
  28.      * @param buffer
  29.      *            a ByteBuffer containing the representation of a message
  30.      * @return the String represented by bb or Optional.empty() if the bb is
  31.      *         mal-formed
  32.      */
  33.     public static Optional<String> decodeMessage(ByteBuffer buffer) {
  34.         CharBuffer charbuff = CharBuffer.allocate(BUFFER_SIZE);
  35.         buffer.flip();
  36.         int csLength = buffer.getInt();
  37.         ByteBuffer bb = ByteBuffer.allocate(csLength);
  38.         for (int i = 0; i < csLength; i++) {
  39.             bb.put(buffer.get());
  40.         }
  41.         String csString = new String(bb.array(), ASCII_CHARSET);
  42.         Charset cs = Charset.forName(csString);
  43.         charbuff = cs.decode(buffer);
  44.         Optional<String> opt = Optional.of(charbuff.toString());
  45.         return opt;
  46.        
  47.     }
  48.  
  49.     /**
  50.      * Create and return a new ByteBuffer containing the representation of msg
  51.      * string using the charset charsetName in the following format. - the size (as
  52.      * an Big Indian int) of the charsetName encoding in ASCII<br/>
  53.      * - the bytes encoding this charsetName in ASCII<br/>
  54.      * - the bytes encoding msg in charsetName.<br/>
  55.      * The returned ByteBuffer is in <strong>write mode</strong> (need to be flipped
  56.      * before to be used).
  57.      *
  58.      * @param msg
  59.      *            the String to send
  60.      * @param charsetName
  61.      *            name of the Charset to encode the String msg
  62.      * @return a newly allocated ByteBuffer containing the representation of msg
  63.      * @throws UnsupportedEncodingException
  64.      */
  65.     private static ByteBuffer encodeMessage(String msg, String charsetName) throws UnsupportedEncodingException {
  66.         Charset cs = Charset.forName(charsetName);
  67.         ByteBuffer csName = ASCII_CHARSET.encode(charsetName);
  68.  
  69.         String csNameAscii = new String(csName.array(), ASCII_CHARSET);
  70.         ByteBuffer msgBuffer = cs.encode(msg);
  71.         String msgCharset = new String(msgBuffer.array(), cs);
  72.  
  73.         ByteBuffer fBuffer = ByteBuffer.allocate(Integer.SIZE + csNameAscii.length() + msgCharset.length());
  74.         fBuffer.putInt(csNameAscii.length());
  75.         fBuffer.put(csName);
  76.         fBuffer.put(msgBuffer);
  77.         return fBuffer;
  78.     }
  79.  
  80.     public static void usage() {
  81.         System.out.println("Usage : ClientBetterUpperCaseUDP host port charsetName");
  82.     }
  83.  
  84.     public static void main(String[] args) throws IOException {
  85.         // check and retrieve parameters
  86.         if (args.length != 3) {
  87.             usage();
  88.             return;
  89.         }
  90.         String host = args[0];
  91.         int port = Integer.valueOf(args[1]);
  92.         String charsetName = args[2];
  93.  
  94.         SocketAddress dest = new InetSocketAddress(host, port);
  95.         // buff to receive messages
  96.         ByteBuffer buff = ByteBuffer.allocateDirect(BUFFER_SIZE);
  97.  
  98.         try (Scanner scan = new Scanner(System.in); DatagramChannel dc = DatagramChannel.open()) {
  99.             while (scan.hasNextLine()) {
  100.                 String line = scan.nextLine();
  101.                 ByteBuffer packet = encodeMessage(line, charsetName);
  102.                 packet.flip();
  103.                 dc.send(packet, dest);
  104.                 buff.clear();
  105.                 dc.receive(buff);
  106.                 Optional<String> res = decodeMessage(buff);
  107.                 if (res.isPresent()) {
  108.                     System.out.println("Received: " + res.get());
  109.                 } else {
  110.                     System.out.println("Received an invalid paquet");
  111.                 }
  112.             }
  113.         }
  114.     }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment