【发布时间】:2014-07-07 16:48:05
【问题描述】:
我正在做套接字编程。我参考了以下链接:
http://examples.javacodegeeks.com/android/core/socket-core/android-socket-example/
以下是有关我的问题的详细信息。我为此 ServerThread 创建了 Android 库(我的项目要求),并在测试应用程序中使用。
现在测试应用程序通过 lib 连接到此并执行该过程。第一次它工作得很好,但如果我关闭并重新打开它会异常崩溃:
“EADDRINUSE(地址已在使用中)”
也试过serverSocket.setReuseAddress(true)这个但没有运气。
我的代码:
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。通信线程与上一个链接中给出的相同。
【问题讨论】:
-
与此处相同的问题...stackoverflow.com/questions/20068710/…
-
你用的是什么类型的socket?
-
如何使用套接字?您能创建一个Minimal, Complete, and Verifiable example 并展示给我们看吗?
-
请不要在评论中发布代码。改为编辑您的问题!
-
@JoachimPileborg 在主要问题中添加了代码.. :)