插座EADDRINUSE(地址已在使用)

编程入门 行业动态 更新时间:2024-10-28 16:22:32
本文介绍了插座EADDRINUSE(地址已在使用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我做的socket编程。

I am doing socket programming.

我从下面的链接托克参考:

I tok reference from below link:

的examples.java$c$cgeeks/android/core/socket-core/android-socket-example/

下面是我的问题的细节:我创建的Andr​​oid的lib这个ServerThread(我的项目要求)。这是在测试程序使用。

Below is detail about my issue: I have created android lib for this ServerThread (my project requirement). and this is used in test app.

现在测试应用程序通过LIB连接到这一点,并做处理。第一次工作完全正常,但如果我关闭并重新打开它,出现异常崩溃的EADDRINUSE(地址已在使用)

Now test app connect to this through lib and do the process. first time it works perfectly fine, but if I closed and reopen it crashed with exception "EADDRINUSE (Address already in use)"

也试过serverSocket.setReuseAddress(真);这一点,但没有运气...

Also tried serverSocket.setReuseAddress(true); this but no luck...

我的code:

public void run() { Socket socket = null; try { serverSocket = new ServerSocket(SERVER_PORT); serverSocket.setReuseAddress(true); } catch (IOException e) { Log.e(TAG, "exception1= " + e.getMessage()); } while (!Thread.currentThread().isInterrupted()) { try { socket = serverSocket.accept(); Log.d(TAG, "server Connected.......!!!!"); communicationThread = new CommunicationThread( socket); commThread = new Thread(communicationThread); commThread.start(); } catch (IOException e) { Log.e(TAG, "exception 2=" + e.getMessage()); } } }

如果我叫serverSocket.close()我得到异常2作为服务器套接字关闭。 在previous链接给通信线程是一样的。

If I call serverSocket.close() I am getting exception 2 as server socket close. Communication thread is same as given in previous link.

推荐答案

您必须调用 setReuseAddress(真)前的套接字绑定到的端口。你是后调用它,因为你传递端口的构造,这将套接字绑定立即

You have to call setReuseAddress(true) before the socket is bound to the port. You are calling it after, because you are passing the port to the constructor, which will bind the socket immediately.

试试这个:

serverSocket = new ServerSocket(); // <-- create an unbound socket first serverSocket.setReuseAddress(true); serverSocket.bind(new InetSocketAddress(SERVER_PORT)); // <-- now bind it

更多推荐

插座EADDRINUSE(地址已在使用)

本文发布于:2023-08-07 14:03:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319468.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:已在   插座   地址   EADDRINUSE

发布评论

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

>www.elefans.com

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