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.
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.
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.
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.
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.
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.
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.
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.
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.
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!
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.
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.
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 ;D ) 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 :). 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 ;D
the less your players have to do the better right ?
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…)
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 ;D
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).
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?
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
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();
// 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";
}
}
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…:\