Package java.net

Examples of java.net.DatagramSocket


        InetSocketAddress serverAddy = new InetSocketAddress("127.0.0.1", port);
        // connect some clients
        for (int i = 0; i < CLIENT_COUNT; i++) {
            try {
                clients[i] = new DatagramSocket();
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("Creation client " + i + " failed");
            }
        }
View Full Code Here


        InetSocketAddress serverAddy = new InetSocketAddress("127.0.0.1", port);
        // connect some clients
        for (int i = 0; i < CLIENT_COUNT; i++) {
            try {
                clients[i] = new DatagramSocket();
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("Creation client " + i + " failed");
            }
        }
View Full Code Here

        NioUdpClient client = new NioUdpClient();
        client.getSessionConfig().setIdleTimeInMillis(IdleStatus.READ_IDLE, 2000);

        client.setFilters(new MyCodec(), new Handler());

        DatagramSocket serverSocket = new DatagramSocket();
        int port = serverSocket.getLocalPort();

        // warm up
        Thread.sleep(100);
        final long t0 = System.currentTimeMillis();

        // now connect the clients

        List<IoFuture<IoSession>> cf = new ArrayList<IoFuture<IoSession>>();
        for (int i = 0; i < CLIENT_COUNT; i++) {
            cf.add(client.connect(new InetSocketAddress("localhost", port)));
        }

        // does the session open message was fired ?
        assertTrue(openLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));

        // gather sessions from futures
        IoSession[] sessions = new IoSession[CLIENT_COUNT];
        for (int i = 0; i < CLIENT_COUNT; i++) {
            sessions[i] = cf.get(i).get();
            assertNotNull(sessions[i]);
        }

        // receive and send back some message
        for (int i = 0; i < CLIENT_COUNT; i++) {
            byte[] receiveData = new byte[1024];
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            serverSocket.receive(receivePacket);

            String sentence = new String(receivePacket.getData());
            LOG.info("RECEIVED  :" + sentence);

            InetAddress IPAddress = receivePacket.getAddress();
            int clientPort = receivePacket.getPort();
            DatagramPacket sendPacket = new DatagramPacket("tata".getBytes(), "tata".getBytes().length, IPAddress,
                    clientPort);
            serverSocket.send(sendPacket);
        }

        // does response was wrote and sent ?
        assertTrue(msgSentLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));

        // test is message was received by the client
        assertTrue(msgReadLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));

        // the session idled
        assertEquals(CLIENT_COUNT, idleLatch.getCount());

        // close the session
        assertEquals(CLIENT_COUNT, closedLatch.getCount());
        serverSocket.close();

        // does the session close event was fired ?
        assertTrue(closedLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));

        long t1 = System.currentTimeMillis();
