use of java.net.DatagramPacket in project opennms by OpenNMS.
the class NativeSocketTest method testServer.
@Test
public void testServer() throws Exception {
String[] cmds = new String[] { "echo", "echo2", "quit" };
DatagramSocket socket = null;
try {
socket = new DatagramSocket();
for (final String cmd : cmds) {
final DatagramSocket sock = socket;
final FutureTask<DatagramPacket> task = new FutureTask<DatagramPacket>(new Callable<DatagramPacket>() {
@Override
public DatagramPacket call() throws Exception {
printf("Sending cmd: %s\n", cmd);
final byte[] data = cmd.getBytes(StandardCharsets.UTF_8);
final DatagramPacket p = new DatagramPacket(data, data.length, InetAddress.getLocalHost(), sock.getLocalPort());
sock.send(p);
printf("Receiving...\n");
final DatagramPacket r = new DatagramPacket(new byte[128], 128);
sock.receive(r);
printf("Received\n");
return r;
}
});
m_executor.execute(task);
final DatagramPacket r = task.get(10, TimeUnit.SECONDS);
assertNotNull(r);
final String response = new String(r.getData(), r.getOffset(), r.getLength(), StandardCharsets.UTF_8);
printf("Received Response: %s from %s:%d\n", response, r.getAddress().getHostAddress(), r.getPort());
assertEquals(cmd, response);
}
} finally {
if (socket != null)
socket.close();
}
}
use of java.net.DatagramPacket in project opennms by OpenNMS.
the class SyslogdLoadIT method testNGSyslog.
@Test
@Transactional
public void testNGSyslog() throws Exception {
loadSyslogConfiguration("/etc/syslogd-syslogng-configuration.xml");
startSyslogdJavaNet();
m_eventCounter.anticipate();
InetAddress address = InetAddress.getLocalHost();
// handle an invalid packet
byte[] bytes = "<34>main: 2010-08-19 localhost foo0: load test 0 on tty1\0".getBytes();
DatagramPacket pkt = new DatagramPacket(bytes, bytes.length, address, SyslogClient.PORT);
SyslogMessageLogDTO messageLog = m_syslogSinkModule.toMessageLog(new SyslogConnection(pkt, false));
m_syslogSinkConsumer.handleMessage(messageLog);
// handle a valid packet
bytes = "<34>monkeysatemybrain!\0".getBytes();
pkt = new DatagramPacket(bytes, bytes.length, address, SyslogClient.PORT);
messageLog = m_syslogSinkModule.toMessageLog(new SyslogConnection(pkt, false));
m_syslogSinkConsumer.handleMessage(messageLog);
m_eventCounter.waitForFinish(120000);
assertEquals(1, m_eventCounter.getCount());
}
use of java.net.DatagramPacket in project jdk8u_jdk by JetBrains.
the class JdpTestCase method run.
public void run() throws Exception {
log.fine("Test started.");
log.fine("Listening for multicast packets at " + connection.address.getHostAddress() + ":" + String.valueOf(connection.port));
log.fine(initialLogMessage());
log.fine("Pause in between packets is: " + connection.pauseInSeconds + " seconds.");
startTime = System.currentTimeMillis();
timeOut = connection.pauseInSeconds * TIME_OUT_FACTOR;
log.fine("Timeout set to " + String.valueOf(timeOut) + " seconds.");
MulticastSocket socket = connection.connectWithTimeout(timeOut * 1000);
byte[] buffer = new byte[BUFFER_LENGTH];
DatagramPacket datagram = new DatagramPacket(buffer, buffer.length);
do {
try {
socket.receive(datagram);
onReceived(extractUDPpayload(datagram));
} catch (SocketTimeoutException e) {
onSocketTimeOut(e);
}
if (hasTestLivedLongEnough()) {
shutdown();
}
} while (shouldContinue());
log.fine("Test ended successfully.");
}
use of java.net.DatagramPacket in project jdk8u_jdk by JetBrains.
the class DatagramTimeout method main.
public static void main(String[] args) throws Exception {
boolean success = false;
DatagramSocket sock = new DatagramSocket();
try {
DatagramPacket p;
byte[] buffer = new byte[50];
p = new DatagramPacket(buffer, buffer.length);
sock.setSoTimeout(2);
sock.receive(p);
} catch (SocketTimeoutException e) {
success = true;
} finally {
sock.close();
}
if (!success)
throw new RuntimeException("Socket timeout failure.");
}
use of java.net.DatagramPacket in project jdk8u_jdk by JetBrains.
the class PortUnreachable method execute.
void execute() throws Exception {
// pick a port for the server
DatagramSocket sock2 = new DatagramSocket();
serverPort = sock2.getLocalPort();
// send a burst of packets to the unbound port - we should get back
// icmp port unreachable messages
//
InetAddress addr = InetAddress.getLocalHost();
byte[] b = "Hello me".getBytes();
DatagramPacket packet = new DatagramPacket(b, b.length, addr, serverPort);
//close just before sending
sock2.close();
for (int i = 0; i < 100; i++) clientSock.send(packet);
serverSend();
// try to receive
b = new byte[25];
packet = new DatagramPacket(b, b.length, addr, serverPort);
clientSock.setSoTimeout(10000);
clientSock.receive(packet);
System.out.println("client received data packet " + new String(packet.getData()));
// done
clientSock.close();
}
Aggregations