Last active
August 29, 2015 14:02
-
-
Save crised/74932e01f7462a171a1a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package client; | |
import io.netty.bootstrap.Bootstrap; | |
import io.netty.channel.ChannelOption; | |
import io.netty.channel.EventLoopGroup; | |
import io.netty.channel.nio.NioEventLoopGroup; | |
import io.netty.channel.socket.nio.NioDatagramChannel; | |
/** | |
* Created by crised on 6/15/14. | |
*/ | |
//es cliente, pero tambien es servidor porque escucha mensaje devuelta. | |
public class Client { | |
public static void main(String[] args) throws Exception { | |
EventLoopGroup group = new NioEventLoopGroup(); | |
try { | |
Bootstrap b = new Bootstrap(); | |
b.group(group) | |
.channel(NioDatagramChannel.class) | |
.option(ChannelOption.SO_BROADCAST, true) | |
.handler(new ChannelInitializer<DatagramChannel>() { | |
@Override | |
public void initChannel(DatagramChannel ch) throws Exception{ | |
ch.pipeline().addLast(new OutboundHandler()); | |
} | |
}); | |
b.bind(0); | |
} finally { | |
group.shutdownGracefully(); | |
} | |
} | |
} | |
package client; | |
import io.netty.buffer.ByteBuf; | |
import io.netty.buffer.Unpooled; | |
import io.netty.channel.ChannelFuture; | |
import io.netty.channel.ChannelHandlerContext; | |
import io.netty.channel.ChannelOutboundHandlerAdapter; | |
import io.netty.channel.ChannelPromise; | |
import io.netty.channel.socket.DatagramPacket; | |
import io.netty.util.CharsetUtil; | |
import java.net.InetSocketAddress; | |
import java.net.SocketAddress; | |
/** | |
* Created by crised on 6/15/14. | |
*/ | |
public class OutboundHandler extends ChannelOutboundHandlerAdapter{ | |
@Override | |
public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception { | |
System.out.println("Bind happened"); | |
InetSocketAddress ipSocketAddress = new InetSocketAddress("255.255.255.255", 7686); | |
ByteBuf byteBuf = Unpooled.copiedBuffer("QOTM?", CharsetUtil.UTF_8); | |
DatagramPacket udpPacket = new DatagramPacket(byteBuf, ipSocketAddress); | |
ChannelFuture future = ctx.writeAndFlush(udpPacket); | |
System.out.println(future.isDone()); | |
ctx.close(); | |
System.out.println("bind method ended"); | |
} | |
@Override | |
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { | |
System.out.println("Write operation has been done"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment