Example usage for java.net DatagramSocket connect

List of usage examples for java.net DatagramSocket connect

Introduction

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

Prototype

public void connect(SocketAddress addr) throws SocketException 

Source Link

Document

Connects this socket to a remote socket address (IP address + port number).

Usage

From source file:Main.java

public static void main(String args[]) {
    try {//from  w w w . ja  va 2s  . c o  m

        InetAddress ia = InetAddress.getByName("www.java2s.com");

        DatagramSocket ds = new DatagramSocket();

        byte buffer[] = "hello".getBytes();
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 80);
        ds.connect(InetSocketAddress.createUnresolved("google.com", 80));
        ds.send(dp);
        ds.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String args[]) {
    try {//from   w  ww.j ava 2 s . c  o  m

        InetAddress ia = InetAddress.getByName("www.java2s.com");

        DatagramSocket ds = new DatagramSocket();

        byte buffer[] = "hello".getBytes();
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 80);
        ds.connect(InetSocketAddress.createUnresolved("google.com", 8080));
        ds.send(dp);
        ds.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String args[]) {
    try {/*from  www.j  av  a  2  s  .c o  m*/

        InetAddress ia = InetAddress.getByName("www.java2s.com");

        DatagramSocket ds = new DatagramSocket(8080, ia);

        byte buffer[] = "hello".getBytes();
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 80);
        ds.connect(InetSocketAddress.createUnresolved("google.com", 8080));
        ds.send(dp);
        ds.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String args[]) {
    try {//from w w w  . j  a  v  a2s .  c o  m

        InetAddress ia = InetAddress.getByName("www.java2s.com");

        DatagramSocket ds = new DatagramSocket(8080, ia);

        byte buffer[] = "hello".getBytes();
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 80);
        ds.connect(InetSocketAddress.createUnresolved("google.com", 8080));

        DatagramChannel channel = ds.getChannel();

        ds.send(dp);
        ds.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String args[]) {
    try {/*  ww  w  .jav a2s .  c o  m*/

        InetAddress ia = InetAddress.getByName("www.java2s.com");

        DatagramSocket ds = new DatagramSocket(InetSocketAddress.createUnresolved("google.com", 8080));

        byte buffer[] = "hello".getBytes();
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 80);
        ds.connect(InetSocketAddress.createUnresolved("google.com", 8080));
        ds.send(dp);
        ds.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.barchart.udt.AppServer.java

private static InetAddress getLocalHostViaUdp() throws UnknownHostException {
    final InetSocketAddress sa = new InetSocketAddress("www.google.com", 80);

    DatagramSocket sock = null;
    try {//w ww  . j a v  a2 s .c  om
        sock = new DatagramSocket();
        sock.connect(sa);
        final InetAddress address = sock.getLocalAddress();
        return address;
    } catch (final SocketException e) {
        log.warn("Exception getting address", e);
        return InetAddress.getLocalHost();
    } finally {
        if (sock != null) {
            sock.close();
        }
    }
}

From source file:com.googlecode.jmxtrans.connections.DatagramSocketFactory.java

/**
 * Creates the socket and the writer to go with it.
 *//*ww w.  j  av a2s  . co m*/
@Override
public DatagramSocket makeObject(SocketAddress socketAddress) throws Exception {
    DatagramSocket datagramSocket = new DatagramSocket();
    datagramSocket.connect(socketAddress);
    return datagramSocket;
}

From source file:com.navercorp.pinpoint.collector.receiver.thrift.udp.UDPReceiverTest.java

@Test
public void sendSocketBufferSize() throws IOException {
    DatagramPacket datagramPacket = new DatagramPacket(new byte[0], 0, 0);

    DatagramSocket datagramSocket = new DatagramSocket();
    datagramSocket.connect(new InetSocketAddress(ADDRESS, PORT));

    datagramSocket.send(datagramPacket);
    datagramSocket.close();//from  w  ww.  j ava2  s.c  om
}

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  ww w .  ja v a 2 s.c  o m
    }
    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();
        }
    }
}

From source file:com.navercorp.pinpoint.profiler.sender.UdpDataSender.java

private DatagramSocket createSocket(String host, int port, int timeout, int sendBufferSize) {
    try {/*from  w w  w  .j a  v a2 s .c o m*/
        final DatagramSocket datagramSocket = new DatagramSocket();

        datagramSocket.setSoTimeout(timeout);
        datagramSocket.setSendBufferSize(sendBufferSize);
        if (logger.isInfoEnabled()) {
            final int checkSendBufferSize = datagramSocket.getSendBufferSize();
            if (sendBufferSize != checkSendBufferSize) {
                logger.info("DatagramSocket.setSendBufferSize() error. {}!={}", sendBufferSize,
                        checkSendBufferSize);
            }
        }

        final InetSocketAddress serverAddress = new InetSocketAddress(host, port);
        datagramSocket.connect(serverAddress);
        return datagramSocket;
    } catch (SocketException e) {
        throw new IllegalStateException("DatagramSocket create fail. Cause" + e.getMessage(), e);
    }
}