Example usage for java.net ServerSocket ServerSocket

List of usage examples for java.net ServerSocket ServerSocket

Introduction

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

Prototype

public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException 

Source Link

Document

Create a server with the specified port, listen backlog, and local IP address to bind to.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    ServerSocket serverSocket = new ServerSocket(12900, 100, InetAddress.getByName("localhost"));
    System.out.println("Server started  at:  " + serverSocket);

    while (true) {
        System.out.println("Waiting for a  connection...");

        final Socket activeSocket = serverSocket.accept();

        System.out.println("Received a  connection from  " + activeSocket);
        Runnable runnable = () -> handleClientRequest(activeSocket);
        new Thread(runnable).start(); // start a new thread
    }//from w ww.ja  v  a 2  s  .  c  om
}

From source file:Main.java

static public void main(String args[]) throws Exception {
    int port = 80;

    NetworkInterface ni = NetworkInterface.getByName("name");
    Enumeration e = ni.getInetAddresses();
    if (!e.hasMoreElements())
        return;/*from ww w  .  j a  v  a2  s  .  c o  m*/
    InetAddress ia = (InetAddress) e.nextElement();

    ServerSocket ss = new ServerSocket(port, 20, ia);
    System.out.println("Listening");
    Socket s = ss.accept();
    System.out.println(s);
}

From source file:org.apache.cassandra.stress.StressServer.java

public static void main(String[] args) throws Exception {
    ServerSocket serverSocket = null;
    CommandLineParser parser = new PosixParser();

    InetAddress address = InetAddress.getByName("127.0.0.1");

    try {//ww w.j  a  v a2  s. c om
        CommandLine cmd = parser.parse(availableOptions, args);

        if (cmd.hasOption("h")) {
            address = InetAddress.getByName(cmd.getOptionValue("h"));
        }
    } catch (ParseException e) {
        System.err.printf("Usage: ./bin/stressd start|stop|status [-h <host>]");
        System.exit(1);
    }

    try {
        serverSocket = new ServerSocket(2159, 0, address);
    } catch (IOException e) {
        System.err.printf("Could not listen on port: %s:2159.%n", address.getHostAddress());
        System.exit(1);
    }

    for (;;)
        new StressThread(serverSocket.accept()).start();
}

From source file:fm.last.irccat.IRCCat.java

