Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
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  
  Question about online multiplayer games ?  (Read 4526 times)
0 Members and 1 Guest are viewing this topic.
Offline jaypaul

Senior Newbie




Game Programmer


« Posted 2009-07-08 10:07:07 »

Hi Friends,

i got very good answers of my previous queries and now i am looking for the answer of multiplayer game.

I know how to make simple single player java applets but how do I make online multiplayer games?

Is there a specific API or something? Where can I get some tutorials?

Your answers will be helpful

Thanks
Paul
Offline ddyer

Junior Member


Medals: 2



« Reply #1 - Posted 2009-07-08 19:02:30 »

Generally speaking, the missing element is communication.
Usually either TCP or UDP.  Usually either peer-to-peer or
using a central server to mediate communication among the
players.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 12


Game Engineer


« Reply #2 - Posted 2009-07-08 19:21:03 »

Take a look at java.net.Socket. Sockets are very easy to use and will do the trick. You basically just send information using InputStreams and OutputStreams.

See my work:
OTC Software
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Corvinex

Senior Member


Projects: 1


Work Hard, Go Pro.


« Reply #3 - Posted 2009-07-08 19:36:14 »

I just put up a basic tutorial couple weeks ago, has code and everything. Its not the most in depth, but it does have a simple structure to start out with.

Multiplayer Using UDP and Applets

"Do what you love and you'll never work a day in your life."
Android: Pac-Ball
Web Applet: Virus Effect
Old Games
Offline Swattkidd7

Junior Member





« Reply #4 - Posted 2009-07-16 07:28:47 »

Wow man thanks a lot for that resource, really rare to find something as detailed as that, and something I have really been looking for! Although I have a quick question, what would be the best way to disconnect a user, I have went through the code many many times and tried to understand it and for the most part have, but I am not sure how I would disconnect a user (where I would close that users socket).

Also in your sendPacket method, you are sending the packet back to the user that it recieved the packet from correct? I was a bit confused because the way I was doing it before I would have a list of connections and anytime I got a message I would send it to everyone (basic chat structure) and if the server failed to send a message to the user it would close its socket.

I would really appreciate the help.

Thanks, and also VirusEffect is awesome haha
Offline TimB

Junior Member


Medals: 1
Projects: 1



« Reply #5 - Posted 2009-07-16 11:07:58 »

Depending on just how multi-player you want it check out Project Darkstar from Sun as well.

http://www.projectdarkstar.com/
Offline Corvinex

Senior Member


Projects: 1


Work Hard, Go Pro.


« Reply #6 - Posted 2009-07-17 03:42:59 »

Hey no problem,
Although I have a quick question, what would be the best way to disconnect a user

There are a couple reasons why you would want to remove a player from the game. One is when the player times out, meaning they stop sending messages. The best way to handle this would be to have a timer for every player, lets call it totalTime. so in the advance() method of the players you add the elapsedTime to that timer variable. Then you check if the totalTime is greator than a set number, lets say 2 seconds.

1  
2  
3  
4  
5  
6  
public void stepTimeout(float eT)
{
   timeOut += eT;
   if(timeOut>kickTime)
      kick = true;
}


Next you would need to change the PlayerList advance method to the code below, to remove a player when they need to be kicked.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public void advance(float elapsedTime)
{
   for(int x=list.size()-1;x>=0;x--)
   {
      list.get(x).advance(elapsedTime);
      if(list.get(x).getKick())
      {
         System.out.println(list.get(x).getName()+" Timed Out");
         list.remove(x);
      }
   }
}

If the server receives a message from the client, then set totalTime = 0;
I have updated the tutorial to incorporate this timeout feature.

Now to "disconnect" someone all the server needs to do is not send the client you want disconnected any messages. This will then make the client think the server has timed out, and you can handle it on the client side any way you'd like. If not receiving messages quit.

Also in your sendPacket method, you are sending the packet back to the user that it recieved the packet from correct? I was a bit confused because the way I was doing it before I would have a list of connections and anytime I got a message I would send it to everyone (basic chat structure) and if the server failed to send a message to the user it would close its socket.
This is the structure i decided to use for the tutorial, there are many different ways to handle the mass sending of information to all users. I decided to just send the latest update of the server to the client. You can also change this up, to store the player's packet info in the player class. Then send the server info to all users at the same time.

Since UDP does not deal with sockets per user, you can just send a shutdown message to the client. Then the client can recognize it and do the proper shutdown on the client side.

Thanks.

"Do what you love and you'll never work a day in your life."
Android: Pac-Ball
Web Applet: Virus Effect
Old Games
Offline Swattkidd7

Junior Member





« Reply #7 - Posted 2009-07-18 03:44:07 »

Wow man, excellent explanation! I really do appreciate it, great job and great work!

Thanks a lot! You deserve more than I have to give lol Cheesy
Offline Swattkidd7

