I want to make a chess online game in Java. I am able to write the game offline, that is no problem, but now I want to make it online so people can play over the internet.
Unfortunately i have no actual idea of how to do this so here are my questions:
What kind of server do i need? (like webserver, xyz...?)
How can i save the Chess board on a server?
Do i have to make this with sockets and make it with the MVC? (so the client is a dumb buddy which only asks if this move is valid, if so it sends the info to the server which says yes/no and then makes the move and sends the new board to the clients again?
Will i need Threads to make multiple instances of the boards on the server so multiple people can play at once?
Thanks to everyone showing the effort trying to help me! As you can see i am new to this and i do not know where to start, but i really want to make this!
You could set up a Tomcat server somewhere and put your programmed servlet there, but for you that may be a bit to much (either renting one or configurating one on a local machine). As an alternative one of your local PCs can just listen on a port, which is usually managed by the tool you use to communicate.
The basics for loading and saving to disk are done with FileInputStream or FileOutputStream. It doesn't matter if your JVM is on your local PC or on a remote system. The advanced tricks are Serialization and APIs like Simple XML for Java.
I'm not sure if I understand the question. If you're asking where the validation of the move should take place, that is personal taste, although I think it should take place on client and server. If you're asking how client and server should communicate, I think Remote Method Invocation (RMI) would be a good start for you. I came to love Akka, but I think that one is a little bit too much, but you can look into it, if you like. Sockets are to RMI and Akka what is going by foot to driving a bike or a car. It's good to understand communication, but not helpful for direct use in higher level programming (as in games).
Each game could run in own thread, yes.
I think a good exercise for you would be to look up all words unknown to you from my answer, there is much to learn.
Related
I am a new one in Java programming i want to know how overall concept of Socket and Server-Client work and how modification in one's system tends to the modification in other's system, As a starter i am making TicTacToe 2 player in java, i had made TicTacToe for Single Player but want some help to make it in multiplayer.I had gone through many question (on this site) before but they only solve some kind of problem but i want a full knowledge that how this all stuff works.
Can any one please tell me that how Server and Client do the message passing in Java ??
I want to ask where to write my Game Code so that it get loaded on both the devices and when user make a turn it get displayed into another user's device also...
There are many different answers/solutions for the use-case you described.
But never never develop network communication on a low-level layer. There are different solutions (api) to put the data(messages) into higher layers(f.e. HTTP). That enables you to turn your software very easy into a multithreaded and/or multisession-app.
I would prefer in your simple application an implementation with jersey and grizzly. You'll find lot of how-to's with google ("web services jersey grizzly") and here in stackoverflow of course. ;)
I'm developing a distributed application, and I need to connect a client Java based to a server C++ based. Both of them will need to send information to each other, but I need them to be able to do things while waiting for the information, and they don't know when they are gonna get new information, or send information.
How can I achieve this? Now I'm trying to implement a basic communication with Sockets, but I don't really get to communicate them. I have read that using sockets + threads is usually a good approach for client-server apps.
Could you please recommend me some web or book to read about this, or send me some example code to learn?
Do you think that i should use other approach, better than sockets? maybe a higher level library (i would need it for c++ and java) or a totally different way?
EDIT:
I will add some extra information.
What I would love to achieve is the following:
My C++ program has a main loop, where I would like to have a call like GetUpdatedDataFromRemoteDevice() where I read the new values of some numerical variables that previously got updated from the net (the socket, for example).
Eventually, the C++ program will need to send a message to the remote device, to tell him to send other kind of data, and after that, keep getting the updated values.
From the Java program (remote device) the application running is an interactive touchable screen, that cant get blocked by the network transmissions, because it must keep working for the user, so all the networking should be done in a separated thread.
That thread, should connect to the server, and when a button is pushed, start to send the data (4 changing numerical values) in a loop until another event happens.
It would be nice also to be easily re-connectable to the server.
ICE is a modern and good library for distributed applications:
many languages as C++ and Java
many platforms
GNU GPL
good performance
easy to use
First, you define the messages you want to exchange between server and client.
Then, you implement the C++ and Java source code to handle these messages.
More info at http://zeroc.com/ice.html
Have fun ;-)
EDIT: I have to use ACE in some projects. I can tell ACE is very old, maybe mature, but uses outdated C++ coding rules :-(
Therefore ACE is not as easy to use as STL or BOOST. Moreover, ACE is not really efficient... I prefer ICE ;-)
I don't know what your application is but robust client server socket programming is pretty hairy task to do properly. Hardware byte order, String encoding, Network errors, retries, duplicate messages, acks etc.. require lots of good design and careful programming. You need to get it work well as single-threaded before even thinking using multiple threads.
Unless you need instant notifications from server to client I suggest that you use HTTP as protocol between client and server. Client can poll server occasionally for new messages.
Anyway the problem has been solved multiple times already.
http://activemq.apache.org/
http://www.rabbitmq.com/devtools.html
http://www.cs.wustl.edu/~schmidt/ACE-overview.html
I did something of this sort once. In my case it was easier to connect my C++ app to a local Java app using JNI and then have the two Java apps talk to each other.
I'm currently working on trying to create a very basic bare bones instant messenger with Java.
I'm at a bit of a loss where to begin, was wondering if anyone here could reccommend a starting point? What I should read? What would be the easiest way for a beginner/intermediate programmer to try and achieve this?
Currently, I'll I'd want it to be able to do is to send text to one machine to another. I am so far unfamiliar with anything network related in java. Would I use a server or just direct connect between two machines?
Any pointers/direction or advice anyone has in this matter would be very helpful!
Firstly check out the official resource for Socket programming, the article on writing a Client/Server Pair should provide you with what you need to know to create a instant messenger.
Well, I am focusing the second question of yours.
If you're building this program for your local area network and if you're intended to use socket programming, you need to find the exact ip address of the machine. If you don't know the ip address ( I mean if you don't want the program to know ip address before you run it ) ,then you can ping local ip addresses until you find some peers.
Some of the p2p applications use servers as the ip matchers. This means that the servers have ip address tables which make all of the users send/receive data by using these servers. But if you just started on networking with this project, I think don't use server.
Lastly, you can take a look at a project of my own. It's a simple parallel document preparation program with a chat module inside it. It's developed in C#. It needs wireless ad hoc network to run. Link : http://code.google.com/p/parallel-docs/
I don't think this type of project is ever going to be a beginner project. I'm absolutely not discouraging you to try it, I think you should. I'm always in over my head with stuff like this but I look it up and ask questions and usually end up learning something. My advice would be no matter how simple of a program it is, start with some Class Diagrams, if nothing else just to get organized before jumping into the code. For example you know you you'll need a user account object right off the bat and you know you'll need certain attributes for that object (username, password, etc.), I encapsulate everything so then you would need methods to get and set these fields and so on. I've found that by doing that even if I don't create complete proper UML diagrams getting the easy stuff out of the way and if somewhat organized first if nothing else frees me to focus on the more complex logic and things I may not yet know how to implement.
I have to implement a simple tour client-server game in Java. Unfortunately, I'm just beginning with network programming and have some problems with choosing an apropriate solution.
Let's assume I'm creating a chess game where two clients can connect to the server, authenticate with their username and passwords and play. The clients must be programmed as applets (thin clients), but I don't know what I should use as a server.
I mean, I've read about several different possibilities like RMI, sockets, servlets, but still don't know which one fits bets my needs. I'm a bit confused because I don't fully understand how the communication would be carried out.
Should I create an executable server which would run all the time on the server and wait for the players? This seems to me like an odd way. Or is there any easier way to do so, e.g. can I make a servlet and put it on Tomcat server so that the server would be run only if there are any players? Could that servlet communicate with applets (clients) and vice versa?*
I'd be really grateful for some tips.
can I make a servlet and put it on Tomcat server so that the server would be run only if there are any players
The Tomcat instance would run anyways, otherwise players couldn't connect to it.
What you could do is to provide a server that starts a new game instance when players connect. The server itself would have to always run.
In terms of technology, I'd suggest you use whatever you feel comfortable with. Don't care about performance yet but try and get started.
So if you already have some knowledge with a communication technology, try and use that. Just be aware of the limitations and take those into account (e.g. message formats, push/pull communication etc.).
It depends what kind of game you are after. Applets are usually good choice for presenting animation (completely in Java) and accessible from a browser. Real world examples would divert towards Flash for client presentation.
If your game is a turn type game (chess, cards etc.), then you can implement your logic in form of servlets or web services or ajax, with appropriate use of hashtables or databases to store live sessions on server side. If your game is more involved in terms of user experience (take an example of Need For Speed type, for instance), then creating a custom server make more sense.
If you are looking for a netwrok application framework in Java then you may consider reading about Apache MINA. Documentation claims that it " helps users develop high performance and high scalability network applications easily." and it has support for various transports such as TCP/IP and UDP/IP via Java NIO. Summary of features can be seen here.
My personal experience with MINA is so far good and used in various projects. One implementation resembles your case, Its not a mulitplayer game but do involves multiple applets connecting a server. I found MINA very good in handling multiple sessions. It do it very neatly. Moreover its very easy to scale and maintain code. Easy to add filters and define protocols.
There are no. of good tutorial available to jump start and initial setup is very easy to do.
However, like any emerging opensource project, it has its problems too. That are: Online community is small and documentation, though improving rapidly, is very limited.
Official user guide covers most of the basics and is a good starting point if you want to know more.
I'm looking to find ways to prevent a Java made game such as runescape from being hacked by using the client server model for keeping the important source code in the server and sending the data to the client. My question is how would it be possible to make something like this, any information links or articles would be helpful.
I'm also wondering what kind of information can someone send from a server that can also be rendered in a 3d graphics engine, or how is it done?
You can't do much about the client being decompiled - you can obfuscate your source code to make it more difficult to reverse-engineer. This will make getting readable source code slightly more difficult, but not impossible. A determined and skilled user could stil reverse-engineer the client.
However you can do a lot of work on the server where the source code can't be recovered. For example you could code the artifical intelligence for NPCs, and all physics calculations on the server and just send the results to the clients.
In fact these sorts of calculations should be done on the server anyway to keep the game consistent and to prevent clients from cheating.
The client will typically do most of the presentation such as 3D rendering. This is usually done locally for performance reasons, so that the rendering can take full advantage of the graphics card in the client.
I'm also wondering what kind of
information can someone send from a
server that can also be rendered in a
3d graphics engine, or how is it done?
While I am not a game/3d developer myself, I found this article illuminating (browse their wiki for more!)
Source Multiplayer Networking [valvesoftware.com]
Every program, given sufficient manpower and time, can be reverse engineered. If you can give a team of testers/developers the program to use for a while, they are going to be able to mimic its functionality. A great example is the MMORPG games out there (think about Ultima Online and the plethora of open source Shards that mimic it and work with the UO client).
You can obfuscate the code, and make it hard to decompile etc., but at the beginning of a project that is beyond overkill. Make something people will be willing to pay for, take the very basic source-code protection precautions and then rely on copyright law to shield you from reserve engineering (its not perfect, but nothing is).
The client server model in theory could prevent many forms of cheating by only sending specific information between the two, notably input from the player and the players view of the game from the server.
However in practice this is rarely done. Lag prevents a real time game from playing adequately without having the client handle at least part of the positioning and physics work. This means a player has copies of information stored locally that they shouldn't have in the game.
The client server model also does little for hacks with macro actions. Players can hack their clients to repeat actions or assist actions (such as aiming). Certain other information can recreated on a hacked client from the information the server provides.
Rendering the 3d scene itself falls into three camps.
Each client can render it however
they feel. It doesn't matter if you
use sword swing A or sword swing B
if they don't interact differently
with the environment.
Clients can make a best guess, and have to reconcile if wrong. I lose connection for 2 seconds. My character keeps running on your screen because your client assumes that's what I would be doing. I reconnect, but I had told the client to turn. Either I jump on your screen or mine.
The server decides. The server says a barrel falls along algorithmic path C. This is most common when the falling barrel interacts with the environment i.e. it can hit and damage other things.