Search in sources :

Example 91 with DatagramPacket

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();
    }
}
Also used : FutureTask(java.util.concurrent.FutureTask) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) Test(org.junit.Test)

Example 92 with DatagramPacket

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());
}
Also used : SyslogMessageLogDTO(org.opennms.netmgt.syslogd.api.SyslogMessageLogDTO) DatagramPacket(java.net.DatagramPacket) SyslogConnection(org.opennms.netmgt.syslogd.api.SyslogConnection) InetAddress(java.net.InetAddress) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 93 with DatagramPacket

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.");
}
Also used : MulticastSocket(java.net.MulticastSocket) SocketTimeoutException(java.net.SocketTimeoutException) DatagramPacket(java.net.DatagramPacket)

Example 94 with DatagramPacket

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.");
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket)

Example 95 with DatagramPacket

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();
}
Also used : DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) InetAddress(java.net.InetAddress)

Aggregations

DatagramPacket (java.net.DatagramPacket)334 DatagramSocket (java.net.DatagramSocket)138 IOException (java.io.IOException)137 InetAddress (java.net.InetAddress)77 SocketException (java.net.SocketException)48 Test (org.junit.Test)37 InetSocketAddress (java.net.InetSocketAddress)34 SocketTimeoutException (java.net.SocketTimeoutException)30 UnknownHostException (java.net.UnknownHostException)26 MulticastSocket (java.net.MulticastSocket)19 InterruptedIOException (java.io.InterruptedIOException)17 SocketAddress (java.net.SocketAddress)16 ByteBuffer (java.nio.ByteBuffer)11 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)11 SyslogConnection (org.opennms.netmgt.syslogd.api.SyslogConnection)11 ArrayList (java.util.ArrayList)10 PortUnreachableException (java.net.PortUnreachableException)9 SyslogMessageLogDTO (org.opennms.netmgt.syslogd.api.SyslogMessageLogDTO)8 BindException (java.net.BindException)7 Transactional (org.springframework.transaction.annotation.Transactional)7