Search in sources :

Example 71 with SocketException

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

the class BridgedResolver method getLocalHost.

public static String getLocalHost() {
    Enumeration<NetworkInterface> ifaces = null;
    try {
        ifaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        LOGGER.log(Level.WARNING, "exception", e);
    }
    while (ifaces.hasMoreElements()) {
        NetworkInterface iface = ifaces.nextElement();
        Enumeration<InetAddress> iaddresses = iface.getInetAddresses();
        while (iaddresses.hasMoreElements()) {
            InetAddress iaddress = iaddresses.nextElement();
            if (!iaddress.isLoopbackAddress() && !iaddress.isLinkLocalAddress() && !iaddress.isSiteLocalAddress() && !(iaddress instanceof Inet6Address)) {
                return iaddress.getHostAddress();
            }
        }
    }
    try {
        return InetAddress.getLocalHost().getHostAddress();
    } catch (Exception e) {
        LOGGER.log(Level.WARNING, "exception", e);
    }
    return "127.0.0.1";
}
Also used : SocketException(java.net.SocketException) NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress) SmackException(org.jivesoftware.smack.SmackException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) SocketException(java.net.SocketException) XMPPException(org.jivesoftware.smack.XMPPException)

Example 72 with SocketException

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

the class ICEResolver method resolve.

/**
     * Resolve the IP and obtain a valid transport method.
     * @throws SmackException 
     * @throws InterruptedException 
     */
@Override
public synchronized void resolve(JingleSession session) throws XMPPException, SmackException, InterruptedException {
    this.setResolveInit();
    for (TransportCandidate candidate : this.getCandidatesList()) {
        if (candidate instanceof ICECandidate) {
            ICECandidate iceCandidate = (ICECandidate) candidate;
            iceCandidate.removeCandidateEcho();
        }
    }
    this.clear();
    // Create a transport candidate for each ICE negotiator candidate we have.
    ICENegociator iceNegociator = negociatorsMap.get(server);
    for (Candidate candidate : iceNegociator.getSortedCandidates()) try {
        Candidate.CandidateType type = candidate.getCandidateType();
        ICECandidate.Type iceType = ICECandidate.Type.local;
        if (type.equals(Candidate.CandidateType.ServerReflexive))
            iceType = ICECandidate.Type.srflx;
        else if (type.equals(Candidate.CandidateType.PeerReflexive))
            iceType = ICECandidate.Type.prflx;
        else if (type.equals(Candidate.CandidateType.Relayed))
            iceType = ICECandidate.Type.relay;
        else
            iceType = ICECandidate.Type.host;
        // JBW/GW - 17JUL08: Figure out the zero-based NIC number for this candidate.
        short nicNum = 0;
        try {
            Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
            short i = 0;
            NetworkInterface nic = NetworkInterface.getByInetAddress(candidate.getAddress().getInetAddress());
            while (nics.hasMoreElements()) {
                NetworkInterface checkNIC = nics.nextElement();
                if (checkNIC.equals(nic)) {
                    nicNum = i;
                    break;
                }
                i++;
            }
        } catch (SocketException e1) {
            LOGGER.log(Level.WARNING, "exeption", e1);
        }
        TransportCandidate transportCandidate = new ICECandidate(candidate.getAddress().getInetAddress().getHostAddress(), 1, nicNum, String.valueOf(Math.abs(random.nextLong())), candidate.getPort(), "1", candidate.getPriority(), iceType);
        transportCandidate.setLocalIp(candidate.getBase().getAddress().getInetAddress().getHostAddress());
        transportCandidate.setPort(getFreePort());
        try {
            transportCandidate.addCandidateEcho(session);
        } catch (SocketException e) {
            LOGGER.log(Level.WARNING, "exception", e);
        }
        this.addCandidate(transportCandidate);
        LOGGER.fine("Candidate addr: " + candidate.getAddress().getInetAddress() + "|" + candidate.getBase().getAddress().getInetAddress() + " Priority:" + candidate.getPriority());
    } catch (UtilityException e) {
        LOGGER.log(Level.WARNING, "exception", e);
    } catch (UnknownHostException e) {
        LOGGER.log(Level.WARNING, "exception", e);
    }
    if (RTPBridge.serviceAvailable(connection)) {
        //            try {
        String localIp;
        int network;
        // JBW/GW - 17JUL08: ICENegotiator.getPublicCandidate() always returned null in JSTUN 1.7.0, and now the API doesn't exist in JSTUN 1.7.1
        //                if (iceNegociator.getPublicCandidate() != null) {
        //                    localIp = iceNegociator.getPublicCandidate().getBase().getAddress().getInetAddress().getHostAddress();
        //                    network = iceNegociator.getPublicCandidate().getNetwork();
        //                }
        //                else {
        {
            localIp = BridgedResolver.getLocalHost();
            network = 0;
        }
        sid = Math.abs(random.nextLong());
        RTPBridge rtpBridge = RTPBridge.getRTPBridge(connection, String.valueOf(sid));
        TransportCandidate localCandidate = new ICECandidate(rtpBridge.getIp(), 1, network, String.valueOf(Math.abs(random.nextLong())), rtpBridge.getPortA(), "1", 0, ICECandidate.Type.relay);
        localCandidate.setLocalIp(localIp);
        TransportCandidate remoteCandidate = new ICECandidate(rtpBridge.getIp(), 1, network, String.valueOf(Math.abs(random.nextLong())), rtpBridge.getPortB(), "1", 0, ICECandidate.Type.relay);
        remoteCandidate.setLocalIp(localIp);
        localCandidate.setSymmetric(remoteCandidate);
        remoteCandidate.setSymmetric(localCandidate);
        localCandidate.setPassword(rtpBridge.getPass());
        remoteCandidate.setPassword(rtpBridge.getPass());
        localCandidate.setSessionId(rtpBridge.getSid());
        remoteCandidate.setSessionId(rtpBridge.getSid());
        localCandidate.setConnection(this.connection);
        remoteCandidate.setConnection(this.connection);
        addCandidate(localCandidate);
        //          if (iceNegociator.getPublicCandidate() == null) {
        if (true) {
            String publicIp = RTPBridge.getPublicIP(connection);
            if (publicIp != null && !publicIp.equals("")) {
                Enumeration<NetworkInterface> ifaces = null;
                try {
                    ifaces = NetworkInterface.getNetworkInterfaces();
                } catch (SocketException e) {
                    LOGGER.log(Level.WARNING, "exception", e);
                }
                // If detect this address in local machine, don't use it.
                boolean found = false;
                while (ifaces.hasMoreElements() && !false) {
                    NetworkInterface iface = ifaces.nextElement();
                    Enumeration<InetAddress> iaddresses = iface.getInetAddresses();
                    while (iaddresses.hasMoreElements()) {
                        InetAddress iaddress = iaddresses.nextElement();
                        if (iaddress.getHostAddress().indexOf(publicIp) > -1) {
                            found = true;
                            break;
                        }
                    }
                }
                if (!found) {
                    try {
                        TransportCandidate publicCandidate = new ICECandidate(publicIp, 1, 0, String.valueOf(Math.abs(random.nextLong())), getFreePort(), "1", 0, ICECandidate.Type.srflx);
                        publicCandidate.setLocalIp(InetAddress.getLocalHost().getHostAddress());
                        try {
                            publicCandidate.addCandidateEcho(session);
                        } catch (SocketException e) {
                            LOGGER.log(Level.WARNING, "exception", e);
                        }
                        addCandidate(publicCandidate);
                    } catch (UnknownHostException e) {
                        LOGGER.log(Level.WARNING, "exception", e);
                    }
                }
            }
        }
    }
    this.setResolveEnd();
}
Also used : Candidate(de.javawi.jstun.test.demo.ice.Candidate) SocketException(java.net.SocketException) Enumeration(java.util.Enumeration) UnknownHostException(java.net.UnknownHostException) NetworkInterface(java.net.NetworkInterface) UtilityException(de.javawi.jstun.util.UtilityException) ICENegociator(de.javawi.jstun.test.demo.ice.ICENegociator) InetAddress(java.net.InetAddress)

Example 73 with SocketException

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

the class RTPBridge method getPublicIP.

/**
     * Get Public Address from the Server.
     *
     * @param xmppConnection
     * @return public IP String or null if not found
     * @throws NotConnectedException 
     * @throws InterruptedException 
     */
@SuppressWarnings("deprecation")
public static String getPublicIP(XMPPConnection xmppConnection) throws NotConnectedException, InterruptedException {
    if (!xmppConnection.isConnected()) {
        return null;
    }
    RTPBridge rtpPacket = new RTPBridge(RTPBridge.BridgeAction.publicip);
    rtpPacket.setTo(RTPBridge.NAME + "." + xmppConnection.getXMPPServiceDomain());
    rtpPacket.setType(Type.set);
    // LOGGER.debug("Relayed to: " + candidate.getIp() + ":" + candidate.getPort());
    StanzaCollector collector = xmppConnection.createStanzaCollectorAndSend(rtpPacket);
    RTPBridge response = collector.nextResult();
    // Cancel the collector.
    collector.cancel();
    if (response == null)
        return null;
    if (response.getIp() == null || response.getIp().equals(""))
        return null;
    Enumeration<NetworkInterface> ifaces = null;
    try {
        ifaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        LOGGER.log(Level.WARNING, "exception", e);
    }
    while (ifaces != null && ifaces.hasMoreElements()) {
        NetworkInterface iface = ifaces.nextElement();
        Enumeration<InetAddress> iaddresses = iface.getInetAddresses();
        while (iaddresses.hasMoreElements()) {
            InetAddress iaddress = iaddresses.nextElement();
            if (!iaddress.isLoopbackAddress())
                if (iaddress.getHostAddress().indexOf(response.getIp()) >= 0)
                    return null;
        }
    }
    return response.getIp();
}
Also used : SocketException(java.net.SocketException) NetworkInterface(java.net.NetworkInterface) StanzaCollector(org.jivesoftware.smack.StanzaCollector) InetAddress(java.net.InetAddress)

Example 74 with SocketException

use of java.net.SocketException in project jmonkeyengine by jMonkeyEngine.

the class UdpEndpoint method send.

public void send(ByteBuffer data) {
    if (!isConnected()) {
        throw new KernelException("Endpoint is not connected:" + this);
    }
    try {
        DatagramPacket p = new DatagramPacket(data.array(), data.position(), data.remaining(), address);
        // Just queue it up for the kernel threads to write
        // out
        kernel.enqueueWrite(this, p);
    //socket.send(p);
    } catch (Exception e) {
        if (e instanceof SocketException) {
            throw new KernelException("Error sending datagram to:" + address, e);
        } else if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        } else {
            throw new RuntimeException(e);
        }
    }
}
Also used : SocketException(java.net.SocketException) DatagramPacket(java.net.DatagramPacket) KernelException(com.jme3.network.kernel.KernelException) SocketException(java.net.SocketException) KernelException(com.jme3.network.kernel.KernelException) IOException(java.io.IOException)

Example 75 with SocketException

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

the class BlockingReadingMode method run.

/**
     * A dedicated thread loop for reading the stream and sending incoming
     * packets to the appropriate router.
     */
@Override
public void run() {
    try {
        socketReader.reader.getXPPParser().setInput(new InputStreamReader(ServerTrafficCounter.wrapInputStream(socket.getInputStream()), CHARSET));
        // Read in the opening tag and prepare for packet stream
        try {
            socketReader.createSession();
        } catch (IOException e) {
            Log.debug("Error creating session", e);
            throw e;
        }
        // Read the packet stream until it ends
        if (socketReader.session != null) {
            readStream();
        }
    } catch (EOFException eof) {
    // Normal disconnect
    } catch (SocketException se) {
    // The socket was closed. The server may close the connection for several
    // reasons (e.g. user requested to remove his account). Do nothing here.
    } catch (AsynchronousCloseException ace) {
    // The socket was closed.
    } catch (XmlPullParserException ie) {
    // It is normal for clients to abruptly cut a connection
    // rather than closing the stream document. Since this is
    // normal behavior, we won't log it as an error.
    // Log.error(LocaleUtils.getLocalizedString("admin.disconnect"),ie);
    } catch (Exception e) {
        if (socketReader.session != null) {
            Log.warn(LocaleUtils.getLocalizedString("admin.error.stream") + ". Session: " + socketReader.session, e);
        }
    } finally {
        if (socketReader.session != null) {
            if (Log.isDebugEnabled()) {
                Log.debug("Logging off " + socketReader.session.getAddress() + " on " + socketReader.connection);
            }
            try {
                socketReader.session.close();
            } catch (Exception e) {
                Log.warn(LocaleUtils.getLocalizedString("admin.error.connection") + socket.toString());
            }
        } else {
            // Close and release the created connection
            socketReader.connection.close();
            Log.debug(LocaleUtils.getLocalizedString("admin.error.connection") + socket.toString());
        }
        socketReader.shutdown();
    }
}
Also used : SocketException(java.net.SocketException) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) InputStreamReader(java.io.InputStreamReader) EOFException(java.io.EOFException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) IOException(java.io.IOException) EOFException(java.io.EOFException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) SocketException(java.net.SocketException)

Aggregations

SocketException (java.net.SocketException)523 IOException (java.io.IOException)189 InetAddress (java.net.InetAddress)127 NetworkInterface (java.net.NetworkInterface)110 Socket (java.net.Socket)100 UnknownHostException (java.net.UnknownHostException)93 ServerSocket (java.net.ServerSocket)82 DatagramSocket (java.net.DatagramSocket)78 SocketTimeoutException (java.net.SocketTimeoutException)77 InetSocketAddress (java.net.InetSocketAddress)62 Test (org.junit.Test)50 ConnectException (java.net.ConnectException)39 DatagramPacket (java.net.DatagramPacket)38 ArrayList (java.util.ArrayList)35 BindException (java.net.BindException)31 Inet4Address (java.net.Inet4Address)24 SocketAddress (java.net.SocketAddress)24 InterruptedIOException (java.io.InterruptedIOException)23 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)23 SmallTest (android.test.suitebuilder.annotation.SmallTest)20