gpt4 book ai didi

java - 仅在启动时启动时绑定(bind)异常 [Raspbian]

转载 作者:搜寻专家 更新时间:2023-11-01 03:18:35 25 4
gpt4 key购买 nike

我在我的 Raspberry 启动期间启动了一个 java 程序(它正在打开 Raspbian)。我使用 /etc/rc.local 文件来执行此操作并且它正常工作(我也尝试过/etc/init.d/解决方案,但我更喜欢另一个) .我直接在控制台模式下启动我的 raspbian,这样我就可以看到我的程序的输出。

我的问题是:手动启动我的应用程序,效果很好。在启动时自动启动,它会引发绑定(bind)异常:无法分配请求的地址。

启动程序

/etc/rc.local 文件中,我写了这一行。它启动一个脚本来启动我的 .jar 程序。

#! /bin/sh -e
#
# rc.local
#
# [...]

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
# \/ Added \/
/home/pi/Documents/DinnerTimePi/startApp

exit 0

我的startApp 脚本包含:

#! /bin/sh

cd /home/pi/Documents/DinnerTimePi/

date >> testStart.txt #Only for checking it runs at launch
java -jar DinnerTimeServer.jar

exit 0

正常程序输出

Server initialised at : 192.168.1.35 address and : 35150 port.
_ #(Waiting for client to connect)

Java程序部分

我的 Java 程序是一个 ServerSocket 做一些事情,它也能正常工作。

public class TimeServer {
//Instance of class : can have only one server at a time
static private TimeServer instance;
//Default values
static private int port = 35150;
static private String host = "192.168.1.35"; //Adapt for your network, give a fix IP by DHCP
//Variables
private ServerSocket server = null;


protected TimeServer(String pHost, int pPort){ // <- I'm running this constructor with the default values above
list = new LinkedList<ClientProcessor>();
host = pHost; // 192.168.1.135
port = pPort; // 35150
try {
// \/ Line 57 on my code (see stack trace) \/
server = new ServerSocket(port, 10, InetAddress.getByName(host));
} catch (BindException bind){
bind.printStackTrace(); //Here is the problem !!
System.exit(1);
} catch (UnknownHostException hoste) {
hoste.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}

如您所见,我使用了 IP 地址 192.168.1.35,它在我的调制解调器上是静态,并且运行良好。我假设在启动期间使用了该端口,所以我尝试了不同的端口,如 6543 和 35150。但它做同样的事情:手动工作,但在自动启动期间不工作。

堆栈跟踪

这是我在启动 Raspberry 时看到的输出。

[...]
[ OK ] Reached target Network is Online.
Starting LSB: Start NTP deamon...
[ OK ] Started LSB: Start NTP deamon.
java.net.BindException: Can't assign requested address
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:387)
at java.net.ServerSocket.bind(ServerSocket.java:375)
at java.net.ServerSocket.<init>(ServerSocket.java:237)
at server.TimeServer.<init>(TimeServer.java:57)
at server.TimeServer.getInstance(TimeServer.java:32)
at server.Main_Server.main(Main_Server.java:12)
[ OK ] Started /etc/rc.local Compatibility.
Starting Terminate Plymouth Boot Screen...
[...]

我不明白的是为什么我的程序在我自己启动时运行良好,而在系统启动时却不行。我的程序没有 root 需求

我希望它很清楚,如果您需要更多信息,请不要犹豫。

感谢帮助! :)

(如果需要,请在 github 上查看整个程序,自动启动分支)

最佳答案

将构造函数的行扔到特定的函数中:

protected boolean initThis(String pHost, int pPort) {
list = new LinkedList<ClientProcessor>();
host = pHost; // 192.168.1.135
port = pPort; // 35150
try {
// \/ Line 57 on my code (see stack trace) \/
server = new ServerSocket(port, 10, InetAddress.getByName(host));
} catch (BindException bind){
bind.printStackTrace(); //Here is the problem !!
return false;
} catch (UnknownHostException hoste) {
hoste.printStackTrace();
return false; // Change this to true if you want it to stop here
} catch (IOException ioe) {
ioe.printStackTrace();
return false; // Change to true to stop here
}
return true;
}

现在在你的构造函数中:

protected TimeServer(String pHost, int pPort) {
while(!initThis(pHost, pPort))
Thread.sleep(500); // Wait 0.5 secs before retry
}

这将重试很多次直到成功。

关于java - 仅在启动时启动时绑定(bind)异常 [Raspbian],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38850351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com