How can I implement a good Client-Server approach? - java

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.

Related

How can I connect an android with a local server?

sorry if my english isn't perfect.
I'm trying to make an app and I need to exchange information between more devices.
I thought that could be a solution connect the devices on a server but I really don't have the idea where start.
What language I need to study to make this? There is a better solution?
This highly depends on what you are trying to achieve in the first place. It would be helpful if you could tell what you are trying to do, but I will still outline some general aspects:
You need to decide, what information is going to be exchanged and how this should happen
What information: Figure out, what exactly needs to be sent and received. Generic text messages? Images? Byte Streams?
How should this be done: Generally spoken, there are two approaches of getting information as a client: Polling and subscribing.
Polling: This approach means to periodically check an endpoint for new data. For example, HTTP uses this way: A web browser or any other client (REST-Client for example) periodically requests information from a HTTP-Server, using a connection just for this single request.
Subscribing / Sync / Notification: In some way or another, the client tells the server that it is interested in the information and wants to get notified when there is something new. The connection is initiated at the beginning and held open for further usage. The benefit of this approach is that changes are received immediately, but on the other hand a permanent connection needs to be maintained.
Things to study
At the beginning, get a good understanding of the TCP/IP Protocol, how Sockets work, how common Protocols do their job (e.g. HTTP, WebSockets)
Take a look at specific Protocols working on top of the basic ones
Tip: REST: Most common WebServices Protocol, providing a common way to exchange stateless data. Uses Polling.
WebSockets: Socket connection using Web Browsers. Commonly used to update information without needing to poll.
There is no specific language to learn for connections. It's more about understanding what the difficulties are and what ways have been invented to address this. Once you get to this point and know what you want to do, it's possible in every language.
Recommendation: As you seem to use Java/Android, I would try to use REST. A really great client-side library for REST on Android is Retrofit. For the server side use what fits for you .. common Java way would be to use Jersey, but you are free to choose from a lot of choices. If using Jersey is too hard for the beginning, maybe take a look at the JS/NodeJS world, those guys invented Express, which allows you to create a REST service out of just a database, wihtout having to code a lot.
First you need to decide if you want to go for an Android or an iOS application. There are other various mobile operating systems as well, but these are widely used . If you want to go for android which is most widely used in my opinion, then you need to learn Java. If you want to go for iOS application, then you need to learn swift or objectiveC. These languages provide the API to connect with various types of services such as Facebook, Firebase and Amazon etc. If you want to connect to some other local server who’s IP is known to you, then you can use socket programming to send messages.
There could be many ways you can implement this. One way will be using Web services. Of course REST might be a better option, if you follow this approach. You can implement Your service(server side code) with any language. I will recommend you use java since you are already using android.
Aside from this You might need to go through the basics of REST, its specifications and
some reference implementations for language of your preference.

Inter-process-communication between a Java application and a local server

Firstly Cheers to all PROGRAMMERS [ Today = Programmers day :) ]
Secondly,
I'm working on a project where the specifications require using a server as a front end and an application in the back end. The project is an advanced smart home system. The server will handle commands coming from the client through the internet (let's say like a remote control from outside the house) and send them (through a channel of communication) to the application (planning on using JAVA application) which will handle the main logic like controlling hardware stuff (lights ...) , reading from a microphone (local mic) and accessing a database to act as a speech recognition system (offline).
Now I'm still in the planning phase and I'm not sure which technologies are the best for this project. I'm thinking to use Node.js or Apache as the server and a JAVA application as the back end and any SQL database for the application's SRS.
I hope this illustration demonstrates clearly how the system works:
The main question is:
What is the best way to make the Java application communicate with the server (communication channel [must be bidirectional]) ?
and Do you recommend a specific server other than the mentioned ones for this job ?
What crossed my mind so far:
1- JSP and servlets (making the server is the application too). But I don't want a server to handle the offline stuff and I'm not sure if java servlets can access hardware interface. I also want the server to be separate from making critical decisions (different layer for security reasons and since it won't be used as frequently as the local [offline] system).
2- Communication channel :
A- A shared file, but it's a bad idea since I don't want the application to check if the file contents changed (command received) or not from time to time (excessive operations).
B- A an inter-process-communication through a port (socket communication) seems the best solution but I don't know how that would turn in terms of operation cost and communication errors.
OS used : Linux Raspbian
EDIT:
I'm sure ZMQ+Apache is good enough for this task, but how is it in comparison to WebServices (like SOAP) ? Would WebServices be a better solution in terms of standard implementation and security ?
All related suggestions are welcomed, TQ
ZeroMQ is great for internal communications, or any other similar communication solutions.
For specifically your case, I can see that ZeroMQ would be a best fit.
Reasons:
You offline server have to be agnostic to web solution.
Communication can be reliable and bi-directional, possibly another patterns like (pub>sub, req<>res, etc).
Restarting any of sides would not require to restart the sockets (connection) on other side, as messages are queued.
Possibility to scale not just on same hardware, but as well to local area network or even through internet.
Big community of support. It might look a bit hard to get into, but in reality it is dead simple, just go to examples and once concept understood - it is very easy and neat to work with.
ZeroMQ has lots drivers for most popular languages, that includes Java and Node.js.
Considerations:
You need to think over packets and data will be sent. So some popular data protocols like XML or JSON is good way of thinking.
Responsibilities over different services - make sure they are not dependant on each other too much. Or if main offline server - is a core of system, make sure it does not depend on web facing service, so that web face can be removed/replaced/improved etc.
Few more points to think about:
Why Java, and what about modular approach? For example if you want to expand and scale - add more sensors into smart home solutions, then having one giant application would require to change it, it is harder to maintain as well as maintain different clients with own needs. Think modular way - some core functionality for offline stuff, but many aggregator processes that would talk to different sensors. This makes easier to support different setups and environments, as well maintain the system as a whole by improving independent components.

Simply opening a port in Java

I am coding a program that necessitates having two machines talk to each other via TCP.
I have a (very) basic familiarity with the java.net Socket classes and related things. I am worried about routers/firewalls blocking the connection. All I need to do is get a port open in order to use the Java STL Socket classes.
I was told to look into using upnp by a friend, and another stackoverflow question regarding upnp suggested using Cling. As a network programming novice, Cling seems like overkill for this (and I am not advanced enough to fully understand the manual).
I am sorry if I am asking the wrong question, looking in the wrong place, etc. All I want to do is connect two machines to send some floats back and forth without having to manually go into my router and forward ports.
Is there anything in the STL that does this automatically, or any simple libs?
First thing first: there's no such thing as "STL" in Java. You're probably referring to the basic Java SDK.
Second, you are asking for "simple means" to perform an operation that is, from a networking point of view, not trivial at all. Look at the most basic task that one of your programs (program A) will have to do: denoting the network location of its "target" (program B). If program B is behind a router, what do you know about program B's network location? nothing. All you know is the router's address.
Same with firewalls. You're concerned about firewalls blocking your connection? well, if your firewalls are any good, then you want them to block all connections except for pre-approved ones.
For programs separated by a NAT, the only sensible method to go about doing what you're looking for is to use UPnP. If Cling is an overkill, try something like weupnp.
There's no free lunch, though.
If there is no network "masking" device (such as a router) between program A and program B, then very simple TCP/IP programming using the Java SDK should just work. If there are networking devices along the ways, you'll have to cope with them by means of configuring your devices, or using UPnP.
I would take a long look at the following tutorial: http://www.giantflyingsaucer.com/blog/?p=224
The idea here is that a library called xsocket creates events that trigger when new information arrives.

Client-server game in Java with applets

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.

Java server-client model

This question is more concerned with the general design of a program. I have a problem in hand in which a centre communicates with a set of clients individually (the clients do not directly interact with each other).
The content of the communication between the centre and a client is entirely numeric, i.e. a bunch of numbers are sent back and forth. The clients are run on individual computers, and the centre can be hosted on another machine (or otherwise). The style of communication is iterative and synchronous in a sense that the centre sends a request/question msg to each individual client and the user of a client provides an answer/reply back to the centre within a time limit in each iteration, and this interaction repeats itself for a number of times until the end.
The centre does some calculation given the responses received from the clients, and the clients simply provide the inputs to that calculation.
I wonder what is an elegant design for this problem using Java? Any comment or suggestion is much appreciated. Many thanks.
I'd use KryoNet which can efficiently serialise your datasets over RMI invocations (remote method calls). Using it myself, and it works well. Has UDP and TCP without any mucking about. Start from the example on the site and work from there. You can easily get both sides of the connection working with RMI.
Sounds the MPI is right for you. Here is a Java version.
Good luck and Regards.
The simplest solution in pure Java is probably to use RMI. Just expose an object via RMI and have the clients call methods on it remotely.
If you want asynchronous behavior, you can use a JMS implementation or roll you own using sockets and TCP/IP as mentioned above.
If you don't like any of those, the MPI mentioned above may be for you. Or possibly Apache Hadoop may be for you.

Categories

Resources