use of java.net.SocketAddress in project android_frameworks_base by ResurrectionRemix.
the class Network method bindSocket.
/**
* Binds the specified {@link FileDescriptor} to this {@code Network}. All data traffic on the
* socket represented by this file descriptor will be sent on this {@code Network},
* irrespective of any process-wide network binding set by
* {@link ConnectivityManager#bindProcessToNetwork}. The socket must not be connected.
*/
public void bindSocket(FileDescriptor fd) throws IOException {
try {
final SocketAddress peer = Os.getpeername(fd);
final InetAddress inetPeer = ((InetSocketAddress) peer).getAddress();
if (!inetPeer.isAnyLocalAddress()) {
// routing upon mark changes.
throw new SocketException("Socket is connected");
}
} catch (ErrnoException e) {
// getpeername() failed.
if (e.errno != OsConstants.ENOTCONN) {
throw e.rethrowAsSocketException();
}
} catch (ClassCastException e) {
// Wasn't an InetSocketAddress.
throw new SocketException("Only AF_INET/AF_INET6 sockets supported");
}
final int err = NetworkUtils.bindSocketToNetwork(fd.getInt$(), netId);
if (err != 0) {
// bindSocketToNetwork returns negative errno.
throw new ErrnoException("Binding socket to network " + netId, -err).rethrowAsSocketException();
}
}
use of java.net.SocketAddress in project GNS by MobilityFirst.
the class Pinger method checkConnection.
/**
*
* @param host
* @param port
* @param timeoutMs
* @return the time to connect or NOCONNECTION
*/
public static long checkConnection(String host, int port, int timeoutMs) {
//default check value
long start = NOCONNECTION;
//default check value
long end = NOCONNECTION;
// default for bad connection
long total = NOCONNECTION;
//make an unbound socket
Socket theSock = new Socket();
try {
InetAddress address = InetAddress.getByName(host);
SocketAddress sockaddr = new InetSocketAddress(address, port);
// Create the socket with a timeout
// when a timeout occurs, we will get timout exp.
// also time our connection this gets very close to the real time
start = System.currentTimeMillis();
theSock.connect(sockaddr, timeoutMs);
end = System.currentTimeMillis();
} catch (UnknownHostException e) {
start = NOCONNECTION;
end = NOCONNECTION;
} catch (SocketTimeoutException e) {
start = NOCONNECTION;
end = NOCONNECTION;
} catch (IOException e) {
start = NOCONNECTION;
end = NOCONNECTION;
} finally {
if (theSock != null) {
try {
theSock.close();
} catch (IOException e) {
}
}
if ((start != NOCONNECTION) && (end != NOCONNECTION)) {
total = end - start;
}
}
//returns NOCONNECTION if timeout
return total;
}
use of java.net.SocketAddress in project jdk8u_jdk by JetBrains.
the class Connect method doTest.
void doTest() {
SctpChannel channel = null;
SctpServerChannel ssc = null;
try {
/* Create a server channel to connect to */
ssc = SctpServerChannel.open().bind(null);
Set<SocketAddress> addrs = ssc.getAllLocalAddresses();
if (addrs.isEmpty())
debug("addrs should not be empty");
final SocketAddress peerAddress = (InetSocketAddress) addrs.iterator().next();
channel = SctpChannel.open();
/* TEST 0.5 Verify default values for new/unconnected channel */
check(channel.getRemoteAddresses().isEmpty(), "non empty set for unconnected channel");
check(channel.association() == null, "non-null association for unconnected channel");
check(!channel.isConnectionPending(), "should not have a connection pending");
/* TEST 1: non-blocking connect */
channel.configureBlocking(false);
if (channel.connect(peerAddress) != true) {
debug("non-blocking connect did not immediately succeed");
check(channel.isConnectionPending(), "should return true for isConnectionPending");
try {
channel.connect(peerAddress);
fail("should have thrown ConnectionPendingException");
} catch (ConnectionPendingException cpe) {
pass();
} catch (IOException ioe) {
unexpected(ioe);
}
channel.configureBlocking(true);
check(channel.finishConnect(), "finishConnect should have returned true");
}
ssc.accept();
ssc.close();
/* TEST 1.5 Verify after connect */
check(!channel.getRemoteAddresses().isEmpty(), "empty set for connected channel");
check(channel.association() != null, "null association for connected channel");
check(!channel.isConnectionPending(), "pending connection for connected channel");
/* TEST 2: Verify AlreadyConnectedException thrown */
try {
channel.connect(peerAddress);
fail("should have thrown AlreadyConnectedException");
} catch (AlreadyConnectedException unused) {
pass();
} catch (IOException ioe) {
unexpected(ioe);
}
/* TEST 2.5: Verify AlreadyConnectedException thrown */
try {
channel.connect(peerAddress, 5, 5);
fail("should have thrown AlreadyConnectedException");
} catch (AlreadyConnectedException unused) {
pass();
} catch (IOException ioe) {
unexpected(ioe);
}
/* TEST 3: UnresolvedAddressException */
channel.close();
channel = SctpChannel.open();
InetSocketAddress unresolved = InetSocketAddress.createUnresolved("xxyyzzabc", 4567);
try {
channel.connect(unresolved);
fail("should have thrown UnresolvedAddressException");
} catch (UnresolvedAddressException unused) {
pass();
} catch (IOException ioe) {
unexpected(ioe);
}
/* TEST 4: UnsupportedAddressTypeException */
SocketAddress unsupported = new UnsupportedSocketAddress();
try {
channel.connect(unsupported);
fail("should have thrown UnsupportedAddressTypeException");
} catch (UnsupportedAddressTypeException unused) {
pass();
} catch (IOException ioe) {
unexpected(ioe);
}
/* TEST 5: ClosedChannelException */
channel.close();
final SctpChannel closedChannel = channel;
testCCE(new Callable<Void>() {
public Void call() throws IOException {
closedChannel.connect(peerAddress);
return null;
}
});
/* TEST 5.5 getRemoteAddresses */
testCCE(new Callable<Void>() {
public Void call() throws IOException {
closedChannel.getRemoteAddresses();
return null;
}
});
testCCE(new Callable<Void>() {
public Void call() throws IOException {
closedChannel.association();
return null;
}
});
check(!channel.isConnectionPending(), "pending connection for closed channel");
/* Run some more finishConnect tests */
/* TEST 6: NoConnectionPendingException */
channel = SctpChannel.open();
try {
channel.finishConnect();
fail("should have thrown NoConnectionPendingException");
} catch (NoConnectionPendingException unused) {
pass();
} catch (IOException ioe) {
unexpected(ioe);
}
/* TEST 7: ClosedChannelException */
channel.close();
final SctpChannel cceChannel = channel;
testCCE(new Callable<Void>() {
public Void call() throws IOException {
cceChannel.finishConnect();
return null;
}
});
/* TEST 8: IOException: Connection refused. Exercises handleSocketError.
* Assumption: no sctp socket listening on 3456 */
SocketAddress addr = new InetSocketAddress("localhost", 3456);
channel = SctpChannel.open();
try {
channel.connect(addr);
fail("should have thrown ConnectException: Connection refused");
} catch (IOException ioe) {
pass();
}
} catch (IOException ioe) {
unexpected(ioe);
} finally {
try {
if (channel != null)
channel.close();
} catch (IOException unused) {
}
try {
if (ssc != null)
ssc.close();
} catch (IOException unused) {
}
}
}
use of java.net.SocketAddress in project jdk8u_jdk by JetBrains.
the class Receive method test.
void test(String[] args) {
SocketAddress address = null;
Server server;
if (!Util.isSCTPSupported()) {
out.println("SCTP protocol is not supported");
out.println("Test cannot be run");
return;
}
if (args.length == 2) {
/* requested to connecct to a specific address */
try {
int port = Integer.valueOf(args[1]);
address = new InetSocketAddress(args[0], port);
} catch (NumberFormatException nfe) {
err.println(nfe);
}
} else {
/* start server on local machine, default */
try {
server = new Server();
server.start();
address = server.address();
debug("Server started and listening on " + address);
} catch (IOException ioe) {
ioe.printStackTrace();
return;
}
}
doTest(address);
}
use of java.net.SocketAddress in project jdk8u_jdk by JetBrains.
the class ReceiveIntoDirect method test.
void test(String[] args) throws IOException {
SocketAddress address = null;
Server server;
if (!Util.isSCTPSupported()) {
out.println("SCTP protocol is not supported");
out.println("Test cannot be run");
return;
}
if (args.length == 2) {
/* requested to connecct to a specific address */
try {
int port = Integer.valueOf(args[1]);
address = new InetSocketAddress(args[0], port);
} catch (NumberFormatException nfe) {
err.println(nfe);
}
} else {
/* start server on local machine, default */
server = new Server();
server.start();
address = server.address();
debug("Server started and listening on " + address);
}
/* many combinations with varing buffer sizes, and offsets */
runWithManyOffsets(address, 20);
runWithManyOffsets(address, 49);
runWithManyOffsets(address, 50);
runWithManyOffsets(address, 51);
runWithManyOffsets(address, 1024);
}
Aggregations