Search in sources :

Example 71 with Socket

use of java.net.Socket in project bigbluebutton by bigbluebutton.

the class SocketScreenVideoSender method connect.

/* BBB-DS */
public void connect(String host, String room, int width, int height) throws ConnectionException {
    this.room = room;
    this.width = width;
    this.height = height;
    try {
        socket = new Socket(host, PORT);
        outStream = new DataOutputStream(socket.getOutputStream());
        sendHeaderEventAndRoom(CaptureEvents.CAPTURE_START.getEvent());
        sendScreenCaptureInfo(width, height);
        outStream.flush();
    } catch (UnknownHostException e) {
        e.printStackTrace();
        throw new ConnectionException("UnknownHostException: " + host);
    } catch (IOException e) {
        e.printStackTrace();
        throw new ConnectionException("IOException: " + host + ":" + PORT);
    }
    System.out.println("Starting capturedScreenSender ");
    sendCapturedScreen = true;
    capturedScreenSender = new Runnable() {

        public void run() {
            while (sendCapturedScreen) {
                try {
                    //System.out.println("ScreenQueue size " + screenQ.size());
                    ScreenVideo newScreen = screenQ.take();
                    try {
                        sendCapturedScreen(newScreen);
                    } catch (ConnectionException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } catch (InterruptedException e) {
                    System.out.println("InterruptedExeption while taking event.");
                }
            }
        }
    };
    exec.execute(capturedScreenSender);
}
Also used : UnknownHostException(java.net.UnknownHostException) DataOutputStream(java.io.DataOutputStream) IOException(java.io.IOException) Socket(java.net.Socket)

Example 72 with Socket

use of java.net.Socket in project musiccabinet by hakko.

the class JdbcDatabaseAdministrationDao method isRDBMSRunning.

/*
	 * Verify that a postgresql server is running.
	 * 
	 * Try sending what pgAdmin would send when connecting to a postgresql server,
	 * and verify that we get an 'R' back as first character from the database
	 * ('R' = authentication request).
	 * 
	 * Iff we get that, we decide that we have a postgresql server at hand.
	 * 
	 * TODO : it would be good to check version of postgresql server, but that isn't
	 * officially available until we have a user account. Checking it indirectly by
	 * looking at line numbers in error message (those change between postgresql
	 * releases) seems too ugly/error prone.
	 */
@Override
public boolean isRDBMSRunning() {
    boolean running = false;
    try {
        Socket socket = new Socket(host, port);
        PrintWriter pw = new PrintWriter(socket.getOutputStream());
        pw.print(new char[] { (char) 0, (char) 0, (char) 0, (char) 42, (char) 0, (char) 3, (char) 0, (char) 0, 'u', 's', 'e', 'r', (char) 0, 'p', 'o', 's', 't', 'g', 'r', 'e', 's', (char) 0, 'd', 'a', 't', 'a', 'b', 'a', 's', 'e', (char) 0, 't', 'e', 'm', 'p', 'l', 'a', 't', 'e', '1', (char) 0, (char) 0 });
        pw.flush();
        InputStreamReader isr = new InputStreamReader(socket.getInputStream());
        char response = (char) isr.read();
        running = true;
        if (response != 'R') {
            LOG.warn("Expected Postgresql server to return R, got " + response + ".");
        }
        socket.close();
    } catch (IOException e) {
        LOG.warn("Couldn't connect to Postgres service!", e);
    // expected if database is down, or we've connected to something that's not postgre
    }
    return running;
}
Also used : InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) Socket(java.net.Socket) PrintWriter(java.io.PrintWriter)

Example 73 with Socket

use of java.net.Socket in project hazelcast by hazelcast.

the class ClientConnectionManagerImpl method createSocketConnection.

