Example usage for java.net DatagramSocket send

List of usage examples for java.net DatagramSocket send

Introduction

In this page you can find the example usage for java.net DatagramSocket send.

Prototype

public void send(DatagramPacket p) throws IOException 

Source Link

Document

Sends a datagram packet from this socket.

Usage

From source file:com.webobjects.monitor.application.HostsPage.java

private boolean hostMeetsMinimumVersion(InetAddress anAddress) {
    byte[] versionRequest;

    try {/*from   ww  w  .  ja v a2 s .  c om*/
        versionRequest = ("womp://queryVersion").getBytes(CharEncoding.UTF_8);
    } catch (UnsupportedEncodingException uee) {
        versionRequest = ("womp://queryVersion").getBytes();
    }
    DatagramPacket outgoingPacket = new DatagramPacket(versionRequest, versionRequest.length, anAddress,
            WOApplication.application().lifebeatDestinationPort());

    byte[] mbuffer = new byte[1000];
    DatagramPacket incomingPacket = new DatagramPacket(mbuffer, mbuffer.length);
    DatagramSocket socket = null;

    try {
        socket = new DatagramSocket();
        socket.send(outgoingPacket);
        incomingPacket.setLength(mbuffer.length);
        socket.setSoTimeout(2000);
        socket.receive(incomingPacket);
        String reply = new String(incomingPacket.getData());
        if (reply.startsWith("womp://replyVersion/")) {
            int lastIndex = reply.lastIndexOf(":webObjects");
            lastIndex += 11;
            String version = reply.substring(lastIndex);
            if (version.equals("4.5")) {
                return false;
            }
        } else {
            return false;
        }
    } catch (InterruptedIOException iioe) {
        return true;
    } catch (SocketException se) {
        return true;
    } catch (Throwable e) {
        return false;
    } finally {
        if (socket != null) {
            socket.close();
        }
    }

    return true;
}

From source file:org.shept.util.Monitor.java

/**
 * Send a simple 'Keep Alive' Datagram to the receiver
 *//*w  w w  .j  a  v  a  2  s .  com*/
public Boolean keepAlive() {
    try {
        InetAddress adr = InetAddress.getByName(hostname);
        byte[] data = sendMsg.getBytes();
        DatagramSocket socket = new DatagramSocket();
        DatagramPacket pack = new DatagramPacket(data, data.length, adr, hostPort);
        socket.setSoTimeout(msecsSendTimeout);
        socket.send(pack);
        socket.close();
        return true;
    } catch (Exception ex) {
        return false;
    }
}

From source file:org.hyperic.util.ntp.NtpClient.java

public NtpResponse getResponse() throws SocketException, UnknownHostException, IOException {
    DatagramSocket socket = new DatagramSocket();
    socket.setSoTimeout(this.timeout);

    InetAddress address = InetAddress.getByName(this.hostname);
    byte[] data = NtpResponse.getRequestBytes();
    DatagramPacket packet = new DatagramPacket(data, data.length, address, this.port);
    socket.send(packet);

    packet = new DatagramPacket(data, data.length);
    socket.receive(packet);/*w ww . jav  a2  s.c  om*/

    NtpResponse response = NtpResponse.decodeResponse(now(), packet.getData());
    return response;
}

From source file:org.wso2.esb.integration.common.utils.clients.UDPClient.java

/**
 * Send UDP message/* w  w  w  . j a  v a  2 s .  c  om*/
 *
 * @param message message
 */
public void sendMessage(String message) throws IOException {
    DatagramSocket clientSocket = null;

    try {
        clientSocket = new DatagramSocket();
        InetAddress inetAddress = InetAddress.getByName(host);
        byte[] sendData = message.getBytes();
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, inetAddress, port);
        clientSocket.send(sendPacket);
    } finally {
        if (clientSocket != null) {
            clientSocket.close();
        }
    }
}

From source file:org.apache.camel.component.netty.NettyUdpWithInOutUsingPlainSocketTest.java

