nullpointererror +无法绑定套接字(nullpointererror + unable to bind sockets)

编程入门 行业动态 更新时间:2024-10-20 04:00:52
nullpointererror +无法绑定套接字(nullpointererror + unable to bind sockets)

这适用于我的应用程序的早期版本,它基本上是完全相同的代码。 唯一的区别是当前的应用程序使用更高的构建工具和sdk编译版本,会导致代码出错吗? 此外,该应用程序现在扩展FragmentActivity而不是Activity。

其中一个错误是空指针异常,它来自logcat是由于ServerSocket(ss)为空。

另一个错误是libcore.io.ErrnoException:绑定失败:代码段中第8行的EADDRINUSE(地址已在使用中)

我尝试过实现ss.setReuseAddress(true),但由于未解决的符号问题,它无法正常工作?

任何想法或指针将不胜感激!

class CommsThread implements Runnable { private volatile boolean stopFlag = false; private ServerSocket ss ; private static final int SERVERPORT = 6000; public void run() { Socket s = null; try { ss = new ServerSocket(SERVERPORT); <-- bind error here } catch (IOException e) { e.printStackTrace(); Log.e(TAG,"IO Error here"); <--- this is printed in log } try { s = ss.accept(); <----- nullpointer exception here } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } myHandler.sendEmptyMessage(0); while(stopFlag == false){ try { out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream())), true); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } out.printf(String.valueOf(d.format(accel[0]))+"\t"+String.valueOf(d.format(accel[1]))+"\t"+String.valueOf(d.format(accel[2])) + "\tAccelerometer" + "\n"); out.printf(String.valueOf(d.format(gyro[0]))+"\t"+String.valueOf(d.format(gyro[1]))+"\t"+String.valueOf(d.format(gyro[2]))+ "\tGyroscope" + "\n"); out.printf(String.valueOf(d.format(magnet[0]))+"\t"+String.valueOf(d.format(magnet[1]))+"\t"+String.valueOf(d.format(magnet[2]))+"\tMagnetometer"+ "\n\n"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if( s==null) { stopComms(); } */ } }

This works in the earlier version of my app, which is basically the exact same code. The only difference is the current app uses a higher build tools and sdk compiled version, would that cause the code to error? Also the app extends FragmentActivity instead of Activity now.

One of the errors is a null pointer exception, which from logcat is due to the ServerSocket (ss) being null.

The other error is libcore.io.ErrnoException: bind failed: EADDRINUSE (Address already in use) at the line 8 in the snippet

I have tried implementing ss.setReuseAddress(true), however it doesn't work due to an unresolved symbol issue?

Any idea or pointers would be greatly appreciated!

class CommsThread implements Runnable { private volatile boolean stopFlag = false; private ServerSocket ss ; private static final int SERVERPORT = 6000; public void run() { Socket s = null; try { ss = new ServerSocket(SERVERPORT); <-- bind error here } catch (IOException e) { e.printStackTrace(); Log.e(TAG,"IO Error here"); <--- this is printed in log } try { s = ss.accept(); <----- nullpointer exception here } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } myHandler.sendEmptyMessage(0); while(stopFlag == false){ try { out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream())), true); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } out.printf(String.valueOf(d.format(accel[0]))+"\t"+String.valueOf(d.format(accel[1]))+"\t"+String.valueOf(d.format(accel[2])) + "\tAccelerometer" + "\n"); out.printf(String.valueOf(d.format(gyro[0]))+"\t"+String.valueOf(d.format(gyro[1]))+"\t"+String.valueOf(d.format(gyro[2]))+ "\tGyroscope" + "\n"); out.printf(String.valueOf(d.format(magnet[0]))+"\t"+String.valueOf(d.format(magnet[1]))+"\t"+String.valueOf(d.format(magnet[2]))+"\tMagnetometer"+ "\n\n"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if( s==null) { stopComms(); } */ } }

最满意答案

NPE只是你自己错误的代码结构。 真正的问题是绑定异常,这意味着其他东西正在侦听端口6000,可能是同一应用程序的先前实例。 解决此问题,或使用其他端口。

The NPE is due to nothing but your own faulty code structure. The real issue is the bind exception, which means something else is listening at port 6000, possibly a prior instance of the same application. Fix that, or use another port.

更多推荐

本文发布于:2023-04-28 01:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329700.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   nullpointererror   unable   sockets   bind

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!