Search in sources :

Example 56 with Socket

use of java.net.Socket in project camel by apache.

the class UnsharableCodecsConflictsTest method canSupplyMultipleCodecsToEndpointPipeline.

@Test
public void canSupplyMultipleCodecsToEndpointPipeline() throws Exception {
    byte[] sPort1 = new byte[8192];
    byte[] sPort2 = new byte[16383];
    Arrays.fill(sPort1, (byte) 0x38);
    Arrays.fill(sPort2, (byte) 0x39);
    byte[] bodyPort1 = (new String(LENGTH_HEADER) + new String(sPort1)).getBytes();
    byte[] bodyPort2 = (new String(LENGTH_HEADER) + new String(sPort2)).getBytes();
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived(new String(sPort2) + "9");
    Socket server1 = getSocket("localhost", port1);
    Socket server2 = getSocket("localhost", port2);
    try {
        sendSopBuffer(bodyPort2, server2);
        sendSopBuffer(bodyPort1, server1);
        sendSopBuffer(new String("9").getBytes(), server2);
    } catch (Exception e) {
        log.error("", e);
    } finally {
        server1.close();
        server2.close();
    }
    mock.assertIsSatisfied();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Socket(java.net.Socket) IOException(java.io.IOException) Test(org.junit.Test)

Example 57 with Socket

use of java.net.Socket in project camel by apache.

the class UnsharableCodecsConflictsTest method getSocket.

private static Socket getSocket(String host, int port) throws IOException {
    Socket s = new Socket(host, port);
    s.setSoTimeout(60000);
    return s;
}
Also used : Socket(java.net.Socket)

Example 58 with Socket

use of java.net.Socket in project camel by apache.

the class NettyProducerHangTest method acceptReplyAcceptClose.

private void acceptReplyAcceptClose() throws IOException {
    byte[] buf = new byte[128];
    ServerSocket serverSocket = new ServerSocket(PORT);
    Socket soc = serverSocket.accept();
    log.info("Open socket and accept data");
    try (InputStream is = soc.getInputStream();
        OutputStream os = soc.getOutputStream()) {
        // read first message
        is.read(buf);
        // reply to the first message
        os.write("response\n".getBytes());
        // read second message
        is.read(buf);
    // do not reply, just close socket (emulate network problem)
    } finally {
        soc.close();
        serverSocket.close();
    }
    log.info("Close socket");
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ServerSocket(java.net.ServerSocket) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket)

Example 59 with Socket

use of java.net.Socket in project camel by apache.

the class NettyTcpWithInOutUsingPlainSocketTest method sendAndReceive.

private String sendAndReceive(String input) throws IOException {
    byte[] buf = new byte[128];
    Socket soc = new Socket();
    soc.connect(new InetSocketAddress("localhost", getPort()));
    // Send message using plain Socket to test if this works
    OutputStream os = null;
    InputStream is = null;
    try {
        os = soc.getOutputStream();
        // must append the line delimiter
        os.write((input + "\n").getBytes());
        is = soc.getInputStream();
        int len = is.read(buf);
        if (len == -1) {
            // no data received
            return null;
        }
    } finally {
        if (is != null) {
            is.close();
        }
        if (os != null) {
            os.close();
        }
        soc.close();
    }
    // convert the buffer to chars
    StringBuilder sb = new StringBuilder();
    for (byte b : buf) {
        char ch = (char) b;
        if (ch == '\n' || ch == 0) {
            // newline denotes end of text (added in the end in the processor below)
            break;
        } else {
            sb.append(ch);
        }
    }
    return sb.toString();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Socket(java.net.Socket)

Example 60 with Socket

use of java.net.Socket in project camel by apache.

the class UnsharableCodecsConflictsTest method getSocket.

private static Socket getSocket(String host, int port) throws IOException {
    Socket s = new Socket(host, port);
    s.setSoTimeout(60000);
    return s;
}
Also used : 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