/** * Creates the socket and the writer to go with it. */ @Override public DatagramSocket makeObject(SocketAddress socketAddress) throws Exception { DatagramSocket datagramSocket = new DatagramSocket(); datagramSocket.connect(socketAddress); return datagramSocket; }
/** * Creates a new UDP connection that send datagrams to the * specified address and port. */ public UdpConnector( InetAddress remote, int remotePort ) throws IOException { InetSocketAddress localSocketAddress = new InetSocketAddress(0); this.sock = new DatagramSocket( localSocketAddress ); remoteAddress = new InetSocketAddress( remote, remotePort ); // Setup to receive only from the remote address sock.connect( remoteAddress ); connected.set(true); }
/** * Connects this datagram socket to the specific {@code address} and {@code port}. * Future calls to {@link #send} will use this as the default target, and {@link #receive} * will only accept packets from this source. * * <p>Beware: because it can't throw, this method silently ignores failures. * Use {@link #connect(SocketAddress)} instead. */ public void connect(InetAddress address, int port) { if (address == null) { throw new IllegalArgumentException("address == null"); } try { connect(new InetSocketAddress(address, port)); } catch (SocketException connectException) { // TODO: or just use SneakyThrow? There's a clear API bug here. pendingConnectException = connectException; } }
/** * Get the ip address of local host. */ public static String getHostAddress() throws SocketException, UnknownHostException { DatagramSocket ds = new DatagramSocket(); ds.connect(InetAddress.getByName(DUMMY_OUT_IP), 80); InetAddress localAddress = ds.getLocalAddress(); if (localAddress.getHostAddress().equals("0.0.0.0")) { localAddress = InetAddress.getLocalHost(); } return localAddress.getHostAddress(); }
public static String getLocalAddressByDatagram() { try(final DatagramSocket socket = new DatagramSocket()){ socket.connect(InetAddress.getByName("8.8.8.8"), 10002); return socket.getLocalAddress().getHostAddress(); } catch (Exception e) { logger.error("Failed to retrieving ip address.", e); } return null; }
public void connect (Selector selector, InetSocketAddress remoteAddress) throws IOException { close(); readBuffer.clear(); writeBuffer.clear(); try { datagramChannel = selector.provider().openDatagramChannel(); datagramChannel.socket().bind(null); datagramChannel.socket().connect(remoteAddress); datagramChannel.configureBlocking(false); selectionKey = datagramChannel.register(selector, SelectionKey.OP_READ); lastCommunicationTime = System.currentTimeMillis(); connectedAddress = remoteAddress; } catch (IOException ex) { close(); IOException ioEx = new IOException("Unable to connect to: " + remoteAddress); ioEx.initCause(ex); throw ioEx; } }
this.socket.connect(this.address);
// bind to INADDR_ANY, allowing packets on all IP addresses of host: DatagramSocket dsock = new DatagramSocket(55555); DatagramPacket packet = new DatagramPacket(new byte[8192]); //next packet can be from anywhere including local host (127.0.0.0/8) dsock.receive(packet); // restrict communication to a single host/port: dsock.connect(new InetSocketAddress("somehost.net", 99)); // next packet can only be from somehost.net on port 99: dsock.receive(packet);
/** * Check if a port is open with UDP, note that this isn't reliable * as UDP will does not send ACKs * * @param ia - address to scan * @param portNo - port to scan * @param timeoutMillis - timeout * @return - true if port is open, false if not or unknown */ public static boolean scanAddress(InetAddress ia, int portNo, int timeoutMillis) { try { byte[] bytes = new byte[128]; DatagramPacket dp = new DatagramPacket(bytes, bytes.length); DatagramSocket ds = new DatagramSocket(); ds.setSoTimeout(timeoutMillis); ds.connect(ia, portNo); ds.send(dp); ds.isConnected(); ds.receive(dp); ds.close(); } catch (SocketTimeoutException e) { return true; } catch (Exception ignore) { } return false; }
/** {@inheritDoc} */ @Override public void connect(final InetAddress address, final int port, final int timeout) throws IOException { LOG.debug("Address: {}, port: {}, timeout: {}", address, port, timeout); m_socket = new DatagramSocket(); m_socket.setSoTimeout(timeout); m_socket.connect(address, port); }
public static ThriftUdpTransport newThriftUdpClient(String host, int port) { ThriftUdpTransport t; try { t = new ThriftUdpTransport(); t.socket.connect(new InetSocketAddress(host, port)); } catch (SocketException e) { throw new RuntimeException("TUDPTransport cannot connect: ", e); } return t; }
public static ThriftUdpTransport newThriftUdpClient(String host, int port) { ThriftUdpTransport t; try { t = new ThriftUdpTransport(); t.socket.connect(new InetSocketAddress(host, port)); } catch (SocketException e) { throw new RuntimeException("TUDPTransport cannot connect: ", e); } return t; }
public static Optional<InetAddress> localInetAddress() { try (DatagramSocket socket = new DatagramSocket()) { socket.connect(InetAddress.getByName("8.8.8.8"), 10002); return Optional.of(socket.getLocalAddress()); } catch (Exception e) { return Optional.empty(); } } }
public void test() throws UtilityException, SocketException, UnknownHostException, IOException, MessageAttributeParsingException, MessageAttributeException, MessageHeaderParsingException { initialSocket = new DatagramSocket(); initialSocket.connect(InetAddress.getByName(stunServer), port); initialSocket.setSoTimeout(timeout); if (bindingCommunicationInitialSocket()) { return; } BindingLifetimeTask task = new BindingLifetimeTask(); timer.schedule(task, binarySearchLifetime); LOGGER.debug("Timer scheduled initially: " + binarySearchLifetime + "."); }
public void test() throws UtilityException, SocketException, UnknownHostException, IOException, MessageAttributeParsingException, MessageAttributeException, MessageHeaderParsingException { initialSocket = new DatagramSocket(); initialSocket.connect(InetAddress.getByName(stunServer), port); initialSocket.setSoTimeout(timeout); if (bindingCommunicationInitialSocket()) { return; } BindingLifetimeTask task = new BindingLifetimeTask(); timer.schedule(task, binarySearchLifetime); LOGGER.debug("Timer scheduled initially: " + binarySearchLifetime + "."); }
DatagramSocket socket = new DatagramSocket(); socket.setSoTimeout (0); socket.setSendBufferSize (16384); socket.setReceiveBufferSize(16384); socket.connect(address);
@Override public void initialize() throws IOException { socket = new DatagramSocket(); socket.connect(new InetSocketAddress(hostname, port)); socket.setSoTimeout((int) getTimeout()); setStreams(new UdpInputStream(socket), new UdpOutputStream(socket)); }
DatagramChannel datagramChannel = DatagramChannel.open(); DatagramSocket datagramSocket = datagramChannel.socket(); datagramSocket.setReuseAddress(true); datagramSocket.bind(new InetSocketAddress(HOST, PORT)); datagramSocket.connect(address); datagramChannel.configureBlocking(false); datagramChannel.register(selector, SelectionKey.OP_READ);
@Override public void connect(InetAddress address, int port) { if(!MockFramework.isEnabled()){ super.connect(address,port); return; } try { connectInternal(address, port); } catch (SocketException se) { throw new MockError("connect failed", se); } }
@Test(expected = NetworkReactiveAuditException.class) public void connect() throws IOException { ReactiveAudit.off.commit(); try (DatagramSocket socket = new DatagramSocket()) { InetSocketAddress localhost = new InetSocketAddress("localhost", PORT); TestTools.strict.commit(); socket.connect(localhost); } }