我正在尝试使用在github上找到的开源项目。
此项目正在使用套接字与2或模式设备进行通信...
我的问题是,当代码发现服务器(平板电脑)时,我收到此错误:
W/System.err: at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:594)
W/System.err: at libcore.io.IoBridge.recvfrom(IoBridge.java:552)
W/System.err: at java.net.PlainDatagramSocketImpl.doRecv(PlainDatagramSocketImpl.java:163)
W/System.err: at java.net.PlainDatagramSocketImpl.receive(PlainDatagramSocketImpl.java:171)
W/System.err: at java.net.DatagramSocket.receive(DatagramSocket.java:274)
W/System.err: at edu.vub.at.commlib.CommLib.discover(CommLib.java:117)
W/System.err: at edu.vub.at.nfcpoker.comm.DiscoveryAsyncTask.doInBackground(DiscoveryAsyncTask.java:60)
W/System.err: at edu.vub.at.nfcpoker.comm.DiscoveryAsyncTask.doInBackground(DiscoveryAsyncTask.java:35)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
W/System.err: at java.lang.Thread.run(Thread.java:818)
W/System.err: Caused by: android.system.ErrnoException: recvfrom failed: EAGAIN (Try again)
W/System.err: at libcore.io.Posix.recvfromBytes(Native Method)
W/System.err: at libcore.io.Posix.recvfrom(Posix.java:189)
W/System.err: at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:250)
W/System.err: at libcore.io.IoBridge.recvfrom(IoBridge.java:549)
W/System.err: ... 12 more
我的方法是:
public static CommLibConnectionInfo discover(Class<?> klass, String broadcastAddress) throws IOException {
final String targetClass = klass.getCanonicalName();
Log.e(TAG, "discover: targetClass "+ targetClass);
Kryo k = new Kryo();
k.setRegistrationRequired(false);
k.register(CommLibConnectionInfo.class);
k.register(UUID.class, new UUIDSerializer());
DatagramSocket ds = new DatagramSocket(DISCOVERY_PORT, InetAddress.getByName(broadcastAddress));
ds.setBroadcast(true);
ds.setReuseAddress(true);
ds.setSoTimeout(DISCOVERY_TIMEOUT);
DatagramPacket dp = new DatagramPacket(new byte[1024], 1024);
try {
ds.receive(dp);
CommLibConnectionInfo clci = k.readObject(new Input(dp.getData()), CommLibConnectionInfo.class);
if (clci.serverType_.equals(targetClass)) {
ds.close();
return clci;
}
return null;
}catch (SocketException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
ds.close();
}
}
此方法在线上发出异常:ds.receive(dp);
该应用程序在Nexus和Lenovo上运行,但在三星设备上我正在接受此例外