Search in sources :

Example 6 with DatagramSocket

use of java.net.DatagramSocket in project hadoop by apache.

the class TestGangliaSink method testShouldCreateDatagramSocketByDefault.

@Test
public void testShouldCreateDatagramSocketByDefault() throws Exception {
    SubsetConfiguration conf = new ConfigBuilder().subset("test.sink.ganglia");
    GangliaSink30 gangliaSink = new GangliaSink30();
    gangliaSink.init(conf);
    DatagramSocket socket = gangliaSink.getDatagramSocket();
    assertFalse("Did not create DatagramSocket", socket == null || socket instanceof MulticastSocket);
}
Also used : MulticastSocket(java.net.MulticastSocket) DatagramSocket(java.net.DatagramSocket) ConfigBuilder(org.apache.hadoop.metrics2.impl.ConfigBuilder) SubsetConfiguration(org.apache.commons.configuration2.SubsetConfiguration) Test(org.junit.Test)

Example 7 with DatagramSocket

use of java.net.DatagramSocket in project hadoop by apache.

the class TestGangliaSink method testShouldCreateDatagramSocketIfMulticastIsDisabled.

@Test
public void testShouldCreateDatagramSocketIfMulticastIsDisabled() throws Exception {
    SubsetConfiguration conf = new ConfigBuilder().add("test.sink.ganglia.multicast", false).subset("test.sink.ganglia");
    GangliaSink30 gangliaSink = new GangliaSink30();
    gangliaSink.init(conf);
    DatagramSocket socket = gangliaSink.getDatagramSocket();
    assertFalse("Did not create DatagramSocket", socket == null || socket instanceof MulticastSocket);
}
Also used : MulticastSocket(java.net.MulticastSocket) DatagramSocket(java.net.DatagramSocket) ConfigBuilder(org.apache.hadoop.metrics2.impl.ConfigBuilder) SubsetConfiguration(org.apache.commons.configuration2.SubsetConfiguration) Test(org.junit.Test)

Example 8 with DatagramSocket

use of java.net.DatagramSocket in project hadoop by apache.

the class SimpleUdpClient method run.

public void run() throws IOException {
    InetAddress IPAddress = InetAddress.getByName(host);
    byte[] sendData = request.getBytes();
    byte[] receiveData = new byte[65535];
    // Use the provided socket if there is one, else just make a new one.
    DatagramSocket socket = this.clientSocket == null ? new DatagramSocket() : this.clientSocket;
    try {
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
        socket.send(sendPacket);
        socket.setSoTimeout(udpTimeoutMillis);
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        socket.receive(receivePacket);
        // Check reply status
        XDR xdr = new XDR(Arrays.copyOfRange(receiveData, 0, receivePacket.getLength()));
        RpcReply reply = RpcReply.read(xdr);
        if (reply.getState() != RpcReply.ReplyState.MSG_ACCEPTED) {
            throw new IOException("Request failed: " + reply.getState());
        }
    } finally {
        // caller of this UDP client to close that socket.
        if (this.clientSocket == null) {
            socket.close();
        }
    }
}
Also used : DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) IOException(java.io.IOException) InetAddress(java.net.InetAddress)

Example 9 with DatagramSocket

use of java.net.DatagramSocket in project hadoop by apache.

the class PrivilegedNfsGatewayStarter method init.

@Override
public void init(DaemonContext context) throws Exception {
    System.err.println("Initializing privileged NFS client socket...");
    NfsConfiguration conf = new NfsConfiguration();
    int clientPort = conf.getInt(NfsConfigKeys.DFS_NFS_REGISTRATION_PORT_KEY, NfsConfigKeys.DFS_NFS_REGISTRATION_PORT_DEFAULT);
    if (clientPort < 1 || clientPort > 1023) {
        throw new RuntimeException("Must start privileged NFS server with '" + NfsConfigKeys.DFS_NFS_REGISTRATION_PORT_KEY + "' configured to a " + "privileged port.");
    }
    try {
        InetSocketAddress socketAddress = new InetSocketAddress("localhost", clientPort);
        registrationSocket = new DatagramSocket(null);
        registrationSocket.setReuseAddress(true);
        registrationSocket.bind(socketAddress);
    } catch (SocketException e) {
        LOG.error("Init failed for port=" + clientPort, e);
        throw e;
    }
    args = context.getArguments();
}
Also used : SocketException(java.net.SocketException) DatagramSocket(java.net.DatagramSocket) InetSocketAddress(java.net.InetSocketAddress) NfsConfiguration(org.apache.hadoop.hdfs.nfs.conf.NfsConfiguration)

Example 10 with DatagramSocket

use of java.net.DatagramSocket in project hadoop by apache.

the class TestUdpServer method testRequest.

static void testRequest(XDR request, XDR request2) {
    try {
        DatagramSocket clientSocket = new DatagramSocket();
        InetAddress IPAddress = InetAddress.getByName("localhost");
        byte[] sendData = request.getBytes();
        byte[] receiveData = new byte[65535];
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, Nfs3Constant.SUN_RPCBIND);
        clientSocket.send(sendPacket);
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        clientSocket.receive(receivePacket);
        clientSocket.close();
    } catch (UnknownHostException e) {
        System.err.println("Don't know about host: localhost.");
        System.exit(1);
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for " + "the connection to: localhost.");
        System.exit(1);
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) IOException(java.io.IOException) InetAddress(java.net.InetAddress)

Aggregations

DatagramSocket (java.net.DatagramSocket)266 DatagramPacket (java.net.DatagramPacket)120 IOException (java.io.IOException)98 SocketException (java.net.SocketException)71 InetAddress (java.net.InetAddress)57 InetSocketAddress (java.net.InetSocketAddress)45 UnknownHostException (java.net.UnknownHostException)26 Test (org.junit.Test)25 SocketTimeoutException (java.net.SocketTimeoutException)24 InterruptedIOException (java.io.InterruptedIOException)18 PortUnreachableException (java.net.PortUnreachableException)18 BindException (java.net.BindException)16 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)16 ServerSocket (java.net.ServerSocket)14 DatagramChannel (java.nio.channels.DatagramChannel)13 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)11 MulticastSocket (java.net.MulticastSocket)10 SocketAddress (java.net.SocketAddress)8 ByteBuffer (java.nio.ByteBuffer)8 SmallTest (android.test.suitebuilder.annotation.SmallTest)5