Search in sources :

Example 1 with Socket

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

the class MinaTcpLineDelimiterUsingPlainSocketTest 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 MAC newline at the end to flag end of textline to Camel-Mina
        os.write((input + "\r").getBytes());
        is = soc.getInputStream();
        int len = is.read(buf);
        if (len == -1) {
            // no data received
            return null;
        }
    } finally {
        IOHelper.close(is, os);
        soc.close();
    }
    // convert the buffer to chars
    StringBuilder sb = new StringBuilder();
    for (byte b : buf) {
        char ch = (char) b;
        if (ch == '\r' || ch == 0) {
            // use MAC delimiter 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 2 with Socket

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

the class Mina2TcpLineDelimiterUsingPlainSocketTest 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 MAC newline at the end to flag end of textline to camel-mina2
        os.write((input + "\r").getBytes());
        is = soc.getInputStream();
        int len = is.read(buf);
        if (len == -1) {
            // no data received
            return null;
        }
    } finally {
        IOHelper.close(is, os);
        soc.close();
    }
    // convert the buffer to chars
    StringBuilder sb = new StringBuilder();
    for (byte b : buf) {
        char ch = (char) b;
        if (ch == '\r' || ch == 0) {
            // use MAC delimiter 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 3 with Socket

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

the class MllpTcpClientProducer method checkConnection.

/**
     * Validate the TCP Connection
     *
     * @return null if the connection is valid, otherwise the Exception encountered checking the connection
     */
void checkConnection() throws IOException {
    if (null == socket || socket.isClosed() || !socket.isConnected()) {
        socket = new Socket();
        socket.setKeepAlive(endpoint.keepAlive);
        socket.setTcpNoDelay(endpoint.tcpNoDelay);
        if (null != endpoint.receiveBufferSize) {
            socket.setReceiveBufferSize(endpoint.receiveBufferSize);
        } else {
            endpoint.receiveBufferSize = socket.getReceiveBufferSize();
        }
        if (null != endpoint.sendBufferSize) {
            socket.setSendBufferSize(endpoint.sendBufferSize);
        } else {
            endpoint.sendBufferSize = socket.getSendBufferSize();
        }
        socket.setReuseAddress(endpoint.reuseAddress);
        socket.setSoLinger(false, -1);
        InetSocketAddress socketAddress;
        if (null == endpoint.getHostname()) {
            socketAddress = new InetSocketAddress(endpoint.getPort());
        } else {
            socketAddress = new InetSocketAddress(endpoint.getHostname(), endpoint.getPort());
        }
        log.debug("Connecting to socket on {}", socketAddress);
        socket.connect(socketAddress, endpoint.connectTimeout);
        log.debug("Creating MllpSocketReader and MllpSocketWriter");
        mllpSocketReader = new MllpSocketReader(socket, endpoint.receiveTimeout, endpoint.readTimeout, true);
        if (endpoint.bufferWrites) {
            mllpSocketWriter = new MllpBufferedSocketWriter(socket, false);
        } else {
            mllpSocketWriter = new MllpSocketWriter(socket, false);
        }
    }
}
Also used : MllpSocketReader(org.apache.camel.component.mllp.impl.MllpSocketReader) MllpBufferedSocketWriter(org.apache.camel.component.mllp.impl.MllpBufferedSocketWriter) InetSocketAddress(java.net.InetSocketAddress) MllpSocketWriter(org.apache.camel.component.mllp.impl.MllpSocketWriter) Socket(java.net.Socket)

Example 4 with Socket

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

the class MllpSocketUtilSocketTest method setUp.

@Before
public void setUp() throws Exception {
    serverSocket = new ServerSocket(0);
    socket = new Socket();
}
Also used : ServerSocket(java.net.ServerSocket) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) Before(org.junit.Before)

Example 5 with Socket

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

the class MllpClientResource method connect.

public void connect(int connectTimeout) {
    try {
        clientSocket = new Socket();
        clientSocket.connect(new InetSocketAddress(mllpHost, mllpPort), connectTimeout);
        clientSocket.setSoTimeout(soTimeout);
        clientSocket.setSoLinger(false, -1);
        clientSocket.setReuseAddress(reuseAddress);
        clientSocket.setTcpNoDelay(tcpNoDelay);
        inputStream = clientSocket.getInputStream();
        outputStream = new BufferedOutputStream(clientSocket.getOutputStream(), 2048);
    } catch (IOException e) {
        String errorMessage = String.format("Unable to establish connection to %s:%s", mllpHost, mllpPort);
        log.error(errorMessage, e);
        throw new MllpJUnitResourceException(errorMessage, e);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) Socket(java.net.Socket)

Aggregations

Socket (java.net.Socket)1553 IOException (java.io.IOException)664 ServerSocket (java.net.ServerSocket)548 OutputStream (java.io.OutputStream)364 InetSocketAddress (java.net.InetSocketAddress)350 Test (org.junit.Test)348 InputStream (java.io.InputStream)249 InputStreamReader (java.io.InputStreamReader)170 BufferedReader (java.io.BufferedReader)151 SocketException (java.net.SocketException)132 SSLSocket (javax.net.ssl.SSLSocket)103 SocketTimeoutException (java.net.SocketTimeoutException)97 UnknownHostException (java.net.UnknownHostException)84 ConnectException (java.net.ConnectException)82 ByteArrayOutputStream (java.io.ByteArrayOutputStream)75 InetAddress (java.net.InetAddress)73 OutputStreamWriter (java.io.OutputStreamWriter)70 ServletOutputStream (javax.servlet.ServletOutputStream)68 DataOutputStream (java.io.DataOutputStream)67 CountDownLatch (java.util.concurrent.CountDownLatch)64