Package java.net

Examples of java.net.DatagramPacket


                sock.close();
        }

        public void run() {
            byte[]         buf=new byte[128000];
            DatagramPacket p;

            while(t != null) {
                p=new DatagramPacket(buf, buf.length);
                try {
                    sock.receive(p);
                    if(receiver != null) {
                        IpAddress addr=new IpAddress(p.getAddress(), p.getPort());
                        receiver.receive(addr, p.getData());
                    }
                }
                catch(IOException e) {
                    if(sock == null)
                        t=null;
View Full Code Here


            }
        }

        void runSocketRead() {
            byte[] buf=new byte[2];
            DatagramPacket packet;

            try {
                sock=new DatagramSocket(12345, InetAddress.getLocalHost());
                // System.out.println("** mcast_sock=" + mcast_sock.getLocalAddress() + ":" + mcast_sock.getLocalPort());
                packet=new DatagramPacket(buf, buf.length);
                //System.out.println("** receive(): start");
                sock.receive(packet);
                //System.out.println("** receive(): done");
            }
            catch(Exception e) {
View Full Code Here

*/
public class UcastTest {

    public static void main(String args[]) { 
  DatagramSocket  sock;
  DatagramPacket  packet;
  byte[]          buf=new byte[0];
  boolean         receiver=true;
  String          val;
  InetAddress     receiver_addr=null, sender_addr=null;
  int             receiver_port=0, sender_port=0;
View Full Code Here

    }


    static void receiverLoop(DatagramSocket sock) {
  DatagramPacket packet;
  byte[]         buf;

  while(true) {
      buf=new byte[256];
      packet=new DatagramPacket(buf, buf.length);
      try {
    sock.receive(packet);
    System.out.println("Receive packet from " + packet.getAddress().getHostAddress() +
                           ':' + packet.getPort() + ": " + new String(packet.getData()));

    buf="ACK".getBytes();
    packet=new DatagramPacket(buf, buf.length, packet.getAddress(), packet.getPort());
    sock.send(packet);
      }
      catch(Exception e) {
    System.err.println(e);
    break;
View Full Code Here

   
    static void senderLoop(DatagramSocket sock) {
  DataInputStream in;
  String          line;
  DatagramPacket  packet;
  InetAddress     dst;
  int             dst_port;
  StringTokenizer tok;
  StringBuffer    sb;
  String          tmp, buf;
  byte[]          bbuf;
  AckReceiver     ack_receiver=new AckReceiver(sock);

  ack_receiver.start();
  in=new DataInputStream(System.in);
  while(true) {
      try {
    System.out.print("> ");
    line=in.readLine();
    if(line.startsWith("quit") || line.startsWith("exit"))
        break;
    tok=new StringTokenizer(line);
    try {
        dst=InetAddress.getByName(tok.nextToken());
        dst_port=Integer.parseInt(tok.nextToken());
    }
    catch(Exception e) {
        System.err.println(e);
        continue;
    }
    sb=new StringBuffer();
    while(tok.hasMoreTokens()) {
        tmp=tok.nextToken();
        sb.append(tmp + ' ');
    }
    buf=sb.toString();
    System.out.println("sending " + buf);
    bbuf=buf.getBytes();
    packet=new DatagramPacket(bbuf, bbuf.length, dst, dst_port);
    sock.send(packet);
      }
      catch(Exception e) {
    System.err.println(e);
    break;
View Full Code Here

  public void run() {
      while(t != null) {
    try {
        buf=new byte[256];
        packet=new DatagramPacket(buf, buf.length);
        sock.receive(packet);
        System.out.println("<< Received packet from " +
               packet.getAddress().getHostAddress() + ':' +
               packet.getPort() + ": " + new String(packet.getData()));
    }
View Full Code Here

        int upper;
        int lower=0;
        int highest_failed=-1;
        DatagramSocket sock;
        byte[] buf;
        DatagramPacket packet;
        InetAddress local_addr;


        try {
            sock=new DatagramSocket();
            local_addr=InetAddress.getLocalHost();
        }
        catch(Exception ex) {
            if(warn) log.warn("failed creating DatagramSocket: " + ex);
            return 0;
        }

        try {
            upper=max_send;
            for(int i=0; i < num_iterations && lower < upper; i++) { // iterations to approximate frag_size
                try {
                    buf=new byte[upper];
                    // System.out.println("** upper=" + upper + " (lower=" + lower + ")");
                    packet=new DatagramPacket(buf, buf.length, local_addr, 9);
                    sock.send(packet);
                    lower=Math.max(lower, upper);
                    upper=upper * 2;
                    if(highest_failed > -1)
                        upper=Math.min(highest_failed, upper);
View Full Code Here

                              Ganglia 3.1.x*/
    xdr_string("GROUP");    /*Group attribute*/
    xdr_string(groupName)/*Group value*/

    for (SocketAddress socketAddress : metricsServers) {
      DatagramPacket packet =
        new DatagramPacket(buffer, offset, socketAddress);
      datagramSocket.send(packet);
    }

    // Now we send out a message with the actual value.
    // Technically, we only need to send out the metadata message once for
    // each metric, but I don't want to have to record which metrics we did and
    // did not send.
    offset = 0;
    xdr_int(133);         // we are sending a string value
    xdr_string(hostName); // hostName
    xdr_string(name);     // metric name
    xdr_int(0);           // spoof = False
    xdr_string("%s");     // format field
    xdr_string(value);    // metric value
       
    for (SocketAddress socketAddress : metricsServers) {
      DatagramPacket packet =
        new DatagramPacket(buffer, offset, socketAddress);
      datagramSocket.send(packet);
    }
  }
View Full Code Here

    xdr_int(slope);
    xdr_int(tmax);
    xdr_int(dmax);
       
    for (SocketAddress socketAddress : metricsServers) {
      DatagramPacket packet =
        new DatagramPacket(buffer, offset, socketAddress);
      datagramSocket.send(packet);
    }
  }
View Full Code Here

     */
    public void send(Action action) {
        DuplicateDetector.getInstance().add(action.toJSON());
        String msg = action.toJSON().toString();

        DatagramPacket dp = new DatagramPacket(msg.getBytes(), msg.length(), this.multicastAddress, this.multicastPort);
        try {
            this.multicastSocket.send(dp);
        } catch (IOException e) {
            e.printStackTrace();
        }
View Full Code Here

TOP

Related Classes of java.net.DatagramPacket

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.