Junior Member





« Reply #8 - Posted 2009-07-19 08:14:25 »

I hate to ask again but I have another question..How would I go about hosting the server on my computer with a static IP address (and xampp) and then having clients from other computers connect to it. Also my client is an Application, not an applet so the client server will not be together.
Offline Corvinex

Senior Member


Projects: 1


Work Hard, Go Pro.


« Reply #9 - Posted 2009-07-19 08:27:17 »

On the server computer you have to unblock the port number in your router's firewall settings. Once that is done, try it. If it still doesn't work then unblock the port in your computer's firewall settings as well.

Then just use the server's internet ip address in the clients ip variable. Hope that helps.

"Do what you love and you'll never work a day in your life."
Android: Pac-Ball
Web Applet: Virus Effect
Old Games
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Swattkidd7

Junior Member





« Reply #10 - Posted 2009-07-20 09:09:49 »

Thanks, I did forget to port forward, and got the server to host on my IP and got the Client to connect to the static internet IP, however another problem arises, and thats with the Server sending the info to the users Internet IP, because at the moment its sending it to the local IP and only users on my local wifi can connect...So im guessing I just need to figure out how to get the server to send to the clients internet IP..

Sorry for asking so many questions and if it seems like you are just spoon feeding me, but I have tried many things and am really understanding things. Also, I appreciate your help a lot!
Offline Corvinex

Senior Member


Projects: 1


Work Hard, Go Pro.


« Reply #11 - Posted 2009-07-20 11:09:49 »

If all your numbers are correct, and no one outside your local network is connecting, means that the server is still behind some firewall or router firewall. What I had to do when I was trying to figure this stuff out, was to manually go into the router's local web page, and specifically open up the port for my computer.

I hope that makes sense, and I'm happy to give back something to the community.

"Do what you love and you'll never work a day in your life."
Android: Pac-Ball
Web Applet: Virus Effect
Old Games
Offline Swattkidd7

Junior Member





« Reply #12 - Posted 2009-07-21 02:08:05 »

Yea it makes sense, but I am pretty sure I did that, I logged into my router and port forwarded my port. I also had my firewall and avast protection turned off. I am pretty sure all of that is good to go.
Offline markmistry

Junior Member





« Reply #13 - Posted 2009-07-21 07:25:20 »

once an external client has connected to the server via tcp theres an already established link providing that you keep it open i use this for most of my communications from the server to the clients,
if a client decides to close down at thier end the server tells the other server to log them off.
i found out ages ago about the port forwarding problem theres no way for a server to make a direct communications link to all clients cause of the router issue(thats why tcp is better than udp.end of story  Grin ) all clients must first connect to a server, in order to communicate.

On my setup i have 1 xp home edition thats running a web server a mysql server and a java application that handles all the connections from clients and other game servers.
 i have another xp home edition running another java application thats acting as a game/chat server. both of these are behind a firewalled/router and my external ip is dynamic Smiley. i make my clients connect to my website to find out what my server ip is. as long as it works it doesn't matter what way you code it right ?
 all i had to do regarding my clients firewall was allow java through i didnt have to mess with any ports Grin
the less your players have to do the better right ?
Offline Swattkidd7

Junior Member





« Reply #14 - Posted 2009-07-21 08:44:37 »

Yea you are right about the less your users have to do the better, but for this its not really the user that has to do anything, its just me having to set it up.

I believe I have found the problem, the problem was that I was never actually using the static ip  persecutioncomplex ...Although I am not sure how I would go about doing that, for example when we create the new socket in the ServerThread class I figured it would automatically start it up on my static IP address on port 5556 (does it)? if it doesnt, then I guess I will have to manually do that by using the datagramsocket constructor with the port and address? (However when I do that i get a bind error...)
Offline h3ckboy
« Reply #15 - Posted 2009-07-21 18:48:45 »

i have another xp home edition running another java application thats acting as a game/chat server. both of these are behind a firewalled/router and my external ip is dynamic Smiley. i make my clients connect to my website to find out what my server ip is. as long as it works it doesn't matter what way you code it right ?
 all i had to do regarding my clients firewall was allow java through i didnt have to mess with any ports Grin
the less your players have to do the better right ?

that is pretty cool, I had toyed with this idea, but now that I know someone has done it I may want to try it.
Offline markmistry

Junior Member





« Reply #16 - Posted 2009-07-21 20:14:24 »

just tell your router to forward what ports you want 5556 etc (tcp or udp or both) to what internal ip you want 192.168.2.5 etc etc

eg

loginport 5556 tcp to 192.168.2.5
chatport 3612 tcp to 192.168.2.6

remember your router will have its own internal ip address 192.168.2.1 or 192.168.1.1
easy peasy lemon squeezy  Grin

oh if you plan to run your own web server you need to forward port 80 in the same way

