Search in sources :

Example 36 with ServerSocket

use of java.net.ServerSocket in project gitblit by gitblit.

the class FanoutSocketService method connect.

@Override
protected boolean connect() {
    if (serviceSocket == null) {
        try {
            serviceSocket = new ServerSocket();
            serviceSocket.setReuseAddress(true);
            serviceSocket.setSoTimeout(serviceTimeout);
            serviceSocket.bind(host == null ? new InetSocketAddress(port) : new InetSocketAddress(host, port));
            logger.info(MessageFormat.format("{0} is ready on {1}:{2,number,0}", name, host == null ? "0.0.0.0" : host, serviceSocket.getLocalPort()));
        } catch (IOException e) {
            logger.error(MessageFormat.format("failed to open {0} on {1}:{2,number,0}", name, host == null ? "0.0.0.0" : host, port), e);
            return false;
        }
    }
    return true;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException)

Example 37 with ServerSocket

use of java.net.ServerSocket in project fqrouter by fqrouter.

the class DNSConnection method openIncoming.

/**
 * NOTE: old connection should be closed. Wait for any incoming
 * connection and accept it. If listening is not active or if
 * SecurityException is caught then SocketException is thrown. If
 * waiting fails then InterruptedIOException is thrown. Must be
 * synchronized outside.
 **
 * @since 3.0
 */
public void openIncoming() throws IOException {
    ServerSocket curListener;
    if ((curListener = listener) != null) {
        try {
            Socket socket = curListener.accept();
            BufferedInputStream in = new BufferedInputStream(socket.getInputStream(), DNSMsgHeader.UDP_PACKET_LEN);
            this.out = socket.getOutputStream();
            this.in = in;
            this.msgBytes = null;
            this.socket = socket;
            return;
        } catch (SecurityException e) {
        }
    }
    throw new SocketException(curListener == null ? "Not listening" : "SecurityException: accept()");
}
Also used : SocketException(java.net.SocketException) BufferedInputStream(java.io.BufferedInputStream) ServerSocket(java.net.ServerSocket) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket)

Example 38 with ServerSocket

use of java.net.ServerSocket in project XobotOS by xamarin.

the class FtpURLConnection method connectInternal.

private void connectInternal() throws IOException {
    int port = url.getPort();
    int connectTimeout = getConnectTimeout();
    if (port <= 0) {
        port = FTP_PORT;
    }
    if (currentProxy == null || Proxy.Type.HTTP == currentProxy.type()) {
        controlSocket = new Socket();
    } else {
        controlSocket = new Socket(currentProxy);
    }
    InetSocketAddress addr = new InetSocketAddress(hostName, port);
    controlSocket.connect(addr, connectTimeout);
    connected = true;
    ctrlOutput = controlSocket.getOutputStream();
    ctrlInput = controlSocket.getInputStream();
    login();
    setType();
    if (!getDoInput()) {
        cd();
    }
    try {
        acceptSocket = new ServerSocket(0);
        dataPort = acceptSocket.getLocalPort();
        /* Cannot set REUSEADDR so we need to send a PORT command */
        port();
        if (connectTimeout == 0) {
            // set timeout rather than zero as before
            connectTimeout = 3000;
        }
        acceptSocket.setSoTimeout(getConnectTimeout());
        if (getDoInput()) {
            getFile();
        } else {
            sendFile();
        }
        dataSocket = acceptSocket.accept();
        dataSocket.setSoTimeout(getReadTimeout());
        acceptSocket.close();
    } catch (InterruptedIOException e) {
        throw new IOException("Could not establish data connection");
    }
    if (getDoInput()) {
        inputStream = new FtpURLInputStream(new BufferedInputStream(dataSocket.getInputStream()), controlSocket);
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) BufferedInputStream(java.io.BufferedInputStream) InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 39 with ServerSocket

use of java.net.ServerSocket in project zaproxy by zaproxy.

the class ProxyServerSSL method createServerSocket.

@Override
protected ServerSocket createServerSocket(String ip, int port) throws UnknownHostException, IOException {
    // ZAP: added the possibility to bound to all interfaces (using null as InetAddress)
    //      when the ip is null or an empty string        
    InetAddress addr = null;
    if ((ip != null) && !ip.isEmpty()) {
        addr = InetAddress.getByName(ip);
    }
    //ServerSocket socket = ssl.listen(port, 300, InetAddress.getByName(getProxyParam().getProxyIp()));
    ServerSocket socket = ssl.listen(port, 300, addr);
    return socket;
}
Also used : ServerSocket(java.net.ServerSocket) InetAddress(java.net.InetAddress)

Example 40 with ServerSocket

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

the class ManagementCenterServiceIntegrationTest method availablePort.

private int availablePort() throws IOException {
    while (true) {
        int port = (int) (65536 * Math.random());
        try {
            ServerSocket socket = new ServerSocket(port);
            socket.close();
            return port;
        } catch (Exception ignore) {
        // try next port
        }
    }
}
Also used : ServerSocket(java.net.ServerSocket) IOException(java.io.IOException)

Aggregations

ServerSocket (java.net.ServerSocket)738 IOException (java.io.IOException)337 Socket (java.net.Socket)266 InetSocketAddress (java.net.InetSocketAddress)131 Test (org.junit.Test)118 SocketException (java.net.SocketException)56 InputStream (java.io.InputStream)51 SocketTimeoutException (java.net.SocketTimeoutException)43 OutputStream (java.io.OutputStream)41 InetAddress (java.net.InetAddress)41 BindException (java.net.BindException)28 URL (java.net.URL)28 SSLServerSocket (javax.net.ssl.SSLServerSocket)26 File (java.io.File)24 InputStreamReader (java.io.InputStreamReader)24 UnknownHostException (java.net.UnknownHostException)24 BufferedReader (java.io.BufferedReader)21 SSLSocket (javax.net.ssl.SSLSocket)21 DatagramSocket (java.net.DatagramSocket)20 ServerSocketChannel (java.nio.channels.ServerSocketChannel)16