Search in sources :

Example 11 with SocketException

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

the class RtpSocket method flushSocket.

public static void flushSocket(DatagramSocket socket) {
    /*
	 * Flush the socket
	 */
    int len = RtpPacket.getDataSize(RtpPacket.PCM_ENCODING, RtpPacket.MAX_SAMPLE_RATE, 2);
    len += RtpPacket.HEADER_SIZE;
    byte[] data = new byte[len];
    DatagramPacket packet = new DatagramPacket(data, len);
    int count = 0;
    try {
        socket.setSoTimeout(1);
        while (true) {
            try {
                socket.receive(packet);
                count++;
            } catch (SocketTimeoutException e) {
                break;
            } catch (IOException e) {
                Logger.println("Error flushing socket " + e.getMessage());
                break;
            }
        }
    } catch (SocketException e) {
        Logger.println("Can't flush receiver socket!");
    }
    if (count > 0) {
        if (Logger.logLevel >= Logger.LOG_MOREINFO) {
            Logger.println("Packets flushed:  " + count);
        }
    }
    try {
        socket.setSoTimeout(0);
    } catch (SocketException e) {
        Logger.println("Can't set socket timeout to 0!");
    }
}
Also used : SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) DatagramPacket(java.net.DatagramPacket) IOException(java.io.IOException)

Example 12 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 13 with SocketException

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

the class ConferenceReceiver method initLoneReceiverChannel.

private void initLoneReceiverChannel(int loneReceiverPort) {
    if (this.loneReceiverPort != loneReceiverPort && loneReceiverChannel != null) {
        close();
    }
    this.loneReceiverPort = loneReceiverPort;
    try {
        selector = Selector.open();
    } catch (IOException e) {
        Logger.println("Conference receiver failed to open selector " + e.getMessage());
        return;
    }
    if (loneReceiverPort == 0) {
        return;
    }
    Logger.println("Init lone channel using port " + loneReceiverPort);
    try {
        loneReceiverChannel = DatagramChannel.open();
        if (Logger.logLevel >= Logger.LOG_INFO) {
            Logger.println("Opened lone receiver channel " + loneReceiverChannel);
        }
    } catch (IOException e) {
        Logger.println("Conference receiver failed to open DatagramChannel " + " " + e.getMessage());
        return;
    }
    try {
        loneReceiverChannel.configureBlocking(false);
    } catch (IOException e) {
        Logger.println("Conference receiver failed to configureBlocking to false " + e.getMessage());
        return;
    }
    DatagramSocket socket = loneReceiverChannel.socket();
    try {
        socket.setReceiveBufferSize(RtpSocket.MAX_RECEIVE_BUFFER);
    } catch (SocketException e) {
        Logger.println("ConferenceReceiver failed to set receive buffer size " + e.getMessage());
        return;
    }
    try {
        socket.setSoTimeout(0);
    } catch (SocketException e) {
        Logger.println("ConferenceReceiver failed to set timeout " + e.getMessage());
        return;
    }
    InetSocketAddress bridgeAddress = Bridge.getLocalBridgeAddress();
    InetSocketAddress isa = new InetSocketAddress(bridgeAddress.getAddress(), loneReceiverPort);
    try {
        socket.bind(isa);
    } catch (IOException e) {
        Logger.println("Conference receiver unable to bind to " + loneReceiverPort + " " + e.getMessage());
        return;
    }
    try {
        SelectionKey selectionKey = loneReceiverChannel.register(selector, SelectionKey.OP_READ);
    } catch (Exception e) {
        Logger.println("Conference receiver unable to register:  " + e.getMessage());
        return;
    }
    memberCount++;
    Logger.println("Lone Channel uses port " + loneReceiverPort);
}
Also used : SocketException(java.net.SocketException) SelectionKey(java.nio.channels.SelectionKey) DatagramSocket(java.net.DatagramSocket) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) IOException(java.io.IOException) SocketException(java.net.SocketException)

Example 14 with SocketException

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

the class ConferenceManager method getConference.

