Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The most appropriate way to do it, if they are in the same LAN is:</p> <ul> <li>Client sends a UDP broadcast to a specific port and matching the network class (A,B,C)</li> <li>Server is listening on this port, receives the broadcast packet and connect or send his IP to the client.</li> </ul> <p>With just two network packets you know the IP address.</p> <p>--EDITED--</p> <p>To broadcast:</p> <pre><code>InetAddress broadcastAddr = SharedFunctions.getNetworkLocalBroadcastAddressdAsInetAddress(); DatagramSocket socket = null; try { socket = new DatagramSocket(); socket.setBroadcast(true); System.arraycopy(BROADCAST_SIGNATURE, 0, buffSend, 0, BROADCAST_SIGNATURE.length); DatagramPacket packet = new DatagramPacket(buffSend, buffSend.length, broadcastAddr, BROADCAST_PORT); socket.send(packet); } catch (Exception e) { e.printStackTrace(); if(socket != null) try {socket.close();} catch (Exception e1) {} } public static InetAddress getNetworkLocalBroadcastAddressdAsInetAddress() throws IOException { for (Enumeration&lt;NetworkInterface&gt; en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); if(VERSION.SDK_INT &lt; 9) { if(!intf.getInetAddresses().nextElement().isLoopbackAddress()){ byte[] quads = intf.getInetAddresses().nextElement().getAddress(); quads[0] = (byte)255; return InetAddress.getByAddress(quads); } }else{ if(!intf.isLoopback()){ List&lt;InterfaceAddress&gt; intfaddrs = intf.getInterfaceAddresses(); return intfaddrs.get(0).getBroadcast(); //return first IP address } } } return null; } </code></pre> <p>To receice broadcast:</p> <pre><code> try { socketReceiver = new DatagramSocket(BROADCAST_PORT); socketReceiver.setBroadcast(true); DatagramPacket packet = new DatagramPacket(buffRecv, buffRecv.length); while(Thread.currentThread() == cThreadReceiver){ socketReceiver.receive(packet); //here you receive the packet and can check the sender IP address } } catch (Exception e) { e.printStackTrace(); if(socketReceiver != null) try {socketReceiver.close();} catch (Exception e1) {} } </code></pre> <p>You will need to do some editing but should start you in the right track.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload