Example usage for java.net BindException printStackTrace

List of usage examples for java.net BindException printStackTrace

Introduction

In this page you can find the example usage for java.net BindException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:ed.net.lb.LB.java

public static void main(String args[]) throws Exception {

    int verbose = 0;

    for (int i = 0; i < args.length; i++) {
        if (args[i].matches("\\-v+")) {
            verbose = args[i].length() - 1;
            args[i] = "";
        }//from   ww  w  . j a  va2 s . c  om
    }

    Options o = new Options();
    o.addOption("p", "port", true, "Port to Listen On");
    o.addOption("v", "verbose", false, "Verbose");
    o.addOption("mapfile", "m", true, "file from which to load mappings");

    CommandLine cl = (new BasicParser()).parse(o, args);

    final int port = Integer.parseInt(cl.getOptionValue("p", "8080"));

    MappingFactory mf = null;
    if (cl.hasOption("m"))
        mf = new TextMapping.Factory(cl.getOptionValue("m", null));
    else
        mf = new GridMapping.Factory();

    System.out.println("10gen load balancer");
    System.out.println("\t port \t " + port);
    System.out.println("\t verbose \t " + verbose);

    int retriesLeft = 2;

    HttpMonitor.setApplicationType("Load Balancer");

    LB lb = null;

    while (retriesLeft-- > 0) {

        try {
            lb = new LB(port, mf, verbose);
            break;
        } catch (BindException be) {
            be.printStackTrace();
            System.out.println("can't bind to port.  going to try to kill old one");
            HttpDownload.downloadToJS(new URL("http://127.0.0.1:" + port + "/~kill"));
            Thread.sleep(100);
        }
    }

    if (lb == null) {
        System.err.println("couldn't ever bind");
        System.exit(-1);
        return;
    }

    lb.start();
    lb.join();
    System.exit(0);
}

From source file:com.esri.geoevent.test.performance.ClockSync.java

@Override
public void run() {
    DatagramSocket socket = null;
    try {// w  w w  .jav a 2s  .c  o m
        byte[] incomingBuffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(incomingBuffer, incomingBuffer.length);

        ByteBuffer bb = ByteBuffer.allocate(8);
        DatagramPacket outgoingPacket = new DatagramPacket(bb.array(), 0, 8, null, port);
        socket = new DatagramSocket(port);
        socket.setSoTimeout(100);
        while (isRunning.get()) {
            try {
                socket.receive(packet);
                long now = System.currentTimeMillis();
                bb.putLong(now);
                outgoingPacket.setAddress(packet.getAddress());
                outgoingPacket.setPort(packet.getPort());
                socket.send(outgoingPacket);
                bb.clear();
                //System.out.println("Sent the time " + now);
            } catch (SocketTimeoutException ex) {
                // Do nothing if nothing was sent.
            }
        }
    } catch (BindException e) {
        // port is in use - increment and try again
        port++;
        this.run();
    } catch (SocketException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(socket);
    }
}

From source file:org.opennms.mock.snmp.MockSnmpAgent.java

public void run() {
    try {// ww  w  .  ja va 2  s . c  o  m
        init();
        loadConfig(ImportModes.UPDATE_CREATE);
        addShutdownHook();
        finishInit();
        super.run();
        m_running = true;
    } catch (BindException be) {
        be.printStackTrace();
        System.err.println(
                "You probably specified an invalid address or a port < 1024 and are not running as root");
    } catch (IOException ie) {
        ie.printStackTrace();
    }

    while (m_running) {
        try {
            Thread.sleep(10); // fast, Fast, FAST, *FAST*!!!
        } catch (InterruptedException e) {
            break;
        }
    }

    try {
        for (TransportMapping transportMapping : transportMappings) {
            transportMapping.close();
        }
    } catch (Throwable t) {
        t.printStackTrace();
    }

    m_stopped = true;
}

From source file:io.github.thefishlive.updater.HttpServer.java

public void run() {
    try {/*w w w  .  j av  a 2s . co  m*/
        int port = GitUpdater.port;

        // Set up the HTTP protocol processor
        HttpProcessor httpproc = HttpProcessorBuilder.create().add(new ResponseDate())
                .add(new ResponseServer("GitUpdater/1.0-SNAPSHOT")).add(new ResponseContent())
                .add(new ResponseConnControl()).build();

        // Set up request handlers
        UriHttpRequestHandlerMapper reqistry = new UriHttpRequestHandlerMapper();
        reqistry.register("*", new ResponceHandler());

        // Set up the HTTP service
        HttpService httpService = new HttpService(httpproc, reqistry);

        SSLServerSocketFactory sf = null;
        if (port == 8443) {
            // Initialize SSL context
            ClassLoader cl = getClass().getClassLoader();
            URL url = cl.getResource("my.keystore");
            if (url == null) {
                System.out.println("Keystore not found");
                System.exit(1);
            }
            KeyStore keystore = KeyStore.getInstance("jks");
            keystore.load(url.openStream(), "secret".toCharArray());
            KeyManagerFactory kmfactory = KeyManagerFactory
                    .getInstance(KeyManagerFactory.getDefaultAlgorithm());
            kmfactory.init(keystore, "secret".toCharArray());
            KeyManager[] keymanagers = kmfactory.getKeyManagers();
            SSLContext sslcontext = SSLContext.getInstance("TLS");
            sslcontext.init(keymanagers, null, null);
            sf = sslcontext.getServerSocketFactory();
        }

        try {
            Thread t = new RequestListenerThread(port, httpService, sf);
            t.setDaemon(false);
            t.start();
        } catch (BindException ex) {
            System.out.println("Error binding to port " + port);
            System.out.println("Perhaps another server is running on that port");
            return;
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}