thufir

datagram

Jul 26th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.85 KB | None | 0 0
  1. package net.bounceme.dur.client.netty;
  2.  
  3. import io.netty.bootstrap.Bootstrap;
  4. import io.netty.buffer.Unpooled;
  5. import io.netty.channel.Channel;
  6. import io.netty.channel.ChannelOption;
  7. import io.netty.channel.EventLoopGroup;
  8. import io.netty.channel.nio.NioEventLoopGroup;
  9. import io.netty.channel.socket.DatagramPacket;
  10. import io.netty.channel.socket.nio.NioDatagramChannel;
  11. import static io.netty.handler.codec.rtsp.RtspHeaders.Values.PORT;
  12. import io.netty.util.CharsetUtil;
  13. import java.net.InetSocketAddress;
  14. import java.util.logging.Logger;
  15. import javax.net.ssl.SSLException;
  16.  
  17. /**
  18.  * Modification of {@link EchoClient} which utilizes Java object serialization.
  19.  */
  20. public final class Client {
  21.  
  22.     private static final Logger log = Logger.getLogger(Client.class.getName());
  23.     static final int SIZE = Integer.parseInt(System.getProperty("size", "256"));
  24.  
  25.     public Client() {
  26.     }
  27.  
  28.     public void init() throws InterruptedException, SSLException {
  29.         MyProps p = new MyProps();
  30.         String host = p.getHost();
  31.         int port = p.getServerPort();
  32.         startClient(host, port);
  33.     }
  34.  
  35.     private void startClient(final String host, final int port) throws SSLException, InterruptedException {
  36.         EventLoopGroup group = new NioEventLoopGroup();
  37.         try {
  38.             Bootstrap b = new Bootstrap();
  39.             b.group(group)
  40.                     .channel(NioDatagramChannel.class)
  41.                     .option(ChannelOption.SO_BROADCAST, true)
  42.                     .handler(new ClientHandler());
  43.             Channel ch = b.bind(0).sync().channel();
  44.             ch.writeAndFlush(new DatagramPacket(
  45.                     Unpooled.copiedBuffer("QOTM?", CharsetUtil.UTF_8),
  46.                     new InetSocketAddress("255.255.255.255", port))).sync();
  47.         } finally {
  48.             group.shutdownGracefully();
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment