Search in sources :

Example 51 with Socket

use of java.net.Socket in project CoreNLP by stanfordnlp.

the class LexicalizedParserClient method getLemmas.

/**
   * Get the lemmas for the text according to the parser's lemmatizer
   * (only applies to English), return it as whitespace tokenized text.
   */
public String getLemmas(String query) throws IOException {
    Socket socket = new Socket(host, port);
    Writer out = new OutputStreamWriter(socket.getOutputStream(), "utf-8");
    out.write("lemma " + query + "\n");
    out.flush();
    String result = readResult(socket);
    socket.close();
    return result;
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) Socket(java.net.Socket) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 52 with Socket

use of java.net.Socket in project CoreNLP by stanfordnlp.

the class LexicalizedParserClient method getParse.

/**
   * Returns the String output of the parse of the given query.
   * <br>
   * The "parse" method in the server is mostly useful for clients
   * using a language other than Java who don't want to import or wrap
   * Tree in any way.  However, it is useful to provide getParse to
   * test that functionality in the server.
   */
public String getParse(String query, boolean binarized) throws IOException {
    Socket socket = new Socket(host, port);
    Writer out = new OutputStreamWriter(socket.getOutputStream(), "utf-8");
    out.write("parse" + (binarized ? ":binarized " : " ") + query + "\n");
    out.flush();
    String result = readResult(socket);
    socket.close();
    return result;
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) Socket(java.net.Socket) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 53 with Socket

use of java.net.Socket in project okhttp by square.

the class Http2Server method run.

private void run() throws Exception {
    ServerSocket serverSocket = new ServerSocket(8888);
    serverSocket.setReuseAddress(true);
    while (true) {
        Socket socket = null;
        try {
            socket = serverSocket.accept();
            SSLSocket sslSocket = doSsl(socket);
            String protocolString = Platform.get().getSelectedProtocol(sslSocket);
            Protocol protocol = protocolString != null ? Protocol.get(protocolString) : null;
            if (protocol != Protocol.HTTP_2) {
                throw new ProtocolException("Protocol " + protocol + " unsupported");
            }
            Http2Connection connection = new Http2Connection.Builder(false).socket(sslSocket).listener(this).build();
            connection.start();
        } catch (IOException e) {
            logger.log(Level.INFO, "Http2Server connection failure: " + e);
            Util.closeQuietly(socket);
        } catch (Exception e) {
            logger.log(Level.WARNING, "Http2Server unexpected failure", e);
            Util.closeQuietly(socket);
        }
    }
}
Also used : ProtocolException(java.net.ProtocolException) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) Protocol(okhttp3.Protocol) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) ProtocolException(java.net.ProtocolException)

Example 54 with Socket

use of java.net.Socket in project okhttp by square.

the class DisconnectTest method setUp.

@Before
public void setUp() throws Exception {
    // Sockets on some platforms can have large buffers that mean writes do not block when
    // required. These socket factories explicitly set the buffer sizes on sockets created.
    server = new MockWebServer();
    server.setServerSocketFactory(new DelegatingServerSocketFactory(ServerSocketFactory.getDefault()) {

        @Override
        protected ServerSocket configureServerSocket(ServerSocket serverSocket) throws IOException {
            serverSocket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
            return serverSocket;
        }
    });
    client = defaultClient().newBuilder().socketFactory(new DelegatingSocketFactory(SocketFactory.getDefault()) {

        @Override
        protected Socket configureSocket(Socket socket) throws IOException {
            socket.setSendBufferSize(SOCKET_BUFFER_SIZE);
            socket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
            return socket;
        }
    }).build();
}
Also used : DelegatingSocketFactory(okhttp3.DelegatingSocketFactory) DelegatingServerSocketFactory(okhttp3.DelegatingServerSocketFactory) MockWebServer(okhttp3.mockwebserver.MockWebServer) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Before(org.junit.Before)

Example 55 with Socket

use of java.net.Socket in project okhttp by square.

the class URLConnectionTest method writeTimeouts.

/** Confirm that an unacknowledged write times out. */
@Test
public void writeTimeouts() throws IOException {
    MockWebServer server = new MockWebServer();
    // Sockets on some platforms can have large buffers that mean writes do not block when
    // required. These socket factories explicitly set the buffer sizes on sockets created.
    final int SOCKET_BUFFER_SIZE = 4 * 1024;
    server.setServerSocketFactory(new DelegatingServerSocketFactory(ServerSocketFactory.getDefault()) {

        @Override
        protected ServerSocket configureServerSocket(ServerSocket serverSocket) throws IOException {
            serverSocket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
            return serverSocket;
        }
    });
    urlFactory.setClient(urlFactory.client().newBuilder().socketFactory(new DelegatingSocketFactory(SocketFactory.getDefault()) {

        @Override
        protected Socket configureSocket(Socket socket) throws IOException {
            socket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
            socket.setSendBufferSize(SOCKET_BUFFER_SIZE);
            return socket;
        }
    }).writeTimeout(500, TimeUnit.MILLISECONDS).build());
    server.start();
    server.enqueue(new MockResponse().throttleBody(1, 1, // Prevent the server from reading!
    TimeUnit.SECONDS));
    connection = urlFactory.open(server.url("/").url());
    connection.setDoOutput(true);
    connection.setChunkedStreamingMode(0);
    OutputStream out = connection.getOutputStream();
    try {
        // 2 MiB.
        byte[] data = new byte[2 * 1024 * 1024];
        out.write(data);
        fail();
    } catch (SocketTimeoutException expected) {
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SocketTimeoutException(java.net.SocketTimeoutException) OutputStream(java.io.OutputStream) MockWebServer(okhttp3.mockwebserver.MockWebServer) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) Test(org.junit.Test)

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