Search in sources :

Example 36 with SocketAddress

use of java.net.SocketAddress in project robovm by robovm.

the class DatagramChannelImpl method receiveDirectImpl.

private SocketAddress receiveDirectImpl(ByteBuffer target, boolean loop) throws IOException {
    SocketAddress retAddr = null;
    DatagramPacket receivePacket = new DatagramPacket(EmptyArray.BYTE, 0);
    int oldposition = target.position();
    int received = 0;
    do {
        received = IoBridge.recvfrom(false, fd, target, 0, receivePacket, isConnected());
        if (receivePacket != null && receivePacket.getAddress() != null) {
            // copy the data of received packet
            if (received > 0) {
                target.position(oldposition + received);
            }
            retAddr = receivePacket.getSocketAddress();
            break;
        }
    } while (loop);
    return retAddr;
}
Also used : DatagramPacket(java.net.DatagramPacket) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 37 with SocketAddress

use of java.net.SocketAddress in project robovm by robovm.

the class OldDatagramSocketTest method test_ConstructorLjava_net_SocketAddress.

public void test_ConstructorLjava_net_SocketAddress() {
    class mySocketAddress extends SocketAddress {

        public mySocketAddress() {
        }
    }
    try {
        try {
            int portNumber = Support_PortManager.getNextPortForUDP();
            ds = new java.net.DatagramSocket(new InetSocketAddress(InetAddress.getLocalHost(), portNumber));
            assertTrue(ds.getBroadcast());
            assertTrue("Created socket with incorrect port", ds.getLocalPort() == portNumber);
            assertTrue("Created socket with incorrect address", ds.getLocalAddress().equals(InetAddress.getLocalHost()));
        } catch (Exception e) {
            fail("Could not create DatagramSocket : " + e.getMessage());
        }
        try {
            int portNumber = Support_PortManager.getNextPortForUDP();
            ds = new java.net.DatagramSocket(new mySocketAddress());
            fail("No exception when constucting datagramSocket with unsupported SocketAddress type");
        } catch (IllegalArgumentException e) {
        }
        //regression for Harmony-894
        ds = new DatagramSocket((SocketAddress) null);
        assertTrue(ds.getBroadcast());
    } catch (Exception ex) {
        fail("unexpected exception when datagramSocket SocketAddress constructor test");
    }
    /*
        SecurityManager sm = new SecurityManager() {

            public void checkPermission(Permission perm) {
            }

            public void checkListen(int port) {
                throw new SecurityException();
            }
        };

        SecurityManager oldSm = System.getSecurityManager();
        System.setSecurityManager(sm);
        try {
            new DatagramSocket(new InetSocketAddress(
                    InetAddress.getLocalHost(), 1));
            fail("SecurityException should be thrown.");
        } catch (SecurityException e) {
            // expected
        } catch (SocketException e) {
            fail("SocketException was thrown.");
        } catch (UnknownHostException e) {
            fail("UnknownHostException was thrown.");
        } finally {
            System.setSecurityManager(oldSm);
        }
        */
    InetSocketAddress isa = null;
    try {
        isa = new InetSocketAddress(InetAddress.getLocalHost(), 1);
    } catch (UnknownHostException e) {
        fail("UnknownHostException was thrown.");
    }
}
Also used : DatagramSocket(java.net.DatagramSocket) UnknownHostException(java.net.UnknownHostException) DatagramSocket(java.net.DatagramSocket) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) BindException(java.net.BindException) InterruptedIOException(java.io.InterruptedIOException) UnknownHostException(java.net.UnknownHostException) SocketException(java.net.SocketException) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) SocketTimeoutException(java.net.SocketTimeoutException) PortUnreachableException(java.net.PortUnreachableException)

Example 38 with SocketAddress

use of java.net.SocketAddress in project robovm by robovm.

the class OldDatagramSocketTest method test_sendLjava_net_DatagramPacket.

public void test_sendLjava_net_DatagramPacket() throws Exception {
    // Test for method void
    // java.net.DatagramSocket.send(java.net.DatagramPacket)
    int[] ports = Support_PortManager.getNextPortsForUDP(2);
    final int portNumber = ports[0];
    class TestDGSend implements Runnable {

        Thread pThread;

        public TestDGSend(Thread t) {
            pThread = t;
        }

        public void run() {
            try {
                byte[] rbuf = new byte[1000];
                sds = new DatagramSocket(portNumber);
                DatagramPacket sdp = new DatagramPacket(rbuf, rbuf.length);
                sds.setSoTimeout(6000);
                sds.receive(sdp);
                retval = new String(rbuf, 0, testString.length());
                pThread.interrupt();
            } catch (java.io.InterruptedIOException e) {
                System.out.println("Recv operation timed out");
                pThread.interrupt();
                ds.close();
            } catch (Exception e) {
                System.out.println("Failed to establish Dgram server: " + e);
            }
        }
    }
    try {
        new Thread(new TestDGSend(Thread.currentThread()), "DGServer").start();
        ds = new java.net.DatagramSocket(ports[1]);
        dp = new DatagramPacket(testString.getBytes(), testString.length(), InetAddress.getLocalHost(), portNumber);
        // Wait to allow send to occur
        try {
            Thread.sleep(500);
            ds.send(dp);
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            ds.close();
            assertTrue("Incorrect data sent: " + retval, retval.equals(testString));
        }
    } catch (Exception e) {
        fail("Exception during send test : " + e.getMessage());
    } finally {
        ds.close();
    }
    /*
        SecurityManager sm = new SecurityManager() {

            public void checkPermission(Permission perm) {
            }

            public void checkMulticast(InetAddress maddr) {
                throw new SecurityException();
            }

            public void checkConnect(String host,
                    int port) {
                throw new SecurityException();
            }
        };
        try {

            ds = new java.net.DatagramSocket(ports[1]);
            dp = new DatagramPacket(testString.getBytes(), testString.length(),
                    InetAddress.getLocalHost(), portNumber);

            SecurityManager oldSm = System.getSecurityManager();
            System.setSecurityManager(sm);
            try {
                ds.send(dp);
                fail("SecurityException should be thrown.");
            } catch (SecurityException e) {
                // expected
            } catch (SocketException e) {
                fail("SocketException was thrown.");
            } finally {
                System.setSecurityManager(oldSm);
            }
        } catch(Exception e) {
            fail("Unexpected exception was thrown: " + e.getMessage());
        }
        */
    DatagramSocket socket = null;
    try {
        byte[] rbuf = new byte[1000];
        DatagramPacket rdp = new DatagramPacket(rbuf, rbuf.length);
        SocketAddress address = new InetSocketAddress(portNumber);
        DatagramChannel channel = DatagramChannel.open();
        channel.configureBlocking(false);
        socket = channel.socket();
        socket.send(rdp);
        fail("IllegalBlockingModeException was not thrown.");
    } catch (IllegalBlockingModeException ibme) {
    //expected
    } catch (IOException ioe) {
        fail("IOException was thrown: " + ioe.getMessage());
    } finally {
        socket.close();
    }
    //Regression for HARMONY-1118
    class testDatagramSocket extends DatagramSocket {

        public testDatagramSocket(DatagramSocketImpl impl) {
            super(impl);
        }
    }
    class testDatagramSocketImpl extends DatagramSocketImpl {

        protected void create() throws SocketException {
        }

        protected void bind(int arg0, InetAddress arg1) throws SocketException {
        }

        protected void send(DatagramPacket arg0) throws IOException {
        }

        protected int peek(InetAddress arg0) throws IOException {
            return 0;
        }

        protected int peekData(DatagramPacket arg0) throws IOException {
            return 0;
        }

        protected void receive(DatagramPacket arg0) throws IOException {
        }

        protected void setTTL(byte arg0) throws IOException {
        }

        protected byte getTTL() throws IOException {
            return 0;
        }

        protected void setTimeToLive(int arg0) throws IOException {
        }

        protected int getTimeToLive() throws IOException {
            return 0;
        }

        protected void join(InetAddress arg0) throws IOException {
        }

        protected void leave(InetAddress arg0) throws IOException {
        }

        protected void joinGroup(SocketAddress arg0, NetworkInterface arg1) throws IOException {
        }

        protected void leaveGroup(SocketAddress arg0, NetworkInterface arg1) throws IOException {
        }

        protected void close() {
        }

        public void setOption(int arg0, Object arg1) throws SocketException {
        }

        public Object getOption(int arg0) throws SocketException {
            return null;
        }
    }
    InetSocketAddress sa = new InetSocketAddress(InetAddress.getLocalHost(), 0);
    //no exception expected for next line
    new testDatagramSocket(new testDatagramSocketImpl()).send(new DatagramPacket(new byte[272], 3, sa));
    // Regression test for Harmony-2938
    InetAddress i = InetAddress.getByName("127.0.0.1");
    DatagramSocket d = new DatagramSocket(0, i);
    try {
        d.send(new DatagramPacket(new byte[] { 1 }, 1));
        fail("should throw NPE.");
    } catch (NullPointerException e) {
    // expected;
    } finally {
        d.close();
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) InterruptedIOException(java.io.InterruptedIOException) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) NetworkInterface(java.net.NetworkInterface) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) BindException(java.net.BindException) InterruptedIOException(java.io.InterruptedIOException) UnknownHostException(java.net.UnknownHostException) SocketException(java.net.SocketException) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) SocketTimeoutException(java.net.SocketTimeoutException) PortUnreachableException(java.net.PortUnreachableException) DatagramSocketImpl(java.net.DatagramSocketImpl) DatagramSocket(java.net.DatagramSocket) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) InetAddress(java.net.InetAddress)

Example 39 with SocketAddress

use of java.net.SocketAddress in project robovm by robovm.

the class OldDatagramSocketTest method test_getChannel.

public void test_getChannel() throws Exception {
    assertNull(new DatagramSocket().getChannel());
    int portNumber = Support_PortManager.getNextPortForUDP();
    DatagramSocket ds = null;
    try {
        InetAddress ia = InetAddress.getByName(Support_Configuration.IPv6GlobalAddressJcl4);
        ds = new DatagramSocket();
        assertNull(ds.getChannel());
        ds.connect(ia, portNumber);
        assertNull(ds.getChannel());
    } catch (SocketException e) {
        fail("SocketException was thrown.");
    } finally {
        ds.disconnect();
        ds.close();
    }
    portNumber = Support_PortManager.getNextPortForUDP();
    SocketAddress address = new InetSocketAddress(portNumber);
    DatagramChannel channel = DatagramChannel.open();
    DatagramSocket socket = channel.socket();
    assertEquals(channel, socket.getChannel());
    socket.close();
}
Also used : SocketException(java.net.SocketException) DatagramSocket(java.net.DatagramSocket) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress)

Example 40 with SocketAddress

use of java.net.SocketAddress in project robovm by robovm.

the class OldSocketTest method test_bindLjava_net_SocketAddress.

public void test_bindLjava_net_SocketAddress() throws IOException {
    class mySocketAddress extends SocketAddress {

        public mySocketAddress() {
        }
    }
    // Address we cannot bind to
    Socket theSocket = new Socket();
    try {
        theSocket.bind(new InetSocketAddress(InetAddress.getByAddress(Support_Configuration.nonLocalAddressBytes), 80));
        fail("No exception when binding to bad address:" + theSocket.getLocalSocketAddress().toString());
    } catch (IOException ex) {
    }
    theSocket.close();
    // now create a socket that is not bound and then bind it
    theSocket = new Socket();
    theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0));
    // validate that the localSocketAddress reflects the address we
    // bound to
    assertEquals(new InetSocketAddress(InetAddress.getLocalHost(), theSocket.getLocalPort()), theSocket.getLocalSocketAddress());
    // make sure we can now connect and that connections appear to come
    // from the address we bound to.
    ServerSocket serverSocket = new ServerSocket(0, 5);
    theSocket.connect(serverSocket.getLocalSocketAddress());
    Socket servSock = serverSocket.accept();
    assertEquals(new InetSocketAddress(InetAddress.getLocalHost(), theSocket.getLocalPort()), servSock.getRemoteSocketAddress());
    theSocket.close();
    servSock.close();
    serverSocket.close();
    // validate if we pass in null that it picks an address for us and
    // all is ok
    theSocket = new Socket();
    theSocket.bind(null);
    assertNotNull("Bind with null did not work", theSocket.getLocalSocketAddress());
    theSocket.close();
    // now check the error conditions
    // Address that we have already bound to
    theSocket = new Socket();
    Socket theSocket2 = new Socket();
    try {
        theSocket.bind(null);
        theSocket2.bind(theSocket.getLocalSocketAddress());
        fail("No exception binding to address that is not available");
    } catch (IOException ex) {
    }
    theSocket.close();
    theSocket2.close();
    // unsupported SocketAddress subclass
    theSocket = new Socket();
    try {
        theSocket.bind(new mySocketAddress());
        fail("No exception when binding using unsupported SocketAddress subclass");
    } catch (IllegalArgumentException ex) {
    }
    theSocket.close();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Aggregations

SocketAddress (java.net.SocketAddress)717 InetSocketAddress (java.net.InetSocketAddress)542 Test (org.junit.Test)169 IOException (java.io.IOException)154 Socket (java.net.Socket)105 InetAddress (java.net.InetAddress)56 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)42 Proxy (java.net.Proxy)39 ArrayList (java.util.ArrayList)37 SocketChannel (java.nio.channels.SocketChannel)36 SocketException (java.net.SocketException)34 ServerSocket (java.net.ServerSocket)33 HashMap (java.util.HashMap)33 Channel (org.jboss.netty.channel.Channel)33 UnknownHostException (java.net.UnknownHostException)32 ByteBuffer (java.nio.ByteBuffer)32 Map (java.util.Map)31 Set (java.util.Set)31 Channel (io.netty.channel.Channel)30 HashSet (java.util.HashSet)30