Bind Exception only on startup at boot [Raspbian]

I run a java program during the launch of Raspberry (which includes Raspbian). I used the file /etc/rc.localto do this and it works correctly (I also tried the solution /etc/init.d/, but I prefer the other). I run my raspbian directly in console mode, so I can see the output of my program.

My problem: By manually launching my application, it works well. Starting automatically at boot, it throws a Bind exception: cannot assign the requested address.

Program launch

In the /etc/rc.local file, I wrote this line. It runs a script that runs my .jar program.

#! /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

My startApp script :

#! /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. : , .

, .

[...]
[ 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, autolaunch)

+4
1

:

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
}

, .

+2

Source: https://habr.com/ru/post/1650666/


All Articles