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.

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.

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.

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 ) 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 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 ...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...)

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 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.

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.5chatport 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.1easy peasy lemon squeezy

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?

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 ipURLkickassip = newURL("http://www.kickassevolution.com/coreservers.html");BufferedReaderin = newBufferedReader(newInputStreamReader(kickassip.openStream()));

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?

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 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.

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