Package java.net

Examples of java.net.DatagramSocket


  }

  public void startServer() {
    try {
      ExecutorService exec = Executors.newFixedThreadPool(100);
      this.socket = new DatagramSocket(this.port);
      while (true) {
        byte[] buf = new byte[256];
        DatagramPacket dp = new DatagramPacket(buf, buf.length);
        this.socket.receive(dp);
        Worker w = new Worker(dp);
View Full Code Here


    }
  }
 
  @Override
  public String toString() {
    DatagramSocket socket = this.channel.socket();
    return new PeerAddress(socket.getLocalAddress(), socket.getLocalPort()).toString();
  }
View Full Code Here

 
  /**
   * @return
   */
  public PeerAddress getInfo() {
    DatagramSocket socket = this.channel.socket();
    return new PeerAddress(socket.getLocalAddress(), socket.getLocalPort());
  }
View Full Code Here

    private DatagramActionResult processAction(DatagramActionDefinition definition, String nodeAddress)
        throws Exception
    {
        InetSocketAddress socketAddress = new InetSocketAddress(nodeAddress, definition.getPort());

        DatagramSocket socket = new DatagramSocket();
        socket.setSoTimeout(definition.getReadTimeoutMs());
        socket.setSendBufferSize(definition.getMessage().length);
        socket.setReceiveBufferSize(definition.getExpectedResponse().length);

        byte[] sendData = definition.getMessage();
        DatagramPacket sendPacket = new DatagramPacket(sendData, 0, sendData.length, socketAddress);

        try {
            socket.send(sendPacket);
            return processDatagramAnswer(definition, socket);
        } finally {
            socket.close();
        }
    }
View Full Code Here

            this.interrupt();
            this.join();
        }

        private void processConnections() throws Exception {
            serverSocket = new DatagramSocket(SOCKET_SERVER_PORT);

            // Read packet
            byte[] readBuffer = new byte[1024];
            DatagramPacket packet = new DatagramPacket(readBuffer, readBuffer.length);
            serverSocket.receive(packet);
View Full Code Here

    static {
        initialize();
    }

    private static void initialize() {
        DatagramSocket socket = null;

        try {
            socket = new DatagramSocket();
            DEFAULT_BROADCAST = socket.getBroadcast();
            DEFAULT_REUSE_ADDRESS = socket.getReuseAddress();
            DEFAULT_RECEIVE_BUFFER_SIZE = socket.getReceiveBufferSize();
            DEFAULT_SEND_BUFFER_SIZE = socket.getSendBufferSize();

            // Check if setReceiveBufferSize is supported.
            try {
                socket.setReceiveBufferSize(DEFAULT_RECEIVE_BUFFER_SIZE);
                SET_RECEIVE_BUFFER_SIZE_AVAILABLE = true;
            } catch (SocketException e) {
                SET_RECEIVE_BUFFER_SIZE_AVAILABLE = false;
            }

            // Check if setSendBufferSize is supported.
            try {
                socket.setSendBufferSize(DEFAULT_SEND_BUFFER_SIZE);
                SET_SEND_BUFFER_SIZE_AVAILABLE = true;
            } catch (SocketException e) {
                SET_SEND_BUFFER_SIZE_AVAILABLE = false;
            }

            // Check if getTrafficClass is supported.
            try {
                DEFAULT_TRAFFIC_CLASS = socket.getTrafficClass();
                GET_TRAFFIC_CLASS_AVAILABLE = true;
            } catch (SocketException e) {
                GET_TRAFFIC_CLASS_AVAILABLE = false;
                DEFAULT_TRAFFIC_CLASS = 0;
            }
        } catch (SocketException e) {
            ExceptionMonitor.getInstance().exceptionCaught(e);
        } finally {
            if (socket != null) {
                socket.close();
            }
        }
    }
View Full Code Here

        this.timeOut = timeOut;
       
        if ( isUdp )
        {
            udpServerAddr = new InetSocketAddress( hostName, portNo );
            udpSocket = new DatagramSocket();
        }
        else
        {
            tcpSocket = new Socket( hostName, portNo );
            tcpSocket.setSoTimeout( ( int ) timeOut );
View Full Code Here

        InetSocketAddress inetLocal = (InetSocketAddress) local;
        if ((inetLocal != null) && inetLocal.isUnresolved()) {
            throw new UnresolvedAddressException();
        }

        final DatagramSocket socket = channel.socket();
        try {
            socket.bind(inetLocal);
        } catch (SocketException e) {
            if (socket.isBound()) {
                throw Util.initCause(new AlreadyBoundException(), e);
            }
            if (socket.isClosed()) {
                throw Util.initCause(new ClosedChannelException(), e);
            }
            throw e;
        }
View Full Code Here

        if (value == null || !name.type().isAssignableFrom(value.getClass())) {
            throw new IllegalArgumentException("Bad parameter for " + name);
        }

        StandardSocketOption stdOpt = (StandardSocketOption) name;
        final DatagramSocket socket = channel.socket();
       
        try {
            switch (stdOpt) {
            case SO_SNDBUF:
                socket.setSendBufferSize(((Integer) value).intValue());
                break;

            case SO_RCVBUF:
                socket.setReceiveBufferSize(((Integer) value).intValue());
                break;

            case SO_REUSEADDR:
                socket.setReuseAddress(((Boolean) value).booleanValue());
                break;

            case SO_BROADCAST:
                socket.setBroadcast(((Boolean) value).booleanValue());
                break;

            case IP_TOS:
                socket.setTrafficClass(((Integer) value).intValue());
                break;

            case IP_MULTICAST_IF:
                ((MulticastSocket) socket).
                    setNetworkInterface((NetworkInterface) value);
                break;

            case IP_MULTICAST_TTL:
                ((MulticastSocket) socket).
                    setTimeToLive(((Integer) value).intValue());
                break;

            case IP_MULTICAST_LOOP:
                // TODO should we reverse the sense of setLoopbackMode? -JM
                ((MulticastSocket) socket).
                    setLoopbackMode(((Boolean) value).booleanValue());
                break;
           

            default:
                throw new IllegalArgumentException("Unsupported option " +
                    name);
            }
        } catch (SocketException e) {
            if (socket.isClosed()) {
                throw Util.initCause(new ClosedChannelException(), e);
            }
            throw e;
        }
        return this;
View Full Code Here

        if (!(name instanceof StandardSocketOption)) {
            throw new IllegalArgumentException("Unsupported option " + name);
        }

        StandardSocketOption stdOpt = (StandardSocketOption) name;
        final DatagramSocket socket = channel.socket();
        MulticastSocket msocket;
       
        try {
            switch (stdOpt) {
            case SO_SNDBUF:
                return socket.getSendBufferSize();

            case SO_RCVBUF:
                return socket.getReceiveBufferSize();

            case SO_REUSEADDR:
                return socket.getReuseAddress();

            case SO_BROADCAST:
                return socket.getBroadcast();

            case IP_TOS:
                return socket.getTrafficClass();

            case IP_MULTICAST_IF:
                msocket = (MulticastSocket) socket;
                return msocket.getNetworkInterface();

            case IP_MULTICAST_TTL:
                msocket = (MulticastSocket) socket;
                return msocket.getTimeToLive();

            case IP_MULTICAST_LOOP:
                msocket = (MulticastSocket) socket;
                // TODO should we reverse the sense of getLoopbackMode? -JM
                return (msocket.getLoopbackMode());

            default:
                throw new IllegalArgumentException("Unsupported option " +
                    name);
            }
        } catch (SocketException e) {
            if (socket.isClosed()) {
                throw Util.initCause(new ClosedChannelException(), e);
            }
            throw e;
        }
    }
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.