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();
}
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();
}
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);
}
}
}
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();
}
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);
}
}
Aggregations