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. ;)
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm new to Java, and I wanted to do something cool with it. I came up with the idea to set up a local server for my home with it as the first step. I want a simple server running without internet, which all the devices connected to my home wifi can view.
On a later stage, I'm planning to do various stuff with it, like for starters a simple chat application. Or a portal for sharing files between my devices.
And in the end product, I want to do streaming. Like the host plays a music on the server and all the connected devices can go to the page and listen to the same music completely synced. Or stream a video!
It's just an idea for the moment, I know this kind of stuff may take a lot of research work, but being new I'm really confused where to start. I just need suggestions/guidance if what I'm saying is possible, and what can I do to get to where I want.
Thanks in advance! :)
It's a long journey if you are building from scratch. There a lot of API which you can use to achieve this very easily. But, I am just going to explain you from very basics and then you can go forward and do more research.
There are 3 main concepts to achieve what you are saying:
Clients: Things which sends request to a server. The devices like mobile, laptop etc.
Server: Thing which receives the request from client and do some processing and return the result back. Now in real life server is just a software program (can be written in many languages Java is one of them) which runs on a computer and listen for clients request on a ip address + port (ip + port is like an address for server). This is same as if I want to send a letter to you I should know your address.
Socket Programming: Socket programming defines protocol and mechanism through which client and server can communicate. In above image all the links are made using socket programming. Thread in above image allow concurrency so for above image every client is like a thread for server.
In your case because you want to connect your client to server through wifi. Your architecture would look like this.
Client
Client ------------------- WIFI ---------------- SERVER
:
:
Where WIFI is just forwarding your request to server and response from server to your client.
Now as you want to achieve different things like chat application and live streaming.
Chat Application (https://www.codeproject.com/Articles/524120/A-Java-Chat-Application)
For chat application we have to make sure that the message which we have sent must reach the destination.
To allow this 100 % accuracy socket programming provides you with TCP protocol.
Streaming Application {Audio/Video} (Live audio stream java)
For streaming application TCP protocol is not neccessary because of two reason.
Firstly, we actually are ok that if one or two packet are lost hence you can see when using youtube there are some glitches.
For online streaming most important thing is it should be fast and TCP is a very heavy protocol.
Thats why Socket Programming also allow you to use UDP protocol which is faster then TCP but don't provide the guarantee that message will reach.
Above is a very brief introduction in laymen term. For better
understanding you have to read about Socket Programming. Once, you did
that you can do above project. However, if you see above link you can
do your project but you won't understand anything and more importantly
in case of failure you can troubleshoot.
That's a nice project to work on. You'll be learning a lot.
Maybe one simple thing you could do as a start is to set up a server to broadcast information using a web socket.
You can find a lot more on internet, but here are some examples:
http://www.baeldung.com/java-websockets
http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/HomeWebsocket/WebsocketHome.html
I don't know if it performs well broadcasting data, but I've been working on a chat application and for simple messages it does the job really well.
Of course you can do what you are thinking, however as you said it yourself it will take alot of RnD to complete things up.
I'll suggest you the way to start would be first to identify and divide your work into different modules for e.g. (File sharing, chat, Music store, etc.)
Then for each of the modules create small use-cases e.g.
FOR File sharing :
display a directory contents
switch directory
create directory
download file
.....and so on.
I assumed you have knowledge of programming and web applications :p
Start development work only after above uses cases are complete. Begin with taking one use-case at a time e.g.
display a directory contents
create a servlet/jsp for that deploy that and then go on with next one untill you complete all the usecases and modules.
That is an VERY ambitious project for someone new to Java. I'd recommend you don't start with a server first, especially a streaming one. Start with basic things, learn about datatypes, classes and object, data structures, collections, things like that. Learn to use the debugger, cannot stress the importance of that one enough. Once you feel comfortable with those concepts, then you move onto servers. By then you'll have a much better grasp on the language and most importantly will understand what Java is telling you when you are looking at a stack trace.
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.
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 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.