View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty5.example.qotm;
17  
18  import io.netty5.bootstrap.Bootstrap;
19  import io.netty5.channel.ChannelOption;
20  import io.netty5.channel.EventLoopGroup;
21  import io.netty5.channel.MultithreadEventLoopGroup;
22  import io.netty5.channel.nio.NioHandler;
23  import io.netty5.channel.socket.nio.NioDatagramChannel;
24  
25  /**
26   * A UDP server that responds to the QOTM (quote of the moment) request to a {@link QuoteOfTheMomentClient}.
27   *
28   * Inspired by <a href="https://docs.oracle.com/javase/tutorial/networking/datagrams/clientServer.html">the official
29   * Java tutorial</a>.
30   */
31  public final class QuoteOfTheMomentServer {
32  
33      private static final int PORT = Integer.parseInt(System.getProperty("port", "7686"));
34  
35      public static void main(String[] args) throws Exception {
36          EventLoopGroup group = new MultithreadEventLoopGroup(NioHandler.newFactory());
37          try {
38              Bootstrap b = new Bootstrap();
39              b.group(group)
40               .channel(NioDatagramChannel.class)
41               .option(ChannelOption.SO_BROADCAST, true)
42               .handler(new QuoteOfTheMomentServerHandler());
43  
44              b.bind(PORT).asStage().get().closeFuture().asStage().await();
45          } finally {
46              group.shutdownGracefully();
47          }
48      }
49  }