public static void main(String[] args) throws Exception {
    try {//from   w ww  . jav a  2s  . com
        if (args.length == 0) {
            System.out.println("first param should be config file");
            System.exit(-1);
        }
        XMLConfiguration c = null;
        try {
            c = new XMLConfiguration(args[0]);
        } catch (ConfigurationException cex) {
            System.err.println("Configuration error, check config file");
            cex.printStackTrace();
            System.exit(1);
        }

        IRCCat bot = new IRCCat(c);

        // listen for stuff and send it to irc:
        ServerSocket serverSocket = null;
        InetAddress inet = null;
        try {
            if (bot.getCatIP() != null)
                inet = InetAddress.getByName(bot.getCatIP());
        } catch (UnknownHostException ex) {
            System.out.println("Could not resolve config cat.ip, fix your config");
            ex.printStackTrace();
            System.exit(2);
        }

        try {
            serverSocket = new ServerSocket(bot.getCatPort(), 0, inet);
        } catch (IOException e) {
            System.err.println("Could not listen on port: " + bot.getCatPort());
            System.exit(1);
        }

        System.out.println("Listening on " + bot.getCatIP() + " : " + bot.getCatPort());

        while (true) {
            try {
                Socket clientSocket = serverSocket.accept();
                // System.out.println("Connection on catport from: "
                // + clientSocket.getInetAddress().toString());
                CatHandler handler = new CatHandler(clientSocket, bot);
                handler.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:com.ls.zencat.ZenCat.java

public static void main(String[] args) throws Exception {
    try {/*from w w w. j  av a 2 s. c  o  m*/
        if (args.length == 0) {
            System.out.println("first param should be config file");
            System.exit(-1);
        }
        XMLConfiguration c = null;
        try {
            c = new XMLConfiguration(args[0]);
        } catch (ConfigurationException cex) {
            System.err.println("Configuration error, check config file");
            cex.printStackTrace();
            System.exit(1);
        }

        ZenCat bot = new ZenCat(c);

        // listen for stuff and send it to irc:
        ServerSocket serverSocket = null;
        InetAddress inet = null;
        try {
            if (bot.getCatIP() != null)
                inet = InetAddress.getByName(bot.getCatIP());
        } catch (UnknownHostException ex) {
            System.out.println("Could not resolve config cat.ip, fix your config");
            ex.printStackTrace();
            System.exit(2);
        }

        try {
            serverSocket = new ServerSocket(bot.getCatPort(), 0, inet);
        } catch (IOException e) {
            System.err.println("Could not listen on port: " + bot.getCatPort());
            System.exit(1);
        }

        System.out.println("Listening on " + bot.getCatIP() + " : " + bot.getCatPort());

        while (true) {
            try {
                Socket clientSocket = serverSocket.accept();
                // System.out.println("Connection on catport from: "
                // + clientSocket.getInetAddress().toString());
                CatHandler handler = new CatHandler(clientSocket, bot);
                handler.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:Logi.GSeries.Service.LogiGSKService.java

/**
 * @param args the command line arguments
 *///from w  ww.jav  a 2 s .  c  om
public static void main(String[] args) {
    SystemTray.DEBUG = false;

    Settings settings;
    if (IOOperations.currentSettingsExist()) {
        settings = IOOperations.loadCurrentSettingsObjectFromFile();
    } else {
        settings = new Settings();
    }

    LogiGSKService l = new LogiGSKService();
    if (settings.getShowSystemTray()) {
        l.showSystemTray();
    } else {
        l.hideSystemTray();
    }
    l.begin();
    try {
        String dataFolderPath = IOOperations.getLocalDataDirectoryPath();
        File dataFolder = new File(dataFolderPath);
        if (!dataFolder.exists()) {
            dataFolder.mkdir();
        }
        String logFolderPath = IOOperations.getLogDirectoryPath();
        File logFolder = new File(logFolderPath);
        if (!logFolder.exists()) {
            logFolder.mkdir();
        }
        FileHandler fileHandler = new FileHandler(logFolderPath + "LogiGSK.log", FILE_SIZE, 3);
        fileHandler.setLevel(Level.ALL);
        logger.setLevel(Level.ALL);
        logger.addHandler(fileHandler);
    } catch (IOException | SecurityException ex) {
        Logger.getLogger(LogiGSKService.class.getName()).log(Level.SEVERE, null, ex);
    }
    int clientNumber = 0;
    ServerSocket listener = null;
    PropertyConfigurator.configure(LogiGSKService.class.getResource("/Logi/GSeries/Service/log4j.properties"));
    reloading = true;
    boolean firstTime = true;
    while (reloading) {
        listener = null;
        if (reloading && !firstTime) {
            if (IOOperations.currentSettingsExist()) {
                settings = IOOperations.loadCurrentSettingsObjectFromFile();
            } else {
                settings = new Settings();
            }
        }
        firstTime = false;
        reloading = false;
        running = true;
        try {
            listener = new ServerSocket(settings.getPort(), 0, InetAddress.getByName(null));
            while (running) {
                new Manager(listener.accept(), clientNumber++, logger).start();
            }
        } catch (IOException ex) {
            Logger.getLogger(LogiGSKService.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (listener != null) {
                try {
                    listener.close();
                } catch (IOException ex) {
                    Logger.getLogger(LogiGSKService.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}

From source file:Main.java

public Main() throws IOException {
    serverSocket = new ServerSocket(8008, 1000, InetAddress.getByName("java2s.com"));
    serverSocket.setSoTimeout(10000);//from w  ww . j a va  2  s. c o  m

}

From source file:org.alfresco.util.PortUtil.java

/**
 * Check if specified port is free./*from  w  ww .  j a va2  s .  com*/
 * @param port Port number to check.
 * @param host A local address to bind to; if null, "" or "0.0.0.0" then all local addresses will be considered.
 */
public static void checkPort(int port, String host) throws IOException {
    ServerSocket serverSocket = null;

    try {
        if (host != null && !host.equals("") && !"0.0.0.0".equals(host.trim())) {
            serverSocket = new ServerSocket(port, 0, InetAddress.getByName(host.trim()));
        } else {
            serverSocket = new ServerSocket(port);
        }
    } finally {
        if (serverSocket != null) {
            try {
                serverSocket.close();
            } catch (IOException ioe) {
                if (logger.isDebugEnabled()) {
                    logger.debug(ioe.toString());
                }
            }
        }
    }
}

From source file:de.ailis.oneinstance.OneInstanceServer.java

/**
 * Constructor.//  w  w w.  j ava 2  s . c om
 * 
 * @param appId
 *            The application name.
 * @param port
 *            The application port.
 * @throws PortAlreadyInUseException
 *             When port is already in use.
 */
OneInstanceServer(final String appId, final int port) throws PortAlreadyInUseException {
    this.appId = appId;
    this.port = port;

    try {
        this.socket = new ServerSocket(this.port, 50, InetAddress.getByName(null));
        this.socket.setSoTimeout(250);
    } catch (final IOException e) {
        throw new PortAlreadyInUseException();
    }
}

From source file:net.sf.jabref.logic.remote.server.RemoteListenerServer.java

public RemoteListenerServer(MessageHandler messageHandler, int port) throws IOException {
    this.serverSocket = new ServerSocket(port, BACKLOG, InetAddress.getByName("localhost"));
    this.messageHandler = messageHandler;
}