Help with Netty use on Android
cobe24
qu_en2008 at yahoo.com.cn
Mon Nov 15 03:29:34 EST 2010
Hi everyone,
I'm new to Netty, and I'm trying to use Netty to write a server on Android
device.
I encountered a problem with Netty binding to a port on Android device, the
code is like below:
int portNum = 25336;
bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(), Executors
.newCachedThreadPool()));
bootstrap.setOption("keepAlive", true);
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.keepAlive", true);
bootstrap.setOption("child.reuseAddress", true);
bootstrap.setOption("child.connectTimeoutMillis", 100);
bootstrap.setOption("readWriteFair", true);
// channel group is used for closing connection properly
channelGroup = new DefaultChannelGroup();
// Configure the pipeline factory.
ServerPipelineFactory pipelineFactory = new ServerPipelineFactory();
pipelineFactory.setInstrumentation(this);
bootstrap.setPipelineFactory(pipelineFactory);
// NOTE: This is a work around to prevent the bad address error.
// This was a bug exposed on Android 2.2
// http://code.google.com/p/android/issues/detail?id=9431
System.setProperty("java.net.preferIPv6Addresses", "false");
// Bind and start to accept incoming connections.
Channel channel = null;
try{
byte ip[] = new byte[] { 10, 0, 2, 15};
InetAddress address1 = InetAddress.getByAddress(ip);
//channel = bootstrap.bind(new
InetSocketAddress(InetAddress.getLocalHost(), portNum));
channel = bootstrap.bind(new InetSocketAddress(address1, portNum));
}
catch(ChannelException e){
Log.e(TAG, "ChannelException "+e.getMessage()+"at port: "+portNum);
}
catch(Exception e){
Log.e(TAG, "exception message:"+e.getMessage());
}
channelGroup.add(channel);
My problem is that whether I specify an ip explicitly(10.0.2.15), or bind to
the loopback ip(commented out), the calling to ServerBootstrap.bind(...)
always throws an ChannelException, and it says that "failed to open a server
socket".
I've already added the permisson below to Android Manifest file:
I'm wondering if I'm not using Netty correctly.
And if you have experienced such problems before? If so, how did you resolve
it?
Thanks a lot!
--
View this message in context: http://netty-forums-and-mailing-lists.685743.n2.nabble.com/Help-with-Netty-use-on-Android-tp5739212p5739212.html
Sent from the Netty User Group mailing list archive at Nabble.com.
More information about the netty-users
mailing list