Well, here is what I've done ...
I'm not very good at coding thread, exceptions

( I guess I'll get better btw feel free to give me advices ) etc so correct & don't bother, anyway it works quite safely, and under linux also. It's multithreaded. ( I included LinuxInetAdress again() )
nevertheless, I Hope this helps

Kine
package menu.networkingUtilities;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.HashSet;
import java.util.Set;
import networking.LinuxInetAddress;
import com.captiveimagination.jgn.JGN;
import com.captiveimagination.jgn.MessageServer;
import com.captiveimagination.jgn.TCPMessageServer;
public class LanScan {
int actualTempPort = 30000;
private boolean wait = true;
static MessageServer server;
Set<Integer> ports;
Set<SpecifiedIPScan> portScans = new HashSet<SpecifiedIPScan>();
final Set<InetSocketAddress> JGNServers = new HashSet<InetSocketAddress>();
LanScan(String subNetMask, int delay, Set<Integer> ports) {
// Set<InetAddress> IPAdresses = new HashSet<InetAddress>();
// try {
// IPAdresses.add(InetAddress.getByName("192.168.1.201"));
// } catch (IOException e) {
// System.out.println("Client initialization failed");
// }
this.ports = ports;
Set<InetAddress> IPAdresses = findIPs(subNetMask, delay);
}
private Set<InetAddress> findIPs(String subNetMask, int delay) {
InetAddress scanIPAddress;
System.out.println("Start scanning for lan computers ...");
Set<InetAddress> IPAddresses = new HashSet<InetAddress>();
for (int i = 0; i < 256; i++) {
try {
scanIPAddress = InetAddress.getByName(subNetMask + i);
boolean f = scanIPAddress.isReachable(delay);
if (f) {
System.out.println("found a computer on the lan !! : "
+ scanIPAddress.getHostName() + " ("
+ scanIPAddress.getHostAddress() + ")");
IPAddresses.add(scanIPAddress);
SpecifiedIPScan portScan = new SpecifiedIPScan(scanIPAddress, ports,
JGNServers);
portScans.add(portScan);
}
} catch (IOException e) {
System.out
.println("problem retreiving IP lan adresses, exiting");
System.exit(0);
}
}
while (wait) {
int threadFinished = 0;
try {
for (SpecifiedIPScan portScan : portScans) {
for (Thread thread : portScan.threads) {
if (thread.isAlive()) {
threadFinished++;
}
}
}
if (threadFinished == 0)
wait = false;
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
}
}
System.out.println("finished !!!!!!!!");
System.out.println("So ...........; there is " + JGNServers.size()
+ " JGN servers ");
for (InetSocketAddress inetAddress : JGNServers) {
System.out.println("---->JGN Server : " + inetAddress.getHostName()
+ "(" + inetAddress.getAddress() + "):"
+ inetAddress.getPort());
}
try {
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return IPAddresses;
}
private static void createAServer() {
try {
server = new TCPMessageServer(new InetSocketAddress(InetAddress
.getByName(LinuxInetAddress.getLocalHost()), 15000));
NetworkFindingMessageServer tms = new NetworkFindingMessageServer(1);
// We add it to this server as a message listener
server.addMessageListener(tms);
// We add it to this server as a connection listener
server.addConnectionListener(tms);
// If you have an update thread of your own this is not necessary as
// you can simply
// call server.update(), but this is a convenience method for
// multithreading.
JGN.createThread(server).start();
System.out.println("debugging server is alive ?! : "
+ server.isAlive());
} catch (IOException e) {
System.out.println("debugging server is alive ?! : no");
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
System.out.println("debugging server is alive ?! : no");
}
}
public static void main(String[] args) {
Set<Integer> ports = new HashSet<Integer>();
//////////sample debugging ports ....
ports.add(15000);
ports.add(10148);
ports.add(20148);
String tempSubNetwork = "";
try {
tempSubNetwork = LinuxInetAddress.getLocalHost();
} catch (UnknownHostException e) {
}
int i = tempSubNetwork.lastIndexOf(".");
String subNetwork = tempSubNetwork.substring(0, i + 1);
System.out.print("Subnetwork : " + subNetwork + " ports tested : ");
for (int port : ports) {
System.out.print(port + ",");
}
createAServer();// <------- create a temporary server for debugging;
LanScan app = new LanScan(subNetwork,
50, ports);
}
package menu.networkingUtilities;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;
import com.captiveimagination.jgn.JGN;
import com.captiveimagination.jgn.MessageClient;
import com.captiveimagination.jgn.MessageServer;
import com.captiveimagination.jgn.TCPMessageServer;
import com.captiveimagination.jgn.event.ConnectionListener;
import com.captiveimagination.jgn.event.MessageListener;
import com.captiveimagination.jgn.message.Message;
import com.captiveimagination.jgn.ping.Ping;
import com.captiveimagination.jgn.test.basic.BasicMessage;
import com.captiveimagination.jgn.test.basic.TestMessageServer;
/**
* @author Matthew D. Hicks
*/
public class NetworkFindingMessageServer implements MessageListener, ConnectionListener {
private int id;
public NetworkFindingMessageServer(int id) {
this.id = id;
}
public void messageCertified(Message message) {
}
public void messageFailed(Message message) {
}
public void messageReceived(Message message) {
}
public void messageSent(Message message) {
}
public void connected(MessageClient client) {
// System.out.println("Connected(" + id + "): " + ((InetSocketAddress)client.getAddress()).getPort());
}
public void disconnected(MessageClient client) {
// System.out.println("Disconnected(" + id + "): " + ((InetSocketAddress)client.getAddress()).getPort());
}
public void negotiationComplete(MessageClient client) {
}
public void kicked(MessageClient client, String reason) {
}
}
package menu.networkingUtilities;
import java.io.IOException;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Set;
import javax.net.ServerSocketFactory;
import networking.LinuxInetAddress;
import com.captiveimagination.jgn.JGN;
import com.captiveimagination.jgn.MessageClient;
import com.captiveimagination.jgn.MessageServer;
import com.captiveimagination.jgn.TCPMessageServer;
public class SpecifiedIPScan {
int tempPort;
boolean connected = false;
ArrayList<Thread> threads = new ArrayList<Thread>();
SpecifiedIPScan(final InetAddress inetAddress, Set<Integer> ports,
final Set<InetSocketAddress> JGNServers) {
for (final int port : ports) {
Thread portScanThread = new Thread(new Runnable() {
public void run() {
SpecifiedIPScan portScan = new SpecifiedIPScan(new InetSocketAddress(
inetAddress, port), JGNServers, getAPort());
}
});
portScanThread.start();
threads.add(portScanThread);
}
}
private int getAPort() {
int numberOfPortsTried = 0;
boolean gotAPort = false;
int actualTempPort = 30000;
Random generator;
generator = new Random();
while (!gotAPort && (numberOfPortsTried < 100)) {
numberOfPortsTried++;
actualTempPort = 30000 + generator.nextInt(30000);
try {
ServerSocket s = ServerSocketFactory.getDefault()
.createServerSocket(actualTempPort);
s.close();
gotAPort = true;
return actualTempPort;
} catch (Throwable e1) {
}
}
System.out
.println("tried more than 99 ports to create a temporary client for testing. problem ... exiting ");
System.exit(0);
return 0;
}
SpecifiedIPScan(InetSocketAddress inetSocketAddress,
Set<InetSocketAddress> JGNServers, int tempPort) {
this.tempPort = tempPort;
if (checkIfThePortIsOpened(inetSocketAddress)) {
checkJGNServer(inetSocketAddress);
if (connected) {
System.out.println("JGN server found at :"
+ inetSocketAddress.getAddress() + " port:"
+ inetSocketAddress.getPort());
JGNServers.add(inetSocketAddress);
}
}
}
private boolean checkIfThePortIsOpened(InetSocketAddress inetSocketAddress) {
try {
// Socket s = new Socket(inetSocketAddress.getAddress(),
// inetSocketAddress.getPort());
Socket s = new Socket(InetAddress.getByName(inetSocketAddress
.getHostName()), inetSocketAddress.getPort());
System.out.println("port " + inetSocketAddress.getPort() + " on "
+ inetSocketAddress.getAddress().getHostName()
+ " open !!!");
/*
* JOptionPane.showMessageDialog(new JFrame(), "Socket Created on "
* + inetSocketAddress.getAddress().getHostName() + " at port " +
* inetSocketAddress.getPort() + "", "Warning !!!", 1);
*/
s.close();
return true;
} catch (UnknownHostException unhe) {
System.out.println("problem scanning "
+ inetSocketAddress.getAddress() + ":"
+ inetSocketAddress.getPort() + ", port skipped");
return false;
} catch (ConnectException ce) {
System.out.println("problem scanning "
+ inetSocketAddress.getAddress() + ":"
+ inetSocketAddress.getPort() + ", port skipped");
return false;
} catch (IOException ie) {
System.out.println("problem scanning "
+ inetSocketAddress.getAddress() + ":"
+ inetSocketAddress.getPort() + ", port skipped");
return false;
}
}
private void checkJGNServer(InetSocketAddress inetSocketAddress) {
boolean ok = false;
try {
MessageServer server2 = new TCPMessageServer(new InetSocketAddress(
InetAddress.getByName(LinuxInetAddress.getLocalHost()),
tempPort));
NetworkFindingMessageServer tms2 = new NetworkFindingMessageServer(
2);
server2.addMessageListener(tms2);
server2.addConnectionListener(tms2);
JGN.createThread(server2).start();
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
}
MessageClient client = server2
.connect(new InetSocketAddress(inetSocketAddress
.getAddress(), inetSocketAddress.getPort()));
connected = true;
while (!client.isConnected()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
client.disconnect();
while (client.isConnected()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
server2.close();
while (server2.isAlive()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
} catch (IOException ie) {
System.out.println("problem connecting a JGN server to "
+ inetSocketAddress.getAddress() + ":"
+ inetSocketAddress.getPort() + ", port skipped");
}
}
public ArrayList<Thread> getThreads() {
return threads;
}
public void setThreads(ArrayList<Thread> threads) {
this.threads = threads;
}
package networking;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
public class LinuxInetAddress {
/**
* Returns an InetAddress representing the address of the localhost. Every
* attempt is made to find an address for this host that is not the loopback
* address. If no other address can be found, the loopback will be returned.
*
* @return InetAddress - the address of localhost
* @throws UnknownHostException
* - if there is a problem determing the address
*/
public static String getLocalHost() throws UnknownHostException {
InetAddress localHost = InetAddress.getLocalHost();
if (!localHost.isLoopbackAddress()) {
return localHost.getHostAddress();
}
InetAddress[] addrs = getAllLocalUsingNetworkInterface();
for (int i = 0; i < addrs.length; i++) {
if (!addrs[i].isLoopbackAddress())
if (!addrs[i].isAnyLocalAddress())
if (!addrs[i].isLinkLocalAddress())
return addrs[i].getHostAddress();
}
return localHost.getHostAddress();
}
/**
* This method attempts to find all InetAddresses for this machine in a
* conventional way (via InetAddress). If only one address is found and it
* is the loopback, an attempt is made to determine the addresses for this
* machine using NetworkInterface.
*
* @return InetAddress[] - all addresses assigned to the local machine
* @throws UnknownHostException
* - if there is a problem determining addresses
*/
public static InetAddress[] getAllLocal() throws UnknownHostException {
InetAddress[] iAddresses = InetAddress.getAllByName("127.0.0.1");
if (iAddresses.length != 1) {
return iAddresses;
}
if (!iAddresses[0].isLoopbackAddress()) {
return iAddresses;
}
return getAllLocalUsingNetworkInterface();
}
/**
* Utility method that delegates to the methods of NetworkInterface to
* determine addresses for this machine.
*
* @return InetAddress[] - all addresses found from the NetworkInterfaces
* @throws UnknownHostException
* - if there is a problem determining addresses
*/
private static InetAddress[] getAllLocalUsingNetworkInterface()
throws UnknownHostException {
ArrayList addresses = new ArrayList();
Enumeration e = null;
try {
e = NetworkInterface.getNetworkInterfaces();
} catch (SocketException ex) {
throw new UnknownHostException("127.0.0.1");
}
while (e.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) e.nextElement();
for (Enumeration e2 = ni.getInetAddresses(); e2.hasMoreElements();) {
addresses.add(e2.nextElement());
}
}
InetAddress[] iAddresses = new InetAddress[addresses.size()];
for (int i = 0; i < iAddresses.length; i++) {
iAddresses[i] = (InetAddress) addresses.get(i);
// System.out.println(iAddresses[i].getHostAddress());
// System.out.println(iAddresses[i].getHostName());
}
return iAddresses;
}
}