Search in sources :

Example 41 with SocketException

use of java.net.SocketException in project screenbird by adamhub.

the class MediaUtil method getMacAddress.

/**
     * Returns the MAC address for the current computer.
     * @return 
     */
public static String getMacAddress() {
    StringBuilder macAddress = new StringBuilder();
    try {
        InetAddress address = InetAddress.getLocalHost();
        NetworkInterface ni = NetworkInterface.getByInetAddress(address);
        byte[] mac = ni.getHardwareAddress();
        for (int i = 0; i < mac.length; i++) {
            macAddress.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "" : ""));
        }
    } catch (SocketException e) {
        log(e);
    } catch (UnknownHostException e) {
        log(e);
    }
    return macAddress.toString();
}
Also used : SocketException(java.net.SocketException) UnknownHostException(java.net.UnknownHostException) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 42 with SocketException

use of java.net.SocketException in project Openfire by igniterealtime.

the class LocalIPResolver method getLocalIP.

public static String getLocalIP() {
    if (overrideIp != null && overrideIp.length() >= 7) {
        return overrideIp;
    }
    Enumeration ifaces;
    try {
        ifaces = NetworkInterface.getNetworkInterfaces();
        while (ifaces.hasMoreElements()) {
            NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
            Enumeration iaddresses = iface.getInetAddresses();
            while (iaddresses.hasMoreElements()) {
                InetAddress iaddress = (InetAddress) iaddresses.nextElement();
                if (!iaddress.isLoopbackAddress() && !iaddress.isLinkLocalAddress() && !iaddress.isSiteLocalAddress()) {
                    return iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName();
                }
            }
        }
        ifaces = NetworkInterface.getNetworkInterfaces();
        while (ifaces.hasMoreElements()) {
            NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
            Enumeration iaddresses = iface.getInetAddresses();
            while (iaddresses.hasMoreElements()) {
                InetAddress iaddress = (InetAddress) iaddresses.nextElement();
                if (!iaddress.isLoopbackAddress() && !iaddress.isLinkLocalAddress()) {
                    return iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName();
                }
            }
        }
        return InetAddress.getLocalHost().getHostAddress() != null ? InetAddress.getLocalHost().getHostAddress() : InetAddress.getLocalHost().getHostName();
    } catch (SocketException e) {
        e.printStackTrace();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
    return "127.0.0.1";
}
Also used : SocketException(java.net.SocketException) Enumeration(java.util.Enumeration) UnknownHostException(java.net.UnknownHostException) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 43 with SocketException

use of java.net.SocketException in project Dempsy by Dempsy.

the class TcpReceiver method start.

@Override
public synchronized void start() throws MessageTransportException {
    if (isStarted())
        return;
    // check to see that the overflowHandler and the failFast setting are consistent.
    if (!failFast && overflowHandler != null)
        logger.warn("TcpReceiver/TcpTransport is configured with an OverflowHandler that will never be used because it's also configured to NOT 'fail fast' so it will always block waiting for messages to be processed.");
    // this sets the destination instance
    getDestination();
    // we need to call bind here in case the getDestination didn't do it
    //  (which it wont if the port is not ephemeral)
    bind();
    // in case this is a restart, we want to reset the stopMe value.
    stopMe.set(false);
    serverThread = new Thread(new Runnable() {

        @Override
        public void run() {
            while (!stopMe.get()) {
                try {
                    // Wait for an event one of the registered channels
                    Socket clientSocket = serverSocket.accept();
                    //  So we need to lock it.
                    synchronized (clientThreads) {
                        // unless we're done.
                        if (!stopMe.get()) {
                            // This should come from a thread pool
                            ClientThread clientThread = makeNewClientThread(clientSocket);
                            Thread thread = new Thread(clientThread, "Client Handler for " + getClientDescription(clientSocket));
                            thread.setDaemon(true);
                            thread.start();
                            clientThreads.add(clientThread);
                        }
                    }
                }// socket from another thread is the only way to make this happen.
                 catch (SocketException se) {
                    // however, if we didn't explicitly stop the server, then there's another problem
                    if (!stopMe.get())
                        logger.error("Socket error on the server managing " + destination, se);
                } catch (Throwable th) {
                    logger.error("Major error on the server managing " + destination, th);
                }
            }
            // we're leaving so signal
            synchronized (eventLock) {
                eventSignaled = true;
                eventLock.notifyAll();
            }
        }
    }, "Server for " + destination);
    if (executor == null) {
        DefaultDempsyExecutor defexecutor = new DefaultDempsyExecutor();
        defexecutor.setCoresFactor(1.0);
        defexecutor.setAdditionalThreads(1);
        defexecutor.setMaxNumberOfQueuedLimitedTasks(10000);
        executor = defexecutor;
        iStartedIt = true;
        executor.start();
    }
    setPendingGague();
    serverThread.start();
}
Also used : SocketException(java.net.SocketException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) DefaultDempsyExecutor(com.nokia.dempsy.executor.DefaultDempsyExecutor)

Example 44 with SocketException

use of java.net.SocketException in project CloudStack-archive by CloudStack-extras.

the class NetUtils method getAllLocalInetAddresses.

public static InetAddress[] getAllLocalInetAddresses() {
    List<InetAddress> addrList = new ArrayList<InetAddress>();
    try {
        for (NetworkInterface ifc : IteratorUtil.enumerationAsIterable(NetworkInterface.getNetworkInterfaces())) {
            if (ifc.isUp() && !ifc.isVirtual()) {
                for (InetAddress addr : IteratorUtil.enumerationAsIterable(ifc.getInetAddresses())) {
                    addrList.add(addr);
                }
            }
        }
    } catch (SocketException e) {
        s_logger.warn("SocketException in getAllLocalInetAddresses().", e);
    }
    InetAddress[] addrs = new InetAddress[addrList.size()];
    if (addrList.size() > 0) {
        System.arraycopy(addrList.toArray(), 0, addrs, 0, addrList.size());
    }
    return addrs;
}
Also used : SocketException(java.net.SocketException) ArrayList(java.util.ArrayList) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 45 with SocketException

use of java.net.SocketException in project CloudStack-archive by CloudStack-extras.

the class NetUtils method getLocalCidrs.

public static String[] getLocalCidrs() {
    String defaultHostIp = getDefaultHostIp();
    List<String> cidrList = new ArrayList<String>();
    try {
        for (NetworkInterface ifc : IteratorUtil.enumerationAsIterable(NetworkInterface.getNetworkInterfaces())) {
            if (ifc.isUp() && !ifc.isVirtual() && !ifc.isLoopback()) {
                for (InterfaceAddress address : ifc.getInterfaceAddresses()) {
                    InetAddress addr = address.getAddress();
                    int prefixLength = address.getNetworkPrefixLength();
                    if (prefixLength < 32 && prefixLength > 0) {
                        String ip = ipFromInetAddress(addr);
                        if (ip.equalsIgnoreCase(defaultHostIp))
                            cidrList.add(ipAndNetMaskToCidr(ip, getCidrNetmask(prefixLength)));
                    }
                }
            }
        }
    } catch (SocketException e) {
        s_logger.warn("UnknownHostException in getLocalCidrs().", e);
    }
    return cidrList.toArray(new String[0]);
}
Also used : SocketException(java.net.SocketException) InterfaceAddress(java.net.InterfaceAddress) ArrayList(java.util.ArrayList) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Aggregations

SocketException (java.net.SocketException)925 IOException (java.io.IOException)349 InetAddress (java.net.InetAddress)254 NetworkInterface (java.net.NetworkInterface)240 UnknownHostException (java.net.UnknownHostException)153 Socket (java.net.Socket)146 SocketTimeoutException (java.net.SocketTimeoutException)118 ServerSocket (java.net.ServerSocket)114 DatagramSocket (java.net.DatagramSocket)95 Test (org.junit.Test)87 InetSocketAddress (java.net.InetSocketAddress)84 ArrayList (java.util.ArrayList)70 Inet4Address (java.net.Inet4Address)51 DatagramPacket (java.net.DatagramPacket)49 ConnectException (java.net.ConnectException)48 InputStream (java.io.InputStream)41 InterruptedIOException (java.io.InterruptedIOException)41 BindException (java.net.BindException)36 EOFException (java.io.EOFException)33 OutputStream (java.io.OutputStream)32