View Full Code Here

     * {@inheritDoc}
     */
    public void start(int port, final CountDownLatch counter, final byte[] data) throws IOException {
        InetAddress serverAddress = InetAddress.getLocalHost();
        byte[] buffer = new byte[65507];
        sender = new DatagramSocket(port + 1);

        DatagramPacket pduSent = new DatagramPacket(data, data.length, serverAddress, port);
        DatagramPacket pduReceived = new DatagramPacket(buffer, data.length);
        sender.send(pduSent);

View Full Code Here

    private static String[] log(final boolean header,
                                final String msg,
                                final Exception ex,
                                final int packets) throws Exception {
        DatagramSocket ds = new DatagramSocket();
        ds.setSoTimeout(2000);

      SyslogAppender appender = new SyslogAppender();
      appender.setSyslogHost("localhost:" + ds.getLocalPort());
      appender.setName("name");
      appender.setHeader(header);
      PatternLayout pl = new PatternLayout("%m");
      appender.setLayout(pl);
      appender.activateOptions();

      Logger l = Logger.getRootLogger();
      l.addAppender(appender);
      if (ex == null) {
        l.info(msg);
      } else {
        l.error(msg, ex);
      }
      appender.close();
      String[] retval = new String[packets];
      byte[] buf = new byte[1000];
      for(int i = 0; i < packets; i++) {
          DatagramPacket p = new DatagramPacket(buf, 0, buf.length);
          ds.receive(p);
          retval[i] = new String(p.getData(), 0, p.getLength());
      }
      ds.close();
      return retval;
    }
View Full Code Here

    /**
     * Tests that any header or footer in layout is sent.
     * @throws Exception if exception during test.
     */
    public void testLayoutHeader() throws Exception {
        DatagramSocket ds = new DatagramSocket();
        ds.setSoTimeout(2000);

      SyslogAppender appender = new SyslogAppender();
      appender.setSyslogHost("localhost:" + ds.getLocalPort());
      appender.setName("name");
      appender.setHeader(false);
      HTMLLayout pl = new HTMLLayout();
      appender.setLayout(pl);
      appender.activateOptions();

      Logger l = Logger.getRootLogger();
      l.addAppender(appender);
      l.info("Hello, World");
      appender.close();
      String[] s = new String[3];
      byte[] buf = new byte[1000];
      for(int i = 0; i < 3; i++) {
          DatagramPacket p = new DatagramPacket(buf, 0, buf.length);
          ds.receive(p);
          s[i] = new String(p.getData(), 0, p.getLength());
      }
      ds.close();
      assertEquals("<14><!DOCTYPE", s[0].substring(0,13));
      assertEquals("<14></table>", s[2].substring(0,12));
    }
View Full Code Here

     * Tests that syslog packets do not exceed 1024 bytes.
     * See bug 42087.
     * @throws Exception if exception during test.
     */
    public void testBigPackets() throws Exception {
        DatagramSocket ds = new DatagramSocket();
        ds.setSoTimeout(2000);

      SyslogAppender appender = new SyslogAppender();
      appender.setSyslogHost("localhost:" + ds.getLocalPort());
      appender.setName("name");
      appender.setHeader(false);
      PatternLayout pl = new PatternLayout("%m");
      appender.setLayout(pl);
      appender.activateOptions();

      Logger l = Logger.getRootLogger();
      l.addAppender(appender);
      StringBuffer msgbuf = new StringBuffer();
      while(msgbuf.length() < 8000) {
          msgbuf.append("0123456789");
      }
      String msg = msgbuf.toString();
      l.info(msg);
      appender.close();
      String[] s = new String[8];
      byte[] buf = new byte[1200];
      for(int i = 0; i < 8; i++) {
          DatagramPacket p = new DatagramPacket(buf, 0, buf.length);
          ds.receive(p);
          assertTrue(p.getLength() <= 1024);
          s[i] = new String(p.getData(), 0, p.getLength());
      }
      ds.close();
      StringBuffer rcvbuf = new StringBuffer(s[0]);
      rcvbuf.delete(0, 4);
      for(int i = 1; i < 8; i++) {
          rcvbuf.setLength(rcvbuf.length() - 3);
          rcvbuf.append(s[i].substring(s[i].indexOf("...") + 3));
View Full Code Here

  private void runKeepFieldsTest(String keepFields) throws IOException {
    init(keepFields);
    source.start();
    // Write some message to the syslog port
    DatagramSocket syslogSocket;
    DatagramPacket datagramPacket;
    datagramPacket = new DatagramPacket(bodyWithTandH.getBytes(),
      bodyWithTandH.getBytes().length,
      InetAddress.getLocalHost(), source.getSourcePort());
    for (int i = 0; i < 10 ; i++) {
      syslogSocket = new DatagramSocket();
      syslogSocket.send(datagramPacket);
      syslogSocket.close();
    }

    List<Event> channelEvents = new ArrayList<Event>();
    Transaction txn = channel.getTransaction();
    txn.begin();
View Full Code Here

    source.start();
    // Write some message to the syslog port

    byte[] largePayload = getPayload(1000).getBytes();

    DatagramSocket syslogSocket;
    DatagramPacket datagramPacket;
    datagramPacket = new DatagramPacket(largePayload,
            1000,
            InetAddress.getLocalHost(), source.getSourcePort());
    for (int i = 0; i < 10 ; i++) {
      syslogSocket = new DatagramSocket();
      syslogSocket.send(datagramPacket);
      syslogSocket.close();
    }

    List<Event> channelEvents = new ArrayList<Event>();
    Transaction txn = channel.getTransaction();
    txn.begin();
View Full Code Here

        }
    }

    private void sendPacket(DatagramPacket datagramPacket) {

        DatagramSocket datagramSocket = getDatagramSocket();

        try {

            datagramSocket.send(datagramPacket);

        } catch (IOException ex) {

            throw new RuntimeException(ex);

        } finally {

            datagramSocket.close();
        }
    }
View Full Code Here

TOP

Related Classes of java.net.DatagramSocket

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.