my servers are currently set to dynamic ip addressing and they work ok at the moment but every now and then i have to manually set them to thier respective ips(then i cant surf the internet at the same time).
Offline h3ckboy
« Reply #17 - Posted 2009-07-21 20:28:33 »

so your external ip changes everyday right? but then it is stored on your website. So then the game checks the ip and then connects to it. and then the router forwards everything on say port 5556 to a certain computer. am I right?
Offline markmistry

Junior Member





« Reply #18 - Posted 2009-07-21 20:43:53 »

in a nut shell yes something like that although my external ip is dynamic it will only change if i unplug my router, its just in case

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  
   

import java.net.*;
import java.io.*;


// on the server side...

public static void URLReader() {

      try{
     
         URL serverip = new URL("http://whatsmyip.org/");
         BufferedReader in = new BufferedReader(new InputStreamReader(serverip.openStream()));
         String sip ="";
         int a =0;
         for (;a<6;){
         
            sip = in.readLine();
            a++;

         }
         
         StringTokenizer newip = new StringTokenizer(sip,"      <title>Whats's My IP Address? Your IP address is ");
         String mynewip = newip.nextToken();
         System.out.println("server located at :"+mynewip);
         in.close();
         
         FileWriter fw;

         try{
         // change the location to suit your needs
                                               //coreservers.html could be a plain.txt file
           fw = new FileWriter("C:/Program Files/Abyss Web Server/htdocs/kickasssite2/coreservers.html",false);
            fw.write(mynewip);
            fw.close();

         }

         catch(IOException exc){
               
            System.out.println("cannot open file");
         }  
      }
     
      catch(Exception e){
     
         System.out.println("Unable to load the IP. Try your connection");
      }
   
    }

blah
blah
blah


URLReader();


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
// on the client side
  public static void clientURLReader() {

      try{
      // sometimes www.kickassevolution.com doesnt work so i just replace it with the external ip of my router
//my domain name is parked and i use there forwarding service that points to my external ip
        URL kickassip = new URL("http://www.kickassevolution.com/coreservers.html");
         BufferedReader in = new BufferedReader(new InputStreamReader(kickassip.openStream()));

         intro.regip = in.readLine().toString();
         
         in.close();
   
      }
     
      catch(Exception e){
   
         kickass.message ="Unable to load the Core IP. Try your connection";
      }
   
    }  
Offline Swattkidd7

Junior Member





« Reply #19 - Posted 2009-07-26 10:55:06 »

Sorry if this is offtopic, of the offtopic haha but I still cant seem to get the networking to work over the internet.. How can I test if the Server is running off my static IP for sure and not local (192. blah blah) and how can I make sure that the client is sending to  my static IP?

Thanks, and sorry if I am just not getting it lol..:\
Offline h3ckboy
« Reply #20 - Posted 2009-07-26 16:49:54 »

I just put up a basic tutorial couple weeks ago, has code and everything. Its not the most in depth, but it does have a simple structure to start out with.

Multiplayer Using UDP and Applets

this tutorial rocks corv. I finnaly went through and assimilated it. I got it wroking with a client of my own. and I am currently working on a pvp tech demo, it has worked like a charm.

THX
Offline markmistry

Junior Member





« Reply #21 - Posted 2009-07-26 22:12:14 »

Quote
Sorry if this is offtopic, of the offtopic haha but I still cant seem to get the networking to work over the internet.. How can I test if the Server is running off my static IP for sure and not local (192. blah blah) and how can I make sure that the client is sending to  my static IP?
when you work over the net you dont use your internal static ip use the url as something like this..http://86.20.236.211/...:(port number)

when your server program is running all it can see is its own internal ip and your computer should open the ports you want to use automatically.

the clients connect to the external ip of the router assuming your behind a firwalled router..you tell the router that the server is behind that any traffic from the internet that wants to connect to say port 35467 is to be forwarded to the internal ip of the machine thats running the server.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #22 - Posted 2009-07-27 08:20:14 »

http://www.canyouseeme.org/

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline h3ckboy
« Reply #23 - Posted 2009-07-27 09:18:52 »

this thread kinda got hijacked....
Offline shatterblast

Senior Newbie




Noobier Than Thou


« Reply #24 - Posted 2009-07-28 05:37:45 »

I jack your attention span.

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.

xsi3rr4x (19 views)
2014-04-15 18:08:23

BurntPizza (15 views)
2014-04-15 03:46:01

UprightPath (28 views)
2014-04-14 17:39:50

UprightPath (13 views)
2014-04-14 17:35:47

Porlus (29 views)
2014-04-14 15:48:38

tom_mai78101 (54 views)
2014-04-10 04:04:31

BurntPizza (111 views)
2014-04-08 23:06:04

tom_mai78101 (212 views)
2014-04-05 13:34:39

trollwarrior1 (181 views)
2014-04-04 12:06:45

CJLetsGame (187 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!