Search in sources :

Example 81 with Socket

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

the class DNSConnection method open.

/**
 * NOTE: old connection should be closed. server must be != null. If
 * server is down or unreachable then NoRouteToHostException
 * (subclass of SocketException) is thrown. If connection is
 * remotely refused then ConnectException (subclass of
 * SocketException) is thrown. If SecurityException is caught then
 * SocketException is thrown. Must be synchronized outside.
 **
 * @since 2.2
 */
public void open(InetAddress server) throws NullPointerException, IOException {
    server.hashCode();
    try {
        Socket socket = new Socket(server, PORT);
        BufferedInputStream in = new BufferedInputStream(socket.getInputStream(), DNSMsgHeader.UDP_PACKET_LEN);
        this.out = socket.getOutputStream();
        this.in = in;
        this.socket = socket;
    } catch (SecurityException e) {
        throw new SocketException("SecurityException: connect(" + server.getHostAddress() + ")");
    }
    this.msgBytes = null;
}
Also used : SocketException(java.net.SocketException) BufferedInputStream(java.io.BufferedInputStream) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket)

Example 82 with Socket

use of java.net.Socket 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 83 with Socket

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

the class DecoratedSocketsSslSocketFactory method createSocket.

@Override
public Socket createSocket(final Socket s, final String host, final int port, final boolean autoClose) throws IOException {
    Socket socket = delegate.createSocket(s, host, port, autoClose);
    decorate((SSLSocket) socket);
    return socket;
}
Also used : Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket)

Example 84 with Socket

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

the class DecoratedSocketsSslSocketFactory method createSocket.

@Override
public Socket createSocket(final String host, final int port, final InetAddress localHost, final int localPort) throws IOException, UnknownHostException {
    Socket socket = delegate.createSocket(host, port, localHost, localPort);
    decorate((SSLSocket) socket);
    return socket;
}
Also used : Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket)

Example 85 with Socket

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

the class RelaxedX509TrustManager method createSocket.

/**
	 * Attempts to get a new socket connection to the given host within the
	 * given time limit.
	 * 
	 * @param host
	 *            the host name/IP
	 * @param port
	 *            the port on the host
	 * @param localAddress
	 *            the local host name/IP to bind the socket to
	 * @param localPort
	 *            the port on the local machine
	 * @param params
	 *            {@link HttpConnectionParams Http connection parameters}
	 * 
	 * @return Socket a new socket
	 * 
	 * @throws IOException
	 *             if an I/O error occurs while creating the socket
	 * @throws UnknownHostException
	 *             if the IP address of the host cannot be determined
	 * @throws 	ConnectTimeoutException        
	 */
@Override
public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort, final HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = params.getConnectionTimeout();
    if (timeout == 0) {
        InetAddress hostAddress = getCachedMisconfiguredHost(host, port);
        if (hostAddress != null) {
            return clientSSLSockFactory.createSocket(hostAddress, port, localAddress, localPort);
        }
        try {
            SSLSocket sslSocket = (SSLSocket) clientSSLSockFactory.createSocket(host, port, localAddress, localPort);
            sslSocket.startHandshake();
            return sslSocket;
        } catch (SSLException e) {
            if (!e.getMessage().contains(CONTENTS_UNRECOGNIZED_NAME_EXCEPTION)) {
                throw e;
            }
            hostAddress = InetAddress.getByName(host);
            cacheMisconfiguredHost(host, port, hostAddress);
            return clientSSLSockFactory.createSocket(hostAddress, port, localAddress, localPort);
        }
    }
    Socket socket = clientSSLSockFactory.createSocket();
    SocketAddress localAddr = new InetSocketAddress(localAddress, localPort);
    socket.bind(localAddr);
    SocketAddress remoteAddr = new InetSocketAddress(host, port);
    socket.connect(remoteAddr, timeout);
    return socket;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) SSLSocket(javax.net.ssl.SSLSocket) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) SSLException(javax.net.ssl.SSLException) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket)

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