protected ClientConnection createSocketConnection(final Address address) throws IOException {
    if (!alive) {
        throw new HazelcastException("ConnectionManager is not active!");
    }
    SocketChannel socketChannel = null;
    try {
        socketChannel = SocketChannel.open();
        Socket socket = socketChannel.socket();
        socket.setKeepAlive(socketOptions.isKeepAlive());
        socket.setTcpNoDelay(socketOptions.isTcpNoDelay());
        socket.setReuseAddress(socketOptions.isReuseAddress());
        if (socketOptions.getLingerSeconds() > 0) {
            socket.setSoLinger(true, socketOptions.getLingerSeconds());
        }
        int bufferSize = getBufferSize();
        socket.setSendBufferSize(bufferSize);
        socket.setReceiveBufferSize(bufferSize);
        InetSocketAddress inetSocketAddress = address.getInetSocketAddress();
        socketChannel.socket().connect(inetSocketAddress, connectionTimeout);
        SocketChannelWrapper socketChannelWrapper = socketChannelWrapperFactory.wrapSocketChannel(socketChannel, true);
        final ClientConnection clientConnection = new ClientConnection(client, ioThreadingModel, connectionIdGen.incrementAndGet(), socketChannelWrapper);
        socketChannel.configureBlocking(true);
        if (socketInterceptor != null) {
            socketInterceptor.onConnect(socket);
        }
        socketChannel.configureBlocking(ioThreadingModel.isBlocking());
        socket.setSoTimeout(0);
        clientConnection.start();
        return clientConnection;
    } catch (Exception e) {
        if (socketChannel != null) {
            socketChannel.close();
        }
        throw ExceptionUtil.rethrow(e, IOException.class);
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) HazelcastException(com.hazelcast.core.HazelcastException) SocketChannelWrapper(com.hazelcast.internal.networking.SocketChannelWrapper) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) Socket(java.net.Socket) TimeoutException(java.util.concurrent.TimeoutException) HazelcastException(com.hazelcast.core.HazelcastException) IOException(java.io.IOException) AuthenticationException(com.hazelcast.client.AuthenticationException)

Example 74 with Socket

use of java.net.Socket in project gradle by gradle.

the class Snarl method send.

private void send(InetAddress host, final String title, final String message) {
    Socket socket = null;
    try {
        try {
            socket = new Socket(host, 9887);
        } catch (ConnectException e) {
            // Snarl is not running
            throw new AnnouncerUnavailableException("Snarl is not running on host " + String.valueOf(host) + ".", e);
        }
        PrintWriter printWriter = null;
        try {
            final OutputStream outputStream = socket.getOutputStream();
            printWriter = new PrintWriter(outputStream, true);
            printWriter.println(formatMessage(title, message));
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        } finally {
            IOUtils.closeQuietly(printWriter);
        }
    } catch (IOException ioException) {
        throw new UncheckedIOException(ioException);
    } finally {
        IOUtils.closeQuietly(socket);
    }
}
Also used : OutputStream(java.io.OutputStream) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException) Socket(java.net.Socket) ConnectException(java.net.ConnectException) PrintWriter(java.io.PrintWriter)

Example 75 with Socket

use of java.net.Socket in project hazelcast by hazelcast.

the class TcpIpConnection method toString.

@Override
public String toString() {
    Socket socket = socketChannel.socket();
    SocketAddress localSocketAddress = socket != null ? socket.getLocalSocketAddress() : null;
    SocketAddress remoteSocketAddress = socket != null ? socket.getRemoteSocketAddress() : null;
    return "Connection[id=" + connectionId + ", " + localSocketAddress + "->" + remoteSocketAddress + ", endpoint=" + endPoint + ", alive=" + alive + ", type=" + type + "]";
}
Also used : SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket)

Aggregations

Socket (java.net.Socket)1633 IOException (java.io.IOException)705 ServerSocket (java.net.ServerSocket)578 OutputStream (java.io.OutputStream)377 InetSocketAddress (java.net.InetSocketAddress)367 Test (org.junit.Test)362 InputStream (java.io.InputStream)259 InputStreamReader (java.io.InputStreamReader)179 BufferedReader (java.io.BufferedReader)160 SocketException (java.net.SocketException)137 SSLSocket (javax.net.ssl.SSLSocket)111 SocketTimeoutException (java.net.SocketTimeoutException)97 UnknownHostException (java.net.UnknownHostException)86 ConnectException (java.net.ConnectException)84 InetAddress (java.net.InetAddress)78 ByteArrayOutputStream (java.io.ByteArrayOutputStream)76 OutputStreamWriter (java.io.OutputStreamWriter)70 DataOutputStream (java.io.DataOutputStream)68 ServletOutputStream (javax.servlet.ServletOutputStream)68 CountDownLatch (java.util.concurrent.CountDownLatch)65