List of usage examples for io.netty.util CharsetUtil UTF_8
Charset UTF_8
To view the source code for io.netty.util CharsetUtil UTF_8.
Click Source Link
From source file:com.jjzhk.Chapter12.udp.ChineseProverbClient.java
License:Apache License
public void run(int port) throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try {//from ww w. j av a 2s . com Bootstrap b = new Bootstrap(); b.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, true) .handler(new ChineseProverbClientHandler()); Channel ch = b.bind(0).sync().channel(); // ???????DP ch.writeAndFlush( new DatagramPacket(Unpooled.copiedBuffer("???", CharsetUtil.UTF_8), new InetSocketAddress("255.255.255.255", port))) .sync(); if (!ch.closeFuture().await(15000)) { System.out.println("?!"); } } finally { group.shutdownGracefully(); } }
From source file:com.jjzhk.Chapter12.udp.ChineseProverbClientHandler.java
License:Apache License
@Override public void messageReceived(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception { String response = msg.content().toString(CharsetUtil.UTF_8); if (response.startsWith("???: ")) { System.out.println(response); ctx.close();/* w w w . j a va 2 s . c o m*/ } }
From source file:com.jjzhk.Chapter12.udp.ChineseProverbServerHandler.java
License:Apache License
@Override public void messageReceived(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception { String req = packet.content().toString(CharsetUtil.UTF_8); System.out.println(req);//from w w w. ja va 2 s.c om if ("???".equals(req)) { ctx.writeAndFlush(new DatagramPacket( Unpooled.copiedBuffer("???: " + nextQuote(), CharsetUtil.UTF_8), packet.sender())); } }
From source file:com.jjzhk.Chapter13.file.FileServer.java
License:Apache License
public void run(int port) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {// ww w .java2s . c om ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100).childHandler(new ChannelInitializer<SocketChannel>() { /* * (non-Javadoc) * * @see * io.netty.channel.ChannelInitializer#initChannel(io * .netty.channel.Channel) */ public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8), new LineBasedFrameDecoder(1024), new StringDecoder(CharsetUtil.UTF_8), new FileServerHandler()); } }); ChannelFuture f = b.bind(port).sync(); System.out.println("Start file server at port : " + port); f.channel().closeFuture().sync(); } finally { // ? bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.khs.microservice.whirlpool.whirlpoolserver.WhirlpoolServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/* ww w . j a v a 2 s . co m*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast("encoder", new HttpResponseEncoder()); p.addLast("decoder", new HttpRequestDecoder()); p.addLast("stringDecoder", new StringDecoder(CharsetUtil.UTF_8)); p.addLast("stringEncoder", new StringEncoder(CharsetUtil.UTF_8)); p.addLast("aggregator", new HttpObjectAggregator(65536)); p.addLast("handler", new WhirlpoolServerHandler()); } }); // Start the server. ChannelFuture f = b.bind(PORT).sync(); logger.info("Whirlpool Server started"); // Wait until the server socket is closed. f.channel().closeFuture().sync(); } finally { logger.info("Whirlpool Server shutdown started"); // Shut down all event loops to terminate all threads. bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); logger.info("Whirlpool Server shutdown completed"); } }
From source file:com.lbwan.game.client.WebSocketClientHandler.java
License:Apache License
@Override protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { Channel ch = ctx.channel();//from www . j a v a 2 s .c o m if (!handshaker.isHandshakeComplete()) { handshaker.finishHandshake(ch, (FullHttpResponse) msg); System.out.println("WebSocket Client connected!"); handshakeFuture.setSuccess(); return; } if (msg instanceof FullHttpResponse) { FullHttpResponse response = (FullHttpResponse) msg; throw new Exception("Unexpected FullHttpResponse (getStatus=" + response.getStatus() + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')'); } WebSocketFrame frame = (WebSocketFrame) msg; if (frame instanceof TextWebSocketFrame) { TextWebSocketFrame textFrame = (TextWebSocketFrame) frame; System.out.println("WebSocket Client received message: " + textFrame.text()); } else if (frame instanceof PongWebSocketFrame) { System.out.println("WebSocket Client received pong"); } else if (frame instanceof CloseWebSocketFrame) { System.out.println("WebSocket Client received closing"); ch.close(); } }
From source file:com.leadtone.riders.server.RidersWebSocketServerHandler.java
License:Apache License
private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) { // Generate an error page if response getStatus code is not OK (200). if (res.getStatus().code() != 200) { res.content().writeBytes(Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8)); setContentLength(res, res.content().readableBytes()); }/*from w w w .j a v a2 s . c o m*/ // Send the response and close the connection if necessary. ChannelFuture f = ctx.channel().write(res); if (!isKeepAlive(req) || res.getStatus().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
From source file:com.liferay.sync.engine.lan.server.discovery.LanDiscoveryListenerHandler.java
License:Open Source License
@Override public void channelRead0(ChannelHandlerContext channelHandlerContext, DatagramPacket datagramPacket) throws Exception { ByteBuf byteBuf = datagramPacket.content(); SyncLanClient syncLanClient = JSONUtil.readValue(byteBuf.toString(CharsetUtil.UTF_8), SyncLanClient.class); String syncLanClientUuid = syncLanClient.getSyncLanClientUuid(); if (syncLanClientUuid.equals(LanClientUtil.getSyncLanClientUuid())) { return;/* www. j a va2s. co m*/ } List<SyncLanEndpoint> syncLanEndpoints = SyncLanEndpointService.findSyncLanEndPoints(syncLanClientUuid); Map<String, Set<Long>> endpoints = syncLanClient.getEndpoints(); for (Map.Entry<String, Set<Long>> entry : endpoints.entrySet()) { for (Long groupId : entry.getValue()) { SyncLanEndpoint syncLanEndpoint = new SyncLanEndpoint(); syncLanEndpoint.setLanServerUuid(entry.getKey()); syncLanEndpoint.setRepositoryId(groupId); syncLanEndpoint.setSyncLanClientUuid(syncLanClientUuid); if (!syncLanEndpoints.remove(syncLanEndpoint)) { SyncLanEndpointService.update(syncLanEndpoint); } } } for (SyncLanEndpoint syncLanEndpoint : syncLanEndpoints) { SyncLanEndpointService.deleteSyncLanEndpoint(syncLanEndpoint); } InetSocketAddress inetSocketAddress = datagramPacket.sender(); syncLanClient.setHostname(inetSocketAddress.getHostName()); syncLanClient.setModifiedTime(System.currentTimeMillis()); SyncLanClientService.update(syncLanClient); }
From source file:com.linecorp.armeria.server.http.HttpServiceCodec.java
License:Apache License
@Override public ByteBuf encodeFailureResponse(ServiceInvocationContext ctx, Throwable cause) throws Exception { final StringWriter sw = new StringWriter(512); final PrintWriter pw = new PrintWriter(sw); cause.printStackTrace(pw);//from ww w . ja v a2 s . c o m pw.flush(); return Unpooled.wrappedBuffer(sw.toString().getBytes(CharsetUtil.UTF_8)); }
From source file:com.loudsight.utilities.websocket.WebSocketClientHandler.java
License:Apache License
@Override public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { Channel ch = ctx.channel();/*from ww w.jav a 2 s . co m*/ if (!handshaker.isHandshakeComplete()) { try { handshaker.finishHandshake(ch, (FullHttpResponse) msg); System.out.println("WebSocket Client connected!"); handshakeFuture.setSuccess(); } catch (WebSocketHandshakeException e) { System.out.println("WebSocket Client failed to connect"); handshakeFuture.setFailure(e); } return; } if (msg instanceof FullHttpResponse) { FullHttpResponse response = (FullHttpResponse) msg; throw new IllegalStateException("Unexpected FullHttpResponse (getStatus=" + response.status() + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')'); } WebSocketFrame frame = (WebSocketFrame) msg; if (frame instanceof BinaryWebSocketFrame) { BinaryWebSocketFrame textFrame = (BinaryWebSocketFrame) frame; listener.onMessage(textFrame.content().array()); } else if (frame instanceof PongWebSocketFrame) { System.out.println("WebSocket Client received pong"); } else if (frame instanceof CloseWebSocketFrame) { System.out.println("WebSocket Client received closing"); ch.close(); } }