Netty UDP receiver fails on Android but not on desktop -
i have netty implementation class works fine on desktop fails on android. i'm confused. netty 4.0.50.final android studio 2.3.3 on os x 10.11.6 on samsung galaxy s5 phone. build.gradle file included @ bottom of post.
this class uses netty works on desktop (receives both direct , broadcast udp packets interface) not on android (receives no udp packets sent either full ip or broadcast address):
import org.slf4j.logger; import org.slf4j.loggerfactory; import java.util.concurrent.callable; import io.netty.bootstrap.bootstrap; import io.netty.channel.channel; import io.netty.channel.channelhandlercontext; import io.netty.channel.channeloption; import io.netty.channel.eventloopgroup; import io.netty.channel.simplechannelinboundhandler; import io.netty.channel.nio.nioeventloopgroup; import io.netty.channel.socket.datagrampacket; import io.netty.channel.socket.nio.niodatagramchannel; public class udpreceiver implements callable<integer> { private static final logger log = loggerfactory.getlogger(udpreceiver.class); public final int dp_incoming_port = 6969; public class udpupstreamhandler extends simplechannelinboundhandler<datagrampacket> { public udpupstreamhandler() { super(); log.info("instantiated upstreamhandler"); } @override protected void channelread0(channelhandlercontext ctx, datagrampacket msg) throws exception { log.info("got packet"); } } @override public integer call() throws exception { log.info("setting udp broadcast receiver"); eventloopgroup group = new nioeventloopgroup(); try { bootstrap b = new bootstrap(); b.group(group) .channel(niodatagramchannel.class) .option(channeloption.so_broadcast, true) .handler(new udpupstreamhandler()); channel channel = b.bind(dp_incoming_port).sync().channel(); channel.closefuture().await(); } { log.info("gracefully shutting down udp broadcast receiver"); group.shutdowngracefully(); } log.info("shutting down udp broadcast receiver"); return null; } }
this class (which doesn't use netty) works , receives packets on both desktop , android, it's not android setup (literally recomment 2 lines in application source code).
import android.util.log; import org.slf4j.logger; import org.slf4j.loggerfactory; import java.net.datagrampacket; import java.net.datagramsocket; import java.net.sockettimeoutexception; import java.util.concurrent.callable; public class udpbroadcastreceiver implements callable<integer> { private static final logger log = loggerfactory.getlogger(udpreceiver.class); public final int dp_incoming_port = 6969; public final long interval_millis = 10000; volatile boolean ffabort = false; long llpacketcount = 0; long lltimeoutcount = 0; public void shutdowngracefully() { ffabort = true; } @override public integer call() throws exception { datagramsocket socket; long llmarkmillis; long llcurrentmillis; byte[] recvbuf = new byte[15000]; socket = new datagramsocket(dp_incoming_port); socket.setbroadcast(true); socket.setsotimeout(1000); datagrampacket packet = new datagrampacket(recvbuf, recvbuf.length); log.info("entering udp broadcast receiver"); llmarkmillis = system.currenttimemillis(); while(!ffabort) { llcurrentmillis = system.currenttimemillis(); if (llcurrentmillis < llmarkmillis || (llcurrentmillis > llmarkmillis+interval_millis)) { log.info("got udp packets: "+llpacketcount+":"+lltimeoutcount); llmarkmillis = llcurrentmillis; } try { socket.receive(packet); llpacketcount += 1; } catch (sockettimeoutexception ee) { lltimeoutcount += 1; } } log.v("tag", "ending udp receive thread"); socket.close(); return null; } }
possible android oddity--i see these lines in logcat, doesn't crash program:
09-10 16:06:29.697 1809-2060/org.allcaps.decodejni i/art: rejecting re-init on previously-failed class java.lang.class<io.netty.util.internal.longaddercounter> 09-10 16:06:29.697 1809-2060/org.allcaps.decodejni i/art: rejecting re-init on previously-failed class java.lang.class<io.netty.util.internal.longaddercounter>
any appreciated. thanks.
build.gradle file attached seems cause many problems:
apply plugin: 'com.android.application' android { sourcecompatibility = 1.7 targetcompatibility = 1.7 compilesdkversion 26 buildtoolsversion "26.0.0" defaultconfig { applicationid "org.allcaps.decodejni" minsdkversion 23 targetsdkversion 26 versioncode 1 versionname "1.0" testinstrumentationrunner "android.support.test.runner.androidjunitrunner" externalnativebuild { cmake { cppflags "-std=c++11 -werror" } } ndk { abifilters 'armeabi-v7a' } } buildtypes { release { minifyenabled false proguardfiles getdefaultproguardfile('proguard-android.txt'), 'proguard-rules.pro' } } lintoptions { abortonerror false // prevent broken linter sfouling things ... } // sourcesets { // main { // // make gradle pack shared libraries finto apk // jnilibs.srcdirs = ['../distribution/gperf/lib'] // } // } externalnativebuild { cmake { path "cmakelists.txt" } } } dependencies { compile filetree(dir: 'libs', include: ['*.jar']) androidtestcompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' compile 'com.android.support:design:26.+' compile 'org.slf4j:slf4j-api:1.7.25' compile 'org.slf4j:slf4j-simple:1.7.25' compile 'io.netty:netty-all:4.0.50.final' //compile group: 'io.netty', name: 'netty-all', version: '4.0.50.final' //compile group: 'io.netty', name: 'netty-handler', version: '4.0.51.final' testcompile 'junit:junit:4.12' }
Comments
Post a Comment