Search in sources :

Example 46 with DatagramSocket

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

the class Mina2UdpTest method sendUdpMessages.

protected void sendUdpMessages() throws Exception {
    DatagramSocket socket = new DatagramSocket();
    try {
        InetAddress address = InetAddress.getByName("127.0.0.1");
        for (int i = 0; i < messageCount; i++) {
            String text = "Hello Message: " + Integer.toString(i);
            byte[] data = text.getBytes();
            DatagramPacket packet = new DatagramPacket(data, data.length, address, getPort());
            socket.send(packet);
        }
        Thread.sleep(2000);
    } finally {
        socket.close();
    }
}
Also used : DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) InetAddress(java.net.InetAddress) MockEndpoint(org.apache.camel.component.mock.MockEndpoint)

Example 47 with DatagramSocket

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

the class MinaUdpTest method sendUdpMessages.

protected void sendUdpMessages() throws Exception {
    DatagramSocket socket = new DatagramSocket();
    try {
        InetAddress address = InetAddress.getByName("127.0.0.1");
        for (int i = 0; i < messageCount; i++) {
            String text = "Hello Message: " + i;
            byte[] data = text.getBytes();
            DatagramPacket packet = new DatagramPacket(data, data.length, address, getPort());
            socket.send(packet);
            Thread.sleep(100);
        }
    } finally {
        socket.close();
    }
}
Also used : DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) InetAddress(java.net.InetAddress) MockEndpoint(org.apache.camel.component.mock.MockEndpoint)

Example 48 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 49 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)

Example 50 with DatagramSocket

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

the class TestPortmap method testRegistration.

@Test(timeout = 1000)
public void testRegistration() throws IOException, InterruptedException {
    XDR req = new XDR();
    RpcCall.getInstance(++xid, RpcProgramPortmap.PROGRAM, RpcProgramPortmap.VERSION, RpcProgramPortmap.PMAPPROC_SET, new CredentialsNone(), new VerifierNone()).write(req);
    PortmapMapping sent = new PortmapMapping(90000, 1, PortmapMapping.TRANSPORT_TCP, 1234);
    sent.serialize(req);
    byte[] reqBuf = req.getBytes();
    DatagramSocket s = new DatagramSocket();
    DatagramPacket p = new DatagramPacket(reqBuf, reqBuf.length, pm.getUdpServerLoAddress());
    try {
        s.send(p);
    } finally {
        s.close();
    }
    // Give the server a chance to process the request
    Thread.sleep(100);
    boolean found = false;
    @SuppressWarnings("unchecked") Map<String, PortmapMapping> map = (Map<String, PortmapMapping>) Whitebox.getInternalState(pm.getHandler(), "map");
    for (PortmapMapping m : map.values()) {
        if (m.getPort() == sent.getPort() && PortmapMapping.key(m).equals(PortmapMapping.key(sent))) {
            found = true;
            break;
        }
    }
    Assert.assertTrue("Registration failed", found);
}
Also used : DatagramSocket(java.net.DatagramSocket) XDR(org.apache.hadoop.oncrpc.XDR) VerifierNone(org.apache.hadoop.oncrpc.security.VerifierNone) DatagramPacket(java.net.DatagramPacket) CredentialsNone(org.apache.hadoop.oncrpc.security.CredentialsNone) Map(java.util.Map) Test(org.junit.Test)

Aggregations

DatagramSocket (java.net.DatagramSocket)294 DatagramPacket (java.net.DatagramPacket)137 IOException (java.io.IOException)112 SocketException (java.net.SocketException)74 InetAddress (java.net.InetAddress)63 InetSocketAddress (java.net.InetSocketAddress)46 UnknownHostException (java.net.UnknownHostException)33 Test (org.junit.Test)27 SocketTimeoutException (java.net.SocketTimeoutException)24 InterruptedIOException (java.io.InterruptedIOException)18 PortUnreachableException (java.net.PortUnreachableException)18 ServerSocket (java.net.ServerSocket)18 BindException (java.net.BindException)16 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)16 DatagramChannel (java.nio.channels.DatagramChannel)14 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)11 MulticastSocket (java.net.MulticastSocket)10 SocketAddress (java.net.SocketAddress)9 ByteBuffer (java.nio.ByteBuffer)8 ArrayList (java.util.ArrayList)6