Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Advertising server on lan  (Read 3796 times)
0 Members and 1 Guest are viewing this topic.
Offline bobjob

JGO Knight


Medals: 10
Projects: 6


David Aaron Muhar


« Posted 2008-08-25 05:22:49 »

My networking code is fine, but I have to input the servers ip manual for the client. is there some way to detect what servers are currently running on the lan?

I have seen this done in other games, I have tried looking it up in good, but i think my keywords are wrong.

My Projects
Games, Webcam chat, Video screencast, PDF tools.

Javagaming.org with chat room
Offline Eli Delventhal
« League of Dukes »

JGO Kernel


Medals: 39
Projects: 12


Game Engineer


« Reply #1 - Posted 2008-08-25 05:41:17 »

I have never tried this, but I pondered the question myself and came up with this potential answer:

Every IP on a LAN has the same first 3 numbers, only the last number changes. So perhaps they scan every possible last number (256 options) and see which one connect.

See my work:
OTC Software
Offline Wildern

Junior Member





« Reply #2 - Posted 2008-08-25 06:21:09 »

You might want to look into broadcast UDP packets.
Games published by our own members! Check 'em out!
Try the Free Demo of Revenge of the Titans
Offline bobjob

JGO Knight


Medals: 10
Projects: 6


David Aaron Muhar


« Reply #3 - Posted 2008-08-25 07:04:17 »

cool thanx guys.
because the game im working on is TCP ill try the scan idea

My Projects
Games, Webcam chat, Video screencast, PDF tools.

Javagaming.org with chat room
Offline jezek2
« Reply #4 - Posted 2008-08-25 08:22:41 »

I have never tried this, but I pondered the question myself and came up with this potential answer:

Every IP on a LAN has the same first 3 numbers, only the last number changes. So perhaps they scan every possible last number (256 options) and see which one connect.

This is not true, you must look both to IP and netmask. There can be various combinations. Remember LAN is the same as internet, it just (mostly) uses one of three reserved IP ranges (192.168.x.x, 10.x.x.x, 172.16.0.0-172.31.255.255). You should use special IP adress 255.255.255.255 for broadcast (or derived one from IP and netmask, where you replace host bits with all 1s).
Offline bobjob

JGO Knight


Medals: 10
Projects: 6


David Aaron Muhar


« Reply #5 - Posted 2008-08-25 08:27:27 »

would you know any tutorials on the net that explain this in detail?

My Projects
Games, Webcam chat, Video screencast, PDF tools.

Javagaming.org with chat room
Offline bobjob

JGO Knight


Medals: 10
Projects: 6


David Aaron Muhar


« Reply #6 - Posted 2008-08-25 08:43:54 »

I monitored the traffic on wc3 when hosting a server, and yeah its creats another UDP port. So i guess ill read up on multicast sockets. Thanx for all the help

My Projects
Games, Webcam chat, Video screencast, PDF tools.

Javagaming.org with chat room
Offline CaptainJester

JGO Knight


Medals: 10
Projects: 2


Make it work; make it better.


« Reply #7 - Posted 2008-08-25 14:12:49 »

Most comercial games use a registry server.  This is a central server where any game server will register itself when it comes online.  This is where you find your lobbys, chat areas, etc.  All the client has to do then is have the registry server's IP address.

Offline Eli Delventhal
« League of Dukes »

JGO Kernel


Medals: 39
Projects: 12


Game Engineer


« Reply #8 - Posted 2008-08-25 17:49:27 »

Most comercial games use a registry server.  This is a central server where any game server will register itself when it comes online.  This is where you find your lobbys, chat areas, etc.  All the client has to do then is have the registry server's IP address.
But what about clients that can't connect over the internet, and therefore to  this registry server?

See my work:
OTC Software
Offline kevglass
« League of Dukes »

JGO Kernel


Medals: 54
Projects: 20


Mentally unstable, best avoided.


« Reply #9 - Posted 2008-08-25 17:55:19 »

It's two different use cases.

CJ is talking about games that allow internet play, the registry server is used to allow players to find games that have been added to the central registry (counter strike is a good example).

bobjob, I think, is talking about LAN games as opposed to internet based play. Most commercial games that support LAN play also use multicast to allow players to broadcast on a common multicast address and hence find each other on the network.

The multicast approach doesn't work with internet games because most routers are configured to not allow re-broadcast multicast packets. So in most cases the multicast will only reach machines on the same subnet.

Using TCP for game play but using multicast/broadcast UDP for discovery is very common.

Kev

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #10 - Posted 2008-08-25 20:36:57 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
public class NetworkDiscovery
{
   private static final long default_broadcast_interval = 2000;
   private static final String default_broadcast_host = "255.255.255.255";
   private static final int default_broadcast_port = 8008;

   public static final void broadcastService(String serviceName, InetSocketAddress broadcastAboutService) throws IOException
   {
      InetSocketAddress broadcastAddress = new InetSocketAddress(InetAddress.getByName(default_broadcast_host), default_broadcast_port);
      NetworkDiscovery.broadcastService(serviceName, broadcastAddress, broadcastAboutService, default_broadcast_interval, -1);
   }

   public static final void broadcastService(String serviceName, InetSocketAddress broadcastAddress, InetSocketAddress broadcastAboutService, long interval, int times) throws IOException
   {
      InetSocketAddress bindAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0);
      DatagramSocket socket = new DatagramSocket(bindAddress);

      byte[] packetData;
      {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         DataOutputStream dos = new DataOutputStream(baos);
         dos.writeUTF(serviceName);
         dos.writeUTF(broadcastAboutService.getHostName());
         dos.writeInt(broadcastAboutService.getPort());
         dos.flush();
         packetData = baos.toByteArray();
      }

