DatagramSocket getLocalAddress - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for DatagramSocket getLocalAddress method.

This code example shows how to use the following methods:getLocalAddress, getLocalPort
 
	public UDPEcho(String host, int port) throws IOException,
			UnknownHostException { 
		this.hostIP = InetAddress.getByName(host);
		this.port = port;
		sock = new DatagramSocket();
		System.out.println("UDP: " + sock.getLocalAddress() + ":"
				+ sock.getLocalPort());
		// sock.connect(hostIP,port); 
	} 
 
	public void send(String s) throws IOException {
		System.out.println("Sending:" + s);
		final DatagramPacket packet = new DatagramPacket(s.getBytes(), s
				.length(), hostIP, port);
		sock.send(packet);
	} 
 
	public void run() { 
		final byte[] buf = new byte[BUF_SIZE];
6
CodeRank
This code example shows how to use the following methods:getLocalAddress, getLocalPort
	public void start()	{ 
 
		StringBuffer name = new StringBuffer("Cyber.SSDPSearchResponseSocket/");
		DatagramSocket s = getDatagramSocket();
		// localAddr is null on Android m3-rc37a (01/30/08) 
		InetAddress localAddr = s.getLocalAddress();
		if (localAddr != null) {
			name.append(s.getLocalAddress()).append(':');
			name.append(s.getLocalPort());
		} 
		deviceSearchResponseThread = new Thread(this,name.toString());
		deviceSearchResponseThread.start();		
	} 
	 
	public void stop() 
	{ 
		deviceSearchResponseThread = null;
	} 
 
	//////////////////////////////////////////////// 
6
CodeRank
This code example shows how to use the following methods:getLocalAddress, getLocalPort, setSoTimeout
 
	/** 
	 * Starts udp relay server. Spawns two threads of execution and returns. 
	 */ 
	public void start() throws IOException { 
		remote_sock.setSoTimeout(iddleTimeout);
		client_sock.setSoTimeout(iddleTimeout);
 
		log.info("Starting UDP relay server on {}:{}", relayIP, relayPort);
		log.info("Remote socket {}:{}", remote_sock.getLocalAddress(),
				remote_sock.getLocalPort());
 
		pipe_thread1 = new Thread(this, "pipe1");
		pipe_thread2 = new Thread(this, "pipe2");
 
		lastReadTime = System.currentTimeMillis();
 
		pipe_thread1.start();
		pipe_thread2.start();
	} 
6
CodeRank
This code example shows how to use the following methods:close, connect, getLocalAddress
    } 
    public static void showWelcomeInfo(int tcpPort1,int tcpPort2){
        boolean usePublicDNS = false;
        try { 
            DatagramSocket udpSocket = null;
            udpSocket = new DatagramSocket();
            udpSocket.connect(InetAddress.getByName("8.8.8.8"), 53); // Using public DNS google
            IP = udpSocket.getLocalAddress();
            udpSocket.close();
            usePublicDNS = true;
        } catch (SocketException e1) {
            System.out.println("cannot get IP address through 8.8.8.8");
        } 
        catch (UnknownHostException e1) {
            System.out.println("cannot get IP address through 8.8.8.8");
        } 
        if(!usePublicDNS){ // no internet connection or goolge 8.8.8.8 down
            try { 
                IP = InetAddress.getLocalHost();
            } catch (UnknownHostException e) {
6
CodeRank
This code example shows how to use the following methods:close, getLocalAddress
        // Test for method java.net.InetAddress 
        // java.net.InetAddress.getLocalHost() 
 
        // We don't know the host name or ip of the machine 
        // running the test, so we can't build our own address 
        DatagramSocket dg = new DatagramSocket(0, InetAddress
                .getLocalHost());
        assertTrue("Incorrect host returned", InetAddress.getLocalHost()
                .equals(dg.getLocalAddress()));
        dg.close();
    } 
 
    /** 
     * @tests java.net.InetAddress#getLocalHost() 
     */ 
    public void test_getLocalHost_extended() throws Exception { 
        // Bogus, but we don't know the host name or ip of the machine 
        // running the test, so we can't build our own address 
        DatagramSocket dg = new DatagramSocket(0, InetAddress.getLocalHost());
        assertEquals("Incorrect host returned", InetAddress.getLocalHost(), dg.getLocalAddress());
5
CodeRank
This code example shows how to use the following methods:getLocalAddress, getLocalPort
      } catch (IOException e) {}
   } 
    
   public InetSocketAddress getInetSocketAddress()
   { 
      return new InetSocketAddress(socket.getLocalAddress(), socket.getLocalPort()) ;
   } 
} 
5
CodeRank
This code example shows how to use the following methods:getLocalAddress, getLocalPort
	public void start()	{ 
 
		StringBuffer name = new StringBuffer("Cyber.SSDPSearchResponseSocket/");
		DatagramSocket s = getDatagramSocket();
		// localAddr is null on Android m3-rc37a (01/30/08) 
		InetAddress localAddr = s.getLocalAddress();
		if (localAddr != null) {
			name.append(s.getLocalAddress()).append(':');
			name.append(s.getLocalPort());
		} 
		deviceSearchResponseThread = new Thread(this,name.toString());
		deviceSearchResponseThread.start();		
	} 
	 
	public void stop() 
	{ 
		deviceSearchResponseThread = null;
	} 
 
	//////////////////////////////////////////////// 
5
CodeRank
This code example shows how to use the following methods:getLocalAddress
     * <p> 
     * @return The local address to which the client's socket is bound. 
     ***/ 
    public InetAddress getLocalAddress()
    { 
        return _socket_.getLocalAddress();
    } 
 
 
    /*** 
     * Sets the DatagramSocketFactory used by the DatagramSocketClient 
     * to open DatagramSockets.  If the factory value is null, then a default 
     * factory is used (only do this to reset the factory after having 
     * previously altered it). 
     * <p> 
     * @param factory  The new DatagramSocketFactory the DatagramSocketClient 
     * should use. 
     ***/ 
    public void setDatagramSocketFactory(DatagramSocketFactory factory)
    { 
5
CodeRank
This code example shows how to use the following methods:getLocalAddress, getLocalPort
		 
	}// run 
	 
	public String toString(){
		String s = "ecouteur reseau : " + super.getName();
		s += "\n\t IP   : " + receveur.getLocalAddress().getHostAddress();
		s += "\n\t port : " + receveur.getLocalPort();
		return  s;
	} 
	 
} 
 
5
CodeRank
This code example shows how to use the following methods:getLocalAddress, getLocalPort
		}// run 
		 
		 
		public String toString(){
			String s = "ecouteur clavier : " + super.getName();
			s += "\n\t IP   : " + envoyeur.getLocalAddress().getHostAddress();
			s += "\n\t port : " + envoyeur.getLocalPort();
			return  s;
		} 
} 
 
5
CodeRank
getLocalAddress Method Overview
Returns the local address to which this socket is bound, or null if this socket is closed.
See Code Examples for other Java 8 DatagramSocket Methods: