/** * 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; }
/** * Returns the {@code SocketAddress} this socket is bound to, or null for an unbound socket. */ public SocketAddress getLocalSocketAddress() { if (!isBound()) { return null; } return new InetSocketAddress(getLocalAddress(), getLocalPort()); }
MessageHandler(InetAddress bind_interface) throws Exception { mcast_sock = new MulticastSocket(mcast_port); mcast_sock.setInterface(bind_interface); mcast_sock.setTimeToLive(ttl); mcast_sock.joinGroup(mcast_addr); sock = new DatagramSocket(0, bind_interface); local_addr = sock.getLocalAddress(); local_port = sock.getLocalPort(); }
public ImageTransmitter(DatagramSocket socket, InetAddress remoteHost, int remotePort, Rectangle area) { try { robot = new Robot(); maxI = (int) Math.ceil(area.getWidth() / tileWidth); maxJ = (int) Math.ceil(area.getHeight() / tileWidth); tiles = new int[maxI][maxJ][tileWidth * tileWidth]; this.area = area; this.socket = socket; localHost = socket.getLocalAddress(); localPort = socket.getLocalPort(); this.remoteHost = remoteHost; this.remotePort = remotePort; this.encoder = new DefaultEncoder(); transmit = true; } catch (AWTException e) { LOGGER.log(Level.WARNING, "exception", e); } }
@Override public void run() { try { //System.out.println("Listening for ECHO: " + socket.getLocalAddress().getHostAddress() + ":" + socket.getLocalPort()); while (true) { DatagramPacket packet = new DatagramPacket(new byte[8], 8); socket.receive(packet); System.out.println("ECHO Packet Received in: " + socket.getLocalAddress().getHostAddress() + ":" + socket.getLocalPort() + " From: " + packet.getAddress().getHostAddress() + ":" + packet.getPort()); for (DatagramListener listener : listeners) { listener.datagramReceived(packet); } packet.setAddress(packet.getAddress()); packet.setPort(packet.getPort()); if (!Arrays.equals(packet.getData(), password)) for (int i = 0; i < 3; i++) socket.send(packet); } } catch (IOException ioe) { if (enabled) { } } }
localHost = socket.getLocalAddress(); this.remoteHost = remoteHost; this.remotePort = remotePort;
@Override public DatagramChannelConfig setBroadcast(boolean broadcast) { try { // See: https://github.com/netty/netty/issues/576 if (broadcast && !javaSocket.getLocalAddress().isAnyLocalAddress() && !PlatformDependent.isWindows() && !PlatformDependent.maybeSuperUser()) { // Warn a user about the fact that a non-root user can't receive a // broadcast packet on *nix if the socket is bound on non-wildcard address. logger.warn( "A non-root user can't receive a broadcast packet if the socket " + "is not bound to a wildcard address; setting the SO_BROADCAST flag " + "anyway as requested on the socket which is bound to " + javaSocket.getLocalSocketAddress() + '.'); } javaSocket.setBroadcast(broadcast); } catch (SocketException e) { throw new ChannelException(e); } return this; }
@Override public void run() { try { LOGGER.fine("Listening for ECHO: " + socket.getLocalAddress().getHostAddress() + ":" + socket.getLocalPort()); while (true) {
@Override public DatagramChannelConfig setBroadcast(boolean broadcast) { try { // See: https://github.com/netty/netty/issues/576 if (broadcast && !javaSocket.getLocalAddress().isAnyLocalAddress() && !PlatformDependent.isWindows() && !PlatformDependent.maybeSuperUser()) { // Warn a user about the fact that a non-root user can't receive a // broadcast packet on *nix if the socket is bound on non-wildcard address. logger.warn( "A non-root user can't receive a broadcast packet if the socket " + "is not bound to a wildcard address; setting the SO_BROADCAST flag " + "anyway as requested on the socket which is bound to " + javaSocket.getLocalSocketAddress() + '.'); } javaSocket.setBroadcast(broadcast); } catch (SocketException e) { throw new ChannelException(e); } return this; }
@Override public DatagramChannelConfig setBroadcast(boolean broadcast) { try { // See: https://github.com/netty/netty/issues/576 if (broadcast && !javaSocket.getLocalAddress().isAnyLocalAddress() && !PlatformDependent.isWindows() && !PlatformDependent.maybeSuperUser()) { // Warn a user about the fact that a non-root user can't receive a // broadcast packet on *nix if the socket is bound on non-wildcard address. logger.warn( "A non-root user can't receive a broadcast packet if the socket " + "is not bound to a wildcard address; setting the SO_BROADCAST flag " + "anyway as requested on the socket which is bound to " + javaSocket.getLocalSocketAddress() + '.'); } javaSocket.setBroadcast(broadcast); } catch (SocketException e) { throw new ChannelException(e); } return this; }
/*** * Returns the local address to which the client's socket is bound. * If you call this method when the client socket is not open, a * NullPointerException is thrown. * * @return The local address to which the client's socket is bound. ***/ public InetAddress getLocalAddress() { return _socket_.getLocalAddress(); }
public String getLocalAddress() { if (0 < localAddr.length()) return localAddr; // I2P prevent NPE #1681 if (ssdpUniSock == null) return ""; return ssdpUniSock.getLocalAddress().getHostAddress(); }
public boolean post(String addr, int port, String msg) { try { InetAddress inetAddr = InetAddress.getByName(addr); DatagramPacket dgmPacket = new DatagramPacket(msg.getBytes(), msg.length(), inetAddr, port); ssdpUniSock.send(dgmPacket); } catch (Exception e) { Debug.warning("addr = " +ssdpUniSock.getLocalAddress().getHostName()); Debug.warning("port = " + ssdpUniSock.getLocalPort()); Debug.warning(e); return false; } return true; }
public UdpProgressParser(ProgressListener listener, int port, InetAddress addr) throws SocketException, URISyntaxException { super(listener); this.socket = new DatagramSocket(port, checkNotNull(addr)); this.address = createUri("udp", socket.getLocalAddress(), socket.getLocalPort()); this.socket.setBroadcast(false); // this.socket.setSoTimeout(); // TODO Setup timeouts }
public synchronized void printInfo() { System.out.println("#########################################"); System.out.println("Peer " + serverSocket.getLocalAddress() + ":" + localPort); System.out.println("UUID: " + getUuid()); System.out.println("Time offset: " + timeOffset); System.out.println(received + " messages received, " + sent + " messages sent"); System.out.println(peers.size() + " peers"); for (final PeerInfo info : peers.values()) { System.out.println(" " + info); } for (final RepositoryObject obj : repository.getObjects()) { System.out.println(" ##### " + obj.getUuid()); System.out.println(" " + obj.getType() + "(" + obj.getUserId() + "): " + obj.getProperties(pte.next())); ((DefaultRepositoryObject)obj).printHistory(); } System.out.println(outputQueue); System.out.flush(); }
/** * {@inheritDoc} */ @Override public MediaStreamTarget getStreamTarget(Channel channel) { // We'll just hack in the local address so that we have something // non-null. It should never be used anyway. DatagramSocket socket = octoRelay.getSocket(); InetAddress inetAddress = socket.getLocalAddress(); int port = socket.getLocalPort(); return new MediaStreamTarget(inetAddress, port, inetAddress, port); }
candidate.setGeneration(generation); candidate.setID(rtpCandidateID); candidate.setIP(socket.getLocalAddress().getHostAddress()); candidate.setPort(socket.getLocalPort()); candidate.setType(CandidateType.host); candidate.setGeneration(generation); candidate.setID(rtcpCandidateID); candidate.setIP(socket.getLocalAddress().getHostAddress()); candidate.setPort(socket.getLocalPort()); candidate.setType(CandidateType.host);
LOG.debug("create session"); UdpSessionConfig config = getSessionConfig(); SocketAddress localAddress = new InetSocketAddress(datagramChannel.socket().getLocalAddress(), datagramChannel .socket().getLocalPort()); final NioUdpSession session = new NioUdpSession(this, idleChecker, datagramChannel, localAddress, remoteAddress);
/** * Returns the {@code SocketAddress} this socket is bound to, or null for an unbound socket. */ public SocketAddress getLocalSocketAddress() { if (!isBound()) { return null; } return new InetSocketAddress(getLocalAddress(), getLocalPort()); }