Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (491)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
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 4782 times)
0 Members and 1 Guest are viewing this topic.
Offline bobjob

JGO Knight


Medals: 10
Projects: 4


David Aaron Muhar


« Posted 2008-08-25 03: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

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #1 - Posted 2008-08-25 03: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 04:21:09 »

You might want to look into broadcast UDP packets.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline bobjob

JGO Knight


Medals: 10
Projects: 4


David Aaron Muhar


« Reply #3 - Posted 2008-08-25 05: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 06: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: 4


David Aaron Muhar


« Reply #5 - Posted 2008-08-25 06: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: 4


David Aaron Muhar


« Reply #6 - Posted 2008-08-25 06: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: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #7 - Posted 2008-08-25 12: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

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #8 - Posted 2008-08-25 15: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

JGO Kernel


Medals: 159
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #9 - Posted 2008-08-25 15: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: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2008-08-25 18: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
Offline bobjob

JGO Knight


Medals: 10
Projects: 4


David Aaron Muhar


« Reply #11 - Posted 2008-08-26 07: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: 4


David Aaron Muhar


« Reply #12 - Posted 2008-08-26 17: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: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #13 - Posted 2008-08-26 21: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
Offline noblemaster

JGO Ninja


Medals: 20
Projects: 10


Age of Conquest makes your day!


« Reply #14 - Posted 2008-08-26 22: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 01: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: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #16 - Posted 2008-08-27 09: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
Offline Mr_Light

Senior Member




shiny.


« Reply #17 - Posted 2008-08-27 14: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.

 

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

The first screenshot will be displayed as a thumbnail.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (29 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (38 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (25 views)
2014-08-16 06:20:21

Tekkerue (34 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (48 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!