Package java.net

Examples of java.net.DatagramSocket


        this.host=host;
        this.port=port;


        if(server) {
            sock=new DatagramSocket(port, host);
            System.out.println("server started (ctrl-c to kill)");
            while(true) {
                byte[] buf=new byte[65000];
                DatagramPacket packet;
                packet=new DatagramPacket(buf, 0, buf.length);
                sock.receive(packet);
                packet=new DatagramPacket(RSP_BUF, 0, RSP_BUF.length, packet.getAddress(), packet.getPort());
                sock.send(packet); // send the response
            }
        }
        else {
            sock=new DatagramSocket();
            System.out.println("sending " + num + " requests");
            sendRequests();
        }
        sock.close();
    }
View Full Code Here


            bind_addr=InetAddress.getByName(bind_addr_str);
        }
        else
            bind_addr=InetAddress.getLocalHost();

        ucast_sock=new DatagramSocket(0, bind_addr);
        ucast_sock.setReceiveBufferSize(max_receiver_buffer_size);
        ucast_sock.setSendBufferSize(max_send_buffer_size);
        mcast_sock=new MulticastSocket(mcast_port);
        mcast_sock.setReceiveBufferSize(max_receiver_buffer_size);
        mcast_sock.setSendBufferSize(max_send_buffer_size);
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");
View Full Code Here

   @version $Revision: 1.5 $
*/
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;

 
 
  for(int i=0; i < args.length; i++) {
      val=args[i];
      if("-help".equals(val)) {
    help();
    return;
      }
      if("-sender".equals(val)) {
    receiver=false;
    continue;
      }
      if("-sender_addr".equals(val)) {
    try {
        sender_addr=InetAddress.getByName(args[++i]);
        sender_port=Integer.parseInt(args[++i]);
        continue;
    }
    catch(Exception e) {
        System.err.println(e);
        help();
        return;
    }
      }
      if("-receiver".equals(val)) {
    receiver=true;
    try {
        receiver_addr=InetAddress.getByName(args[++i]);
        receiver_port=Integer.parseInt(args[++i]);
    }
    catch(Exception e) {
        System.err.println(e);
        help();
        return;
    }
    continue;
      }
      help();
  }


  try {
      if(receiver) {
    sock=new DatagramSocket(receiver_port, receiver_addr);
    System.out.println("Socket=" + sock.getLocalAddress() + ':' + sock.getLocalPort());
    System.out.println("starting as receiver");
    receiverLoop(sock);
      }
      else {
    if(sender_addr != null && sender_port > 0)
        sock=new DatagramSocket(sender_port, sender_addr);
    else
        sock=new DatagramSocket();
    System.out.println("Socket=" + sock.getLocalAddress() + ':' + sock.getLocalPort());
    System.out.println("starting as sender");
    senderLoop(sock);
      }
  }
  catch(Exception e) {
View Full Code Here

    public int senseMaxFragSize() {
        int max_send=32000;
        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);
                }
                catch(IOException io_ex) {
                    if(highest_failed > -1)
                        highest_failed=Math.min(highest_failed, upper); // never exceed max_upper
                    else
                        highest_failed=upper;
                    upper=(upper + lower) / 2;
                }
                catch(Throwable ex) {
                    if(warn) log.warn("exception=" + ex);
                    break;
                }
            }

            /** Reduce the frag_size a bit to prevent packets that are too large (see bug #854887) */
            lower-=frag_overhead;
            if(log.isDebugEnabled()) log.debug("frag_size=" + lower);
            return lower;
        }
        finally {
            if(sock != null)
                sock.close();
        }
    }
View Full Code Here

        }
    }


    void senseMaxSendBufferSize(HashMap map) {
        DatagramSocket sock;
        int max_size=4096, retval=max_size;

        if(map != null && map.containsKey("frag_size)"))
            max_size=((Integer)map.get("frag_size")).intValue();

        try {
            sock=new DatagramSocket();
            while(max_size < 1000000) {
                sock.setSendBufferSize(max_size);
                if((retval=sock.getSendBufferSize()) < max_size)
                    return;
                max_size*=2;
            }
        }
        catch(Throwable ex) {
View Full Code Here

    }



    void senseMaxReceiveBufferSize(HashMap map) {
        DatagramSocket sock;
        int max_size=4096, retval=max_size;

        try {
            sock=new DatagramSocket();
            while(max_size < 1000000) {
                sock.setReceiveBufferSize(max_size);
                if((retval=sock.getReceiveBufferSize()) < max_size)
                    return;
                max_size*=2;
            }
        }
        catch(Throwable ex) {
View Full Code Here

     * @author The Apache Directory Project (mina-dev@directory.apache.org)
     */
    static boolean isPortAvailable(int port) {

        ServerSocket ss = null;
        DatagramSocket ds = null;
        try {
            ss = new ServerSocket(port);
            ss.setReuseAddress(true);
            ds = new DatagramSocket(port);
            ds.setReuseAddress(true);
            return true;
        } catch (IOException e) {
            // do nothing
        } finally {
            if (ds != null) {
                ds.close();
            }
            if (ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {
View Full Code Here

    slopeTable = getAttributeTable(SLOPE_PROPERTY);
    tmaxTable  = getAttributeTable(TMAX_PROPERTY);
    dmaxTable  = getAttributeTable(DMAX_PROPERTY);
       
    try {
      datagramSocket = new DatagramSocket();
    }
    catch (SocketException se) {
      se.printStackTrace();
    }
  }
View Full Code Here

    boolean shouldLog = Logger.shouldLog(LogLevel.DEBUG, this);
    if (shouldLog)
      Logger.debug(
        this,
        "Running old style detection code");
    DatagramSocket ds = null;
    try {
      try {
        ds = new DatagramSocket();
      } catch (SocketException e) {
        Logger.error(this, "SocketException", e);
        return null;
      }

      // This does not transfer any data
      // The ip is a.root-servers.net, 53 is DNS
      try {
        ds.connect(InetAddress.getByName("198.41.0.4"), 53);
      } catch (UnknownHostException ex) {
        Logger.error(this, "UnknownHostException", ex);
        return null;
      }
      return ds.getLocalAddress();
    } finally {
      if (ds != null) {
        ds.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.