套接字重新连接存在很大问题。我有用Delphi编写的套接字服务器,但通信应用程序必须在java(android)中。问题是我的服务器停止了几分钟 - 例如重启或类似的东西。 在这种情况下java(android)客户端粉碎(由错误停止)本身。 我在远程服务中编写客户端,它在两个线程中工作。 第一个用于连接或重新连接:
thrd1 = new Thread(new Runnable() {
public void run() {
while (!Thread.interrupted()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
if (sock==null)
try {
sock = new Socket("address", 5000);
r = new BufferedReader(new InputStreamReader(sock.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
} catch (UnknownHostException e) {
} catch (IOException e) {
}
}
第二种是定期向服务器发送数据:
thrd2 = new Thread(new Runnable() {
public void run() {
while (!Thread.interrupted()) {
try {
if (sock != null) {
out.write("TEST DATA\n");
out.flush();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
sock = null;
out = null;
r = null;
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
}
}
}
});
为什么我的应用程序在连接丢失后会死亡?如何在服务器不工作的很长一段时间后进行正确的重新连接(换句话说:如何等待启动服务器并在服务器启动后立即连接它。
感谢Lois我改进了我的代码,现在它看起来像:
thrd2 = new Thread(new Runnable() {
public void run() {
while (!Thread.interrupted()) {
try {
if (sock != null) {
out.write("TEST DATA\n");
out.flush();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//if sock is null wait 300ms
else {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
try {
sock=null;
Thread.sleep(1000);
} catch (InterruptedException e1) {
e.printStackTrace();
}
}
}
}
和
thrd1 = new Thread(new Runnable() {
public void run() {
while (!Thread.interrupted()) {
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
}
if (sock==null)
try {
sock = new Socket();
sock.connect(new InetSocketAddress(
address, 5000), 300);
r = new BufferedReader(new InputStreamReader(
sock.getInputStream()));
out = new BufferedWriter(
new OutputStreamWriter(sock
.getOutputStream()));
if ((thrd2!=null)&&(!thrd2.isAlive()))
thrd2.start();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
;
}
});
if ((thrd1!=null)&&(!thrd1.isAlive())) thrd1.start();
应用程序没有粉碎但我获得了奇怪的通信过程。服务器在服务器唤醒时向我提供两个或更多(取决于断开服务器的时间)尝试从套接字客户端启动(初始化)连接的信息?我认为这是不正确的 - 在服务器醒来之后,它必须只有一次尝试(如果它是成功的)开始连接?顺便说一下:从这个帖子我知道每个catch都必须有实现 - 我的意思是在异常之后“try”子句去那里时要做的一些代码(不是空代码)。但是当我只想保护我的应用程序免于错误时我必须放在那里但是对这种情况的反应在我的代码中不需要任何特殊的反应?
下面是一些日志说明这种情况(抱歉,我在第一篇文章中忘记了这个帖子主要活动是com.example.aj.siec:
12-09 10:09:29.750: W/System.err(1799): java.net.SocketTimeoutException: Connection timed out
12-09 10:09:29.750: W/System.err(1799): at org.apache.harmony.luni.platform.OSNetworkSystem.connect(Native Method)
12-09 10:09:29.750: W/System.err(1799): at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:357)
12-09 10:09:29.750: W/System.err(1799): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:204)
12-09 10:09:29.750: W/System.err(1799): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:437)
12-09 10:09:29.750: W/System.err(1799): at java.net.Socket.connect(Socket.java:983)
12-09 10:09:29.750: W/System.err(1799): at com.example.aj.siec.MainActivity$1$2.run(MainActivity.java:117)
12-09 10:09:29.750: W/System.err(1799): at java.lang.Thread.run(Thread.java:1019)
12-09 10:09:30.830: W/System.err(1799): java.net.SocketTimeoutException: Connection timed out
12-09 10:09:30.830: W/System.err(1799): at org.apache.harmony.luni.platform.OSNetworkSystem.connect(Native Method)
12-09 10:09:30.830: W/System.err(1799): at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:357)
12-09 10:09:30.830: W/System.err(1799): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:204)
12-09 10:09:30.830: W/System.err(1799): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:437)
12-09 10:09:30.830: W/System.err(1799): at java.net.Socket.connect(Socket.java:983)
12-09 10:09:30.830: W/System.err(1799): at com.example.aj.siec.MainActivity$1$2.run(MainActivity.java:117)
12-09 10:09:30.830: W/System.err(1799): at java.lang.Thread.run(Thread.java:1019)
12-09 10:09:31.800: W/System.err(1799): java.net.SocketTimeoutException: Connection timed out
12-09 10:09:31.800: W/System.err(1799): at org.apache.harmony.luni.platform.OSNetworkSystem.connect(Native Method)
12-09 10:09:31.800: W/System.err(1799): at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:357)
12-09 10:09:31.800: W/System.err(1799): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:204)
12-09 10:09:31.800: W/System.err(1799): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:437)
12-09 10:09:31.800: W/System.err(1799): at java.net.Socket.connect(Socket.java:983)
12-09 10:09:31.800: W/System.err(1799): at com.example.aj.siec.MainActivity$1$2.run(MainActivity.java:117)
12-09 10:09:31.800: W/System.err(1799): at java.lang.Thread.run(Thread.java:1019)
谢谢你的回答 - 解决这个问题对我来说真的很重要!
此致
ARTIK
答案 0 :(得分:1)
您没有显示堆栈跟踪,因此我将在上面的代码中指出一些即时错误:
thrd2
thrd1
thrd2
当socket为null时,不要点击sleep()
并继续循环消耗100%的CPU时间。问候。