How can my Android app communicate with a native daemon? - java

So, I have a custom Android application running as System (built as a system app in my ROM). And Ive written a native daemon that works fine too and runs as root. However, I was wondering how the two can communicate? I know I can have the native daemon create a network socket and have the App connect to it, but using a local socket for IPC seems even better, but I am struggling to find example code for it.
I figured I could create the socket like as described here but I have no idea how to communicate with it in Java.
Any help would be much appreciated.
Also, at first I wrote a native library and used JNI to communicate with it from Java, but quickly realized that it obviously runs in the context of the app, so it cant be run as root, which is a requirement I have, and something the native daemon can do fine. Is using sockets the best alternative here or would Binder work too?
Thanks a ton!

The solution here was to look at AOSP and see how installd communicates with the Installer java app. In init.rc you see how the named local socket is created and in installd.c you see how native code communicates with the socket. In Installer.java you see how its done from Java land.
Dont think Binder would be the right way to go for my particular use case.

Related

How to let a java program comunicate with a different java program

I am trying to build a system that loadbalances minecraft players across multiple servers and creates new minecraft servers. I made a plugin to loadbalance players and it works like a charm. But now I want to make a system that creates new servers based on the data it receives from the plugin.
This system is completely seperate from the plugin. But how do I make it so the plugin sends some sort of command to the server system so it knows to create a new server. I have looked into Sockets but was wondering if this is the best solution. Maybe a Rest api will work aswell?
In my opinion the best solution is sockets based on your own protocol, it will be much more productive, than http or something else.
I can suggest using this lightweight socket library written by me -
Sonder.
It's working directly on sockets using nio.
You could solve this by having one java program write to a .txt file and another program read the line then delete the command using java.io.FileWriter and java.util.Scanner. I have a github project that does this here:
https://github.com/DPS100/Diego-s-Project-Euler/blob/master/Main.java

Does client-side java work on a server?

Can I write client side java in intelliJ, and then simply put it on a server for it to run on a server? Or will I need to write in a specialised version of Java and need to use a specialised IDE instead?
I'm trying to make an app and run the primary functions on a server then have the app send requests and fetch the data from the server. (It will esentially be a search engine of sorts and queries will be made to the server, which will then apply filters and use my algorithms to perform a search, before returning a result to the mobile device). But I'm not sure if I can begin writing this java on intellij, just as I would when making say, a simple calculator, or if I would need to write the java using a specialised server variation and server IDE.
Thank you.
The Java language is the same no matter what, so your logic can be ported easily. However on a server you may use a framework that's different from one you'd use on a client (for example you tagged this android- there wouldn't be Activities or Views on a server. You'd use another framework instead, possibly Spring.) Or you could go frameworkless, but then you'll have to write a lot of code that the frameworks do for you.
As for IDE- an IDE is an editor. You can write Java in notepad if you want. There's no difference, its just what features make your life easier. And yes, IntelliJ is a common choice.
If you would like make your own website, you should go with JavaEE or better with Spring.
If you would like to make simple server side application which will listen on specific port then respond something, go with Sockets
Netty is interesting NIO Socket library

Launch application and tracking its status on one Windows machine using other connected via LAN

I want to be able to communicate between two machines that are running Windows.
I want to be able to launch an application from one machine and then on another view its status as it runs.
Could anyone please help me with what technologies I can use?
I have done some research and found out that SSH can be used, but I am unsure of how to use it.
I would like to be able to use Java for it, if possible. If not, please suggest.
If I understood correctly, You can use RMi in this case.
The Java RMI system allows an object running in one Java Virtual Machine (VM) to invoke methods on an object running in another Java VM. RMI provides for remote communication between programs written in the Java programming language via the Java Remote Method Protocol (JRMP).
Hope this will help you.

Java Socket Programming in Eclipse

I was trying to create a socket program in JAVA using eclipse. I have the server and client code ready but I am not understanding how to run the code in eclipse for the server and client in the same project. Should I
1] have main method in both the server and the client and start running them individually, or
2] Should I use threads to run each of them separately, or
3] If I were to add another client in the network then how do I run that as well.
Please do help me and let me know which method should I adopt.
Thanks for your help.
You can do either #1 or #2. Eclipse has no issue with running/debugging multiple Java processes concurrently, nor is there anything invalid about having a single Java process that starts up both the server and client on separate threads.
However, I would suggest that in the real world it is most likely that you will not always be starting the server and client processes on the same machine, at the same time. So I think option #1 makes the most sense. You server and client apps should be able to run independently of each other, whether you're inside of Eclipse or not.
So if you do #1, then to add another client to the network you simply spin up another client process, the same way you did with the first client (Right click on the class -> Run As -> Java Application). You can start as many as you like that way. Though if you want to start up a bunch of them (like, say, for load testing), then consider creating another class with its own main() method that just spins up a bunch of clients on separate threads.
I think it would be better if you put individual main methods in the client and the server. This helps with the debugging and would help you determine which client is currently running if each client is run separately.
You can probably have a server java class file with main method and this is started by the eclipse. You can also have a client java class which have multi threaded to start a few clients. To identify the interaction between the server and the client, you can enable debug perspective in eclipse. This link should be able to give you idea on how you can start coding.

Java GUI + Server Issue

So far, I've been searching far and wide on the internet for the best practice when it comes to writing a Java server with a GUI (for stopping and starting it). I know that I have to run the server in a separate thread as the GUI but I was also thinking that it my be a little less complicated if I made each piece, the server and the GUI, in its own Java program or process. If I go with the latter, how do I interact with the server from the GUI if they aren't the same application. Would I just kill the process from Java. I really need some help (or maybe a tutorial).
P.S. I'm going to write the client for other PC's and android phones in the house so I could create a simple messaging system in the network for easy copy and paste between devices so I was considering building the GUI and server in with the client so I could distribute that and it would be two way but I'm not sure if thats the right approach or not.
Thanks!
You sir, are looking for an Application Server, like JBoss. Interact with it using web pages as the GUI. Use HTTP as your messaging protocol (POST and GET). Use Eclipse for Java EE
and read some tutorial / guide. It might be some to learn now, but you'll benefit from it later.
For your requested functionality, create a web application with a servlet for receieving HTTP requests first.
I don't think there is a 'right' way to do this. If as you say, you want to have more than one client (an app running on PCs and an app running on android phones), then you are better off going for a client-server architecture, where your server and your client are two (or more in the case of the different GUIs you want to create) different programs.
The way they can communicate is also open for you to choose. You can go low level and connect through sockets, or you could use HTTP and create web clients.
If you give us a bit more context about what you want to create (server and client are way too generic words) then we could give more tailored questions.

Categories

Resources