private String sendAndReceiveUdpMessages(String input) throws Exception {
    DatagramSocket socket = new DatagramSocket();
    InetAddress address = InetAddress.getByName("127.0.0.1");

    // must append delimiter
    byte[] data = (input + "\n").getBytes();

    DatagramPacket packet = new DatagramPacket(data, data.length, address, PORT);
    LOG.debug("+++ Sending data +++");
    socket.send(packet);

    Thread.sleep(1000);/*from w ww.j  av  a 2s.c  o  m*/

    byte[] buf = new byte[128];
    DatagramPacket receive = new DatagramPacket(buf, buf.length, address, PORT);
    LOG.debug("+++ Receiving data +++");
    socket.receive(receive);

    socket.close();

    return new String(receive.getData(), 0, receive.getLength());
}

From source file:werewolf_client.Werewolf_Client.java

public void sentToClient(String adr, int p, String message)
        throws SocketException, UnknownHostException, IOException {
    DatagramSocket sender = new DatagramSocket();
    InetAddress ip = InetAddress.getByName(adr);
    byte[] sendData = new byte[1024];
    sendData = message.getBytes();//from  ww  w .j a v a 2s.  co m

    DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, ip, p);
    double rand = random.nextDouble();
    if (rand < 0.85) {
        sender.send(sendPacket);
    }
    sender.close();
}

From source file:com.all.landownloader.discovery.LanDiscoverySocket.java

public void reply(InetAddress address) throws IllegalArgumentException {
    try {/*w w  w  . ja v  a2 s  . co  m*/
        byte[] buf = createPacket(REPLY_MSG);
        DatagramPacket packet = new DatagramPacket(buf, buf.length, address, PORT);
        DatagramSocket socket = new DatagramSocket();
        socket.send(packet);
        socket.close();
    } catch (IOException e) {
    }
}

From source file:com.clustercontrol.poller.impl.UdpTransportMappingImpl.java

public void sendMessage(UdpAddress targetAddress, byte[] message, TransportStateReference tmStateReference)
        throws java.io.IOException {
    InetSocketAddress targetSocketAddress = new InetSocketAddress(targetAddress.getInetAddress(),
            targetAddress.getPort());/*from w ww .  j av  a  2s.c  o  m*/
    if (logger.isDebugEnabled()) {
        logger.debug("Sending message to " + targetAddress + " with length " + message.length + ": "
                + new OctetString(message).toHexString());
    }
    DatagramSocket s = ensureSocket();
    s.send(new DatagramPacket(message, message.length, targetSocketAddress));
}

From source file:com.mendhak.gpslogger.common.OpenGTSClient.java

public void sendRAW(String id, String accountName, SerializableLocation[] locations) throws Exception {
    for (SerializableLocation loc : locations) {
        if (Utilities.IsNullOrEmpty(accountName)) {
            accountName = id;/*from w ww. j  a va2s  .c o m*/
        }
        String message = accountName + "/" + id + "/" + GPRMCEncode(loc);
        DatagramSocket socket = new DatagramSocket();
        byte[] buffer = message.getBytes();
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName(server), port);
        tracer.debug("Sending UDP " + message);
        socket.send(packet);
        socket.close();
    }
}

From source file:io.github.gsteckman.rpi_rest.SsdpHandler.java

private void sendResponse(final SocketAddress addr) {
    if (!(addr instanceof InetSocketAddress)) {
        LOG.warn("Don't know how to handle non Internet addresses");
        return;//from   w  ww .  j a v  a  2s  .c om
    }
    DatagramSocket sock = null;
    LOG.debug("Responding to " + addr.toString());

    try {
        sock = new DatagramSocket();
        sock.connect(addr);
        byte[] ba = generateSearchResponse().getBytes();
        sock.send(new DatagramPacket(ba, ba.length));
    } catch (IOException e) {
        LOG.error(e.getMessage());
    } finally {
        if (sock != null) {
            sock.close();
        }
    }
}