      try
      {
         for (int i = 0; i < times || times == -1; i++)
         {
            socket.send(new DatagramPacket(packetData, 0, packetData.length, broadcastAddress));

            try
            {
               Thread.sleep(interval);
            }
            catch (InterruptedException exc)
            {
               exc.printStackTrace();
            }
         }
      }
      catch (Exception exc)
      {
         exc.printStackTrace();
      }
   }

   /**
    * Listens for UDP packets at the specified <code>port</code>.<br>
    * Only returns when the received packet has the specified <code>name</code>.<br>
    * This way multiple broadcasting sockets per UDP port are supported
    */


   public static final InetSocketAddress discoverService(String serviceName)
   {
      return NetworkDiscovery.discoverService(serviceName, default_broadcast_port, 10, (int)default_broadcast_interval * 4);
   }

   public static final InetSocketAddress discoverService(String serviceName, int port, int maxTries, int socketTimeout)
   {
      try
      {
         int packetLength = 1024;

         DatagramSocket socket = new DatagramSocket(port, InetAddress.getLocalHost());
         DatagramPacket packet = new DatagramPacket(new byte[packetLength], 0, packetLength);

         socket.setSoTimeout(socketTimeout);

         for (int i = 0; i < maxTries; i++)
         {
            try
            {
               socket.receive(packet);
            }
            catch (IOException exc)
            {
               exc.printStackTrace();
               break;
            }

            ByteArrayInputStream bais = new ByteArrayInputStream(packet.getData(), packet.getOffset(), packet.getLength());
            DataInputStream dis = new DataInputStream(bais);

            String theServiceName = dis.readUTF();
            if (!theServiceName.equals(serviceName))
               continue;

            String notifyHost = dis.readUTF();
            int notifyPort = dis.readInt();
            return new InetSocketAddress(notifyHost, notifyPort);
         }
      }
      catch (IOException exc)
      {
         exc.printStackTrace();
      }

      throw new NoSuchServiceException("Service \"" + serviceName + "\" not found");
   }

   public static class NoSuchServiceException extends RuntimeException
   {
      public NoSuchServiceException(String msg)
      {
         super(msg);
      }
   }
}

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline bobjob

JGO Knight


Medals: 10
Projects: 6


David Aaron Muhar


« Reply #11 - Posted 2008-08-26 09:55:26 »

bobjob, I think, is talking about LAN games as opposed to internet based play. Most commercial games that support LAN play also use multicast to allow players to broadcast on a common multicast address and hence find each other on the network.
yeah.

thanx for the example riven, cant wait to go through it.

My Projects
Games, Webcam chat, Video screencast, PDF tools.

Javagaming.org with chat room
Offline bobjob

JGO Knight


Medals: 10
Projects: 6


David Aaron Muhar


« Reply #12 - Posted 2008-08-26 19:43:55 »

i tested the code, and its works really well.

just in case anyone is interested in using the code, I fixed/changed a few things:
broadcast address to "255.255.255.255"
interval to be alot higher, i was comparing it to other comercial game delays, 6000 seems decent enough
new Socket(serviceAddress);  needs to be changed to: new Socket(serviceAddress.getHostName(), serviceAddress.getPort());



thanx again Riven that really saved me alot of time.

My Projects
Games, Webcam chat, Video screencast, PDF tools.

Javagaming.org with chat room
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #13 - Posted 2008-08-26 23:22:41 »

I updated the code a bit for usability.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline noblemaster

JGO Ninja


Medals: 15
Projects: 7


Age of Conquest makes your day!


« Reply #14 - Posted 2008-08-27 00:26:18 »

I updated the code a bit for usability.
Wow, excellent job on the code update!

* gathers Riven's code  Grin *

Offline Mr_Light

Senior Member




shiny.


« Reply #15 - Posted 2008-08-27 03:50:08 »

http://java.sun.com/javase/6/docs/api/java/net/NetworkInterface.html#getInterfaceAddresses()
and
http://java.sun.com/javase/6/docs/api/java/net/InterfaceAddress.html#getBroadcast()
Should get the broadcast adress.  however requires java 6 and up, and 255.255.255.255 should always work. Well as far as ipv4 goes.
For ipv6 use multicast. Perhaps we should code a multicast version to ensure that it works on both ipv4 and 6 networks.


edit. I suppose you could also go for ethernet broadcast but I'm not sure if you can do that from java as it's (good)support for everything lower then ip is absent(ping anyone?).

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #16 - Posted 2008-08-27 11:59:35 »

There is an obscure isReachable() method somewhere that 'might use ping', according to the spec. (sigh)

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline Mr_Light

Senior Member




shiny.


« Reply #17 - Posted 2008-08-27 16:07:37 »

http://java.sun.com/j2se/1.5.0/docs/api/java/net/InetAddress.html#isReachable(int)

Kinda useless if you want to figure out the most responsive server / 'closest' server.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (80 views)
2013-05-17 21:29:12

alaslipknot (89 views)
2013-05-16 21:24:48

gouessej (122 views)
2013-05-16 00:53:38

gouessej (114 views)
2013-05-16 00:17:58

theagentd (126 views)
2013-05-15 15:01:13

theagentd (113 views)
2013-05-15 15:00:54

StreetDoggy (158 views)
2013-05-14 15:56:26

kutucuk (180 views)
2013-05-12 17:10:36

kutucuk (180 views)
2013-05-12 15:36:09

UnluckyDevil (186 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.28 seconds with 21 queries.