Captive Imagination
August 01, 2010, 12:00:28 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Check out the teaser site for the first official game by Captive Imagination: http://www.galaxiesbeyond.com
 
   Home   Help Search Calendar Login Register  
Pages: 1 [2] 3  All   Go Down
  Print  
Author Topic: Listing Servers  (Read 5472 times)
0 Members and 1 Guest are viewing this topic.
nymon
Newbie
*
Offline Offline

Posts: 23


View Profile
« Reply #15 on: September 15, 2008, 02:28:24 PM »

Two years later and we've come right back around to the same point!  Grin
I would like to have this as well (again). So, I'm willing to help you out kine with ideas and such if I can.
Logged
darkfrog
Administrator
Inspired Imagination
*****
Offline Offline

Posts: 2650


View Profile
« Reply #16 on: September 15, 2008, 04:42:51 PM »

I'm happy to help with concepts and such, but code-writing time is strained for me. Smiley
Logged
kine
Jr. Member
**
Offline Offline

Posts: 72


View Profile
« Reply #17 on: September 16, 2008, 11:24:57 AM »

Hi :-)

here is a sample code to scan ips and opened ports
when I'll have time I'll make a multithreaded more efficient scan and why not even contribute to JGN :-)
it takes approx 3 seconds to scan 255 ips ( max delay 10ms)

Code:
package networkingTemp;

import java.io.IOException;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class KineIpScanner {

private InetAddress serverReliable, serverFast, clientReliable, clientFast;

KineIpScanner() {

for (int i = 0; i < 256; i++) {

try {
serverReliable = InetAddress.getByName("192.168.1." + i);
boolean f = serverReliable.isReachable(10);
if (f) {
System.out.println(serverReliable.getHostName());
JOptionPane.showMessageDialog(new JFrame(), "found "
+ serverReliable.getHostName() + "at address :"
+ serverReliable.getHostAddress() + "",
"Warning !!!", 1);

PortScan();
}
} catch (IOException e) {
System.out.println("Client initialization failed");
}
}

System.out.println("finished");
}

private void PortScan() {

for (int i = 5000; i < 10000; i++) {
try {

Socket s = new Socket(serverReliable, i);
JOptionPane.showMessageDialog(new JFrame(),
"Socket Created on " + serverReliable.getHostName()
+ " at port " + i + "", "Warning !!!", 1);
} catch (UnknownHostException unhe) {
JOptionPane.showMessageDialog(new JFrame(), "Problem Finding "
+ serverReliable.getHostName() + "", "Warning !!!", 1);
} catch (ConnectException ce) {

} catch (IOException ie) {
JOptionPane.showMessageDialog(new JFrame(),
"IOException Occured\n" + ie + "", "Warning !!!", 1);
}
}
}

public static void main(String[] args) {
KineIpScanner app = new KineIpScanner();

}

Logged
darkfrog
Administrator
Inspired Imagination
*****
Offline Offline

Posts: 2650


View Profile
« Reply #18 on: September 16, 2008, 12:51:42 PM »

You could use JGN's "ping" functionality to try to ping the servers and since JGN is entirely asynchronous anyway you'd just have to wait for results.
Logged
nymon
Newbie
*
Offline Offline

Posts: 23


View Profile
« Reply #19 on: September 16, 2008, 01:10:10 PM »

I think instead of a port scan (which may be considered a little hostile) you should just specify a particular port you are looking for. In most cases you will only ever be looking for one specific port; is this right Darkfrog?

Good start though, father than I got.
Logged
darkfrog
Administrator
Inspired Imagination
*****
Offline Offline

Posts: 2650


View Profile
« Reply #20 on: September 16, 2008, 05:44:38 PM »

That's correct....in fact, that's what pinging does is hit a specific port.
Logged
kine
Jr. Member
**
Offline Offline

Posts: 72


View Profile
« Reply #21 on: September 17, 2008, 06:28:39 AM »

Hi !  Tongue

Sorry for this stupid question, but I can't find a correct way to use ping ...

Here is how I can manage to do it:
boolean found = (new TCPMessageServer(new InetSocketAddress(foundIP, bolidzport, 1000)) != null); for example, but I don't know howto use JGN ping method ...

Can you show me the way to use JGN's ping method  DarkFrog ? Smiley

Kine  Cheesy
      
Logged
darkfrog
Administrator
Inspired Imagination
*****
Offline Offline

Posts: 2650


View Profile
« Reply #22 on: September 17, 2008, 07:28:01 AM »

Ugh, I just realized that you have to have an existing connection....we should write something that allows you to check to see if the port is open before anything else.
Logged
kine
Jr. Member
**
Offline Offline

Posts: 72


View Profile
« Reply #23 on: September 19, 2008, 05:32:02 AM »

I think I'll wait a bit before sending my code ... but I'll do so in a month or two...

         I'm not experienced enough with JGN & java . For the moment I prefer to focus on my game and make a "KineDirtyJava  Kiss" IP scan+opened ports scan + JGN connection scan, and I'll clean it later when I'll be aware of all these multithreading and exception stuffs.

See you :-)

Kine
Logged
kine
Jr. Member
**
Offline Offline

Posts: 72


View Profile
« Reply #24 on: September 19, 2008, 09:19:46 AM »

I guess to use a multi threaded JGN server scan we'd have to use several clients ?
Logged
darkfrog
Administrator
Inspired Imagination
*****
Offline Offline

Posts: 2650


View Profile
« Reply #25 on: September 19, 2008, 10:44:12 AM »

Yeah, you'd probably want to have your local MessageServer attempt to connect to a specified port on each address in that IP range and store the MessageClients you get back to see which ones end up connecting and which ones fail.  The port you attempt to connect to could be specifically for game information so you can get real-time data about that game and then disconnect when you're done.
Logged
kine
Jr. Member
**
Offline Offline

Posts: 72


View Profile
« Reply #26 on: September 20, 2008, 10:28:53 AM »

Well, here is what I've done ...

I'm not very good at coding thread, exceptions  Sad ( 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  Wink

Kine

Code:
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);

}

Code:
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) {
}
}

Code:
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;
}


Code:

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;

}
}
Logged
kine
Jr. Member
**
Offline Offline

Posts: 72


View Profile
« Reply #27 on: September 20, 2008, 10:33:59 AM »

oups it don't manage multi network interfaces ... anyway I guess it's not a big problem ...  Smiley
Logged
darkfrog
Administrator
Inspired Imagination
*****
Offline Offline

Posts: 2650


View Profile
« Reply #28 on: September 20, 2008, 12:50:26 PM »

Only took a brief read, but it looks good.  However, you probably want to have all the TCPMessageServers under a single thread rather than a thread for each one.  Threads are incredibly expensive to create and since JGN is entirely asynchronous you shouldn't have any problems having it all in a single thread.
Logged
Trussell
Jr. Member
**
Offline Offline

Posts: 52


View Profile
« Reply #29 on: June 22, 2009, 08:02:36 PM »

This doesn't work for me... does anybody know why? I am running my game on one computer and trying to see JGN servers with the LanScan code from my laptop, but it's not working. I see 0 servers. Any clues?
Logged
Pages: 1 [2] 3  All   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.277 seconds with 20 queries.