public static ConferenceManager getConference(String conferenceId, String mediaPreference, String displayName, boolean permanent) {
    ConferenceManager conferenceManager;
    try {
        conferenceManager = findConferenceManager(conferenceId);
        if (Logger.logLevel >= Logger.LOG_INFO) {
            Logger.println("found existing conference:  '" + conferenceId + "'");
        }
        return conferenceManager;
    } catch (ParseException e) {
    }
    try {
        conferenceManager = new ConferenceManager(conferenceId, mediaPreference, displayName);
    } catch (SocketException e) {
        Logger.error("Can't create conference " + conferenceId + " " + e.getMessage());
        return null;
    }
    synchronized (conferenceList) {
        conferenceList.add(conferenceManager);
    }
    Logger.println("starting new conference:  '" + conferenceId + "'.  " + " conferences in progress:  " + conferenceList.size());
    conferenceManager.setPermanent(permanent);
    String id = conferenceManager.getId();
    if (displayName != null) {
        id += ":" + mediaPreference + ":" + displayName;
    }
    ConferenceManager.conferenceEventNotification(new ConferenceEvent(ConferenceEvent.CONFERENCE_STARTED, id));
    return conferenceManager;
}
Also used : SocketException(java.net.SocketException) ParseException(java.text.ParseException) ConferenceEvent(com.sun.voip.ConferenceEvent)

Example 15 with SocketException

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

the class ConferenceMember method initializeChannel.

private void initializeChannel() throws IOException {
    datagramChannel = conferenceManager.getConferenceReceiver().getChannel(cp);
    if (datagramChannel != null) {
        synchronized (datagramChannel) {
            if (loneRtcpReceiver == null) {
                int rtcpPort = datagramChannel.socket().getLocalPort() + 1;
                Logger.println("Starting lone RtcpReceiver on port " + rtcpPort);
                loneRtcpReceiver = new RtcpReceiver(new DatagramSocket(rtcpPort), true);
            }
            rtcpReceiver = loneRtcpReceiver;
        }
        return;
    }
    /*
	 * We are trying to find a pair of sockets with consecutive port nums.
	 * The first socket must have an even port.
	 *
	 * If we find a socket that we don't like, we have to keep it open
	 * otherwise when we try to find another socket, we may get the same
	 * one as before.
	 *
	 * So we make a list of the bad sockets and close them all
	 * after we're done.
	 */
    ArrayList badChannels = new ArrayList();
    int nextRtpPort = firstRtpPort;
    try {
        while (true) {
            datagramChannel = DatagramChannel.open();
            if (Logger.logLevel >= Logger.LOG_DETAIL) {
                Logger.println("Call " + cp + " Opened datagram channel " + datagramChannel);
            }
            datagramChannel.configureBlocking(false);
            DatagramSocket socket = datagramChannel.socket();
            socket.setReceiveBufferSize(RtpSocket.MAX_RECEIVE_BUFFER);
            socket.setSoTimeout(0);
            InetSocketAddress bridgeAddress = Bridge.getLocalBridgeAddress();
            InetSocketAddress isa = new InetSocketAddress(bridgeAddress.getAddress(), nextRtpPort);
            if (nextRtpPort > 0) {
                nextRtpPort += 2;
                if (lastRtpPort != 0 && (nextRtpPort + 1) > lastRtpPort) {
                    Logger.println("No more RTP ports available, last is " + lastRtpPort);
                    closeBadChannels(badChannels);
                    throw new IOException("No more RTP ports available, last is " + lastRtpPort);
                }
            }
            try {
                socket.bind(isa);
                int localPort = socket.getLocalPort();
                if ((localPort & 1) != 0) {
                    /*
			 * Port is odd, can't use this datagramSocket
			 */
                    if (Logger.logLevel >= Logger.LOG_INFO) {
                        Logger.println("Call " + cp + " skipping DatagramSocket with odd port " + localPort);
                    }
                    badChannels.add(datagramChannel);
                    continue;
                }
                Logger.writeFile("Call " + cp + " RTCP Port " + (localPort + 1));
                rtcpReceiver = new RtcpReceiver(new DatagramSocket(localPort + 1), false);
                break;
            } catch (SocketException e) {
                /*
		     * Couldn't bind, can't use this DatagramSocket.
		     */
                if (Logger.logLevel >= Logger.LOG_INFO) {
                    Logger.println("Call " + cp + " skipping DatagramSocket " + e.getMessage());
                }
                badChannels.add(datagramChannel);
                continue;
            }
        }
    } catch (Exception e) {
        closeBadChannels(badChannels);
        throw new IOException("Call " + cp + " MemberReceiver exception! " + e.getMessage());
    }
    closeBadChannels(badChannels);
    if (Logger.logLevel >= Logger.LOG_INFO) {
        Logger.println("Call " + cp + " port " + datagramChannel.socket().getLocalPort());
    }
}
Also used : SocketException(java.net.SocketException) DatagramSocket(java.net.DatagramSocket) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) RtcpReceiver(com.sun.voip.RtcpReceiver) IOException(java.io.IOException) IOException(java.io.IOException) SocketException(java.net.SocketException) ParseException(java.text